Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well first off, you have to come to grips with the fact that SQL Server refuses to allow dynamic SQL in functions. It's unfortunate, but it is a limitation you must work around.</p> <p>Here is a technique to apply dynamic SQL logic on each row of a table</p> <ul> <li>Create a temp table to hold all the input parameters for your dynamic SQL as well as the expected results</li> <li>Create a stored procedure that already expects the temp table to exist and loops over the rows of the temp table applying the dynamic SQL logic</li> <li>Add the data you want evaluated to the temp table</li> <li>Execute your dynamic SQL proc</li> <li>Enjoy your expected results :-)</li> </ul> <p>1) Let's start with setting up some test data.</p> <pre><code> -- ==================================================================== -- BEGIN: Setup test data -- ==================================================================== IF OBJECT_ID('Branch', 'U') IS NOT NULL DROP TABLE Branch; IF OBJECT_ID('Branch2', 'U') IS NOT NULL DROP TABLE Branch2; GO CREATE TABLE Branch ( ID int ,Column1 varchar(20) ,Column2 varchar(20) ,Column3 varchar(20) ) CREATE TABLE Branch2 ( ID int ,Column1 varchar(20) ,Column2 varchar(20) ,Column3 varchar(20) ) SET NOCOUNT ON INSERT Branch SELECT 1, 'Hello', 'World', 'I write SQL' INSERT Branch SELECT 2, 'I like', 'it to be', 'complicated' INSERT Branch2 SELECT 1, 'Hello', 'World', 'I write SQL' INSERT Branch2 SELECT 2, 'I like', 'it to be', 'complicated' SET NOCOUNT OFF GO -- ==================================================================== -- END: Setup test data -- ==================================================================== </code></pre> <p>2) Next, we'll create a proc that handles the dynamic SQL.</p> <pre><code> -- ==================================================================== -- BEGIN: Proc with dynamic SQL logic -- ==================================================================== IF OBJECT_ID('prcReturnTableData', 'P') IS NOT NULL DROP PROCEDURE prcReturnTableData; GO CREATE PROCEDURE prcReturnTableData AS DECLARE @SQL nvarchar(MAX) ,@ParameterDefinitions nvarchar(MAX) ,@TableName varchar(128) ,@ID int SELECT @ParameterDefinitions = '@ID int' DECLARE @idx int SELECT @idx = MIN(idx) FROM #Result WHILE @idx IS NOT NULL BEGIN SELECT @TableName = TableName ,@ID = ID FROM #Result WHERE idx = @idx SELECT @SQL = ' DECLARE @ReturnVal varchar(MAX) SELECT @ReturnVal = '''' SELECT @ReturnVal = @ReturnVal + '' // '' + T2.N.value(''local-name(.)'', ''nvarchar(128)'') + '': '' + T2.N.value(''.'', ''nvarchar(max)'') FROM ( SELECT * FROM ' + @TableName + ' WHERE ID = @ID FOR XML PATH(''''), type ) as T1(X) CROSS APPLY T1.X.nodes(''/*'') as T2(N) UPDATE #Result SET ReturnVal = @ReturnVal WHERE ID = @ID'; EXEC sp_executeSQL @SQL, @ParameterDefinitions, @ID = @ID; SELECT @idx = MIN(idx) FROM #Result WHERE idx &gt; @idx END GO -- ==================================================================== -- END: Proc with dynamic SQL logic -- ==================================================================== </code></pre> <p>3) Finally, let's execute the proc and get the results we want.</p> <pre><code> -- ==================================================================== -- BEGIN: Solution to execute dynamic SQL logic on each row of a table -- ==================================================================== -- Create the temp table that the dynamic SQL proc expects IF OBJECT_ID('tempdb..#Result', 'U') IS NOT NULL DROP TABLE #Result; CREATE TABLE #Result (idx int IDENTITY(1,1), TableName varchar(128) NOT NULL, ID int NOT NULL, ReturnVal varchar(MAX) NULL); -- Popluate the temp table with the rows you want to evaluate INSERT #Result (TableName, ID) SELECT 'Branch', ID FROM Branch INSERT #Result (TableName, ID) SELECT 'Branch2', ID FROM Branch2 -- Results before SELECT * FROM #Result -- Execute the dynamic SQL proc EXEC prcReturnTableData -- Results after SELECT * FROM #Result -- ==================================================================== -- END: Solution to execute dynamic SQL logic on each row of a table -- ==================================================================== </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload