Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a TABLE variable to sp_executesql

Tags:

I'm trying to pass a TABLE variable to the sp_executesql procedure:

 DECLARE @params NVARCHAR(MAX)  SET @params = '@workingData TABLE ( col1 VARCHAR(20),                 col2 VARCHAR(50) )'   EXEC sp_executesql @sql, @params, @workingData 

I get the error:

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'TABLE'. 

I tried omitting the column specification after 'TABLE'. I also tried to declare the table as a variable inside the dynamic SQL. But no luck...

Seems to me that TABLE variables aren't allowed to be passed as parameters in this procedure?. BTW: I'm running MSSQL2008 R2.

I'm not interested in using a local temp table like #workingData because I load the working data from another procedure:

INSERT INTO @workingData      EXEC myProc @param1, @param2 

Which I cannot do directly into a temp varaible (right?)...

Any help appreciated!

like image 504
Alex Avatar asked Nov 23 '10 17:11

Alex


People also ask

How do you pass variables in SQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

Can we use table variable in dynamic SQL?

Answers. This is not possible. Cause the table variable is isolated from the scope of the dynamic SQL. You can only use temporary and normal tables.

What is difference between EXEC and Sp_executesql?

sp_executesql supports parameterisation, whereas EXEC only accepts a string. Only performance differences that may arise are due to the parameterisation i.e. a parameterised sp_executesql call is more likely to have a reusable cached plan.


2 Answers

If you are using SQL Server 2008, to pass a table variable to a stored procedure you must first define the table type, e.g.:

CREATE TYPE SalesHistoryTableType AS TABLE (                          [Product] [varchar](10) NULL,                     [SaleDate] [datetime] NULL,                     [SalePrice] [money] NULL ) GO 

or use an existing table type stored in the database.

Use this query to locate existing table types

SELECT * FROM sys.table_types 

To use in an stored procedure, declare an input variable to be the table:

CREATE PROCEDURE usp_myproc (     @TableVariable SalesHistoryTableType READONLY ) AS BEGIN     --Do stuff       END GO 

Populate the table variable before passing to the stored procedure:

DECLARE @DataTable AS SalesHistoryTableType INSERT INTO @DataTable SELECT * FROM (Some data) 

Call the stored procedure:

EXECUTE usp_myproc @TableVariable = @DataTable 

Further discussions here.

like image 115
Gary Kindel Avatar answered Oct 18 '22 16:10

Gary Kindel


OK, this will get me what I want, but surely isn't pretty:

DECLARE @workingData TABLE ( col1 VARCHAR(20),         col2 VARCHAR(20) )      INSERT INTO @workingData         EXEC myProc      /* Unfortunately table variables are outside scope        for the dynamic SQL later run. We copy the         table to a temp table.         The table variable is needed to extract data directly        from the strored procedure call above...     */     SELECT *      INTO #workingData     FROM @workingData           DECLARE @sql NVARCHAR(MAX)     SET @sql = 'SELECT * FROM #workingData'      EXEC sp_executesql @sql 

There must be a better way to pass this temporary resultset into sp_executesql!?

Regards Alex

like image 43
Alex Avatar answered Oct 18 '22 14:10

Alex