Skip to content
Advertisement

Declaring A variable inside dynamic ssql

I have the following string and would like to replace >30 at the end of the string with @value so I can pass it through a stored procedure. How do I declare @value. My orginal code had >35′ in place of vaule and worked fine. Thanks in advance.

 Declare @value  int
 set  @value=30

 exec (' insert into results_stats select'''+
@tab_name+''','''+@col_name+''',max(len('+@col_name+')),'+@col_name+','''+@dat 
a_type+'''  from LIVE.'+@tab_name+' Group By '+@col_name+' Having 
max(len('+@col_name+'))'''+@value+'')

Advertisement

Answer

You want sp_executesql().

Read through the documentation in the link; it covers how to do this in detail.

The other thing to be aware of is you may need to include the > operator from your >35 as part of the string literal.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement