Wednesday, May 28, 2008

SQL : Passing local variables to dynamic SQL statements.

Declare @DSQL nvarchar(4000), <@inputLocalParameter> , <@outLocalParameter>

Set
@DSQL = N'Select <@outLocalParameter> = from where like '''+<@inputLocalParameter>+''''

Exec sp_executesql @query =
@DSQL, @params = N'<@outLocalParameter> output', <@outLocalParameter> = <@outLocalParameter> output

Print
<@outLocalParameter>


<@inputLocalParameter> - Parameter / Local variable that is an input to the dynamic query session.
<@outLocalParameter> - Parameter that is set and retrieved from the dynamic query session.
- Table name that the data is fetched from.
- Column Name referred in the table.
@query, @params - would be wondering wr they jumped from, they are inbuilt parameters of
the system stored procedure "
sp_executesql", so don't u panic!!!

No comments: