However the requirement itself is a bit strange for me, but here is a way that could be a good start point for you:
declare @var1 int
Set @var1= 1
--some code here
declare @var2 nvarchar(max)
set @var2 = 10
--some other code here
declare @var3 bit
print @@VERSION
print 'this is fake @value inside a string'
--$ This is a Hint to help me find the Query that should parsed
declare @sql varbinary(max)
select @sql=sql_handle
from sys.sysprocesses
where spid=56
declare @q nvarchar(max)
select @q= substring(text,1,charindex('$',text)-3) from sys.dm_exec_sql_text(@sql)
Select distinct rtrim(ltrim(substring(Name,1,charindex(' ',Name)))) as Name from(
Select substring(replace(Name,'=',' '),8, Len(Name)) as Name from dbo.SplitString(@q,'declare ')
) as K
where Name like '@[^@]%'
By running the above query you will get the variables name.
Output:
@var1
@var2
@var3
You can find the source code for SplitString function Here
Note: If you are using SQL Server 2016 and your database's compatibility level is equal or greater than 130, you can also use SPLIT_STRING
introduced by Microsoft it self. Learn more Here