36

I have a query that contain a table variable:

DECLARE @Selects    XML ;
SET @Selects='<Selects><Select><Q_ID>1</Q_ID><Q_DESC>nima1</Q_DESC></Select><Select><Q_ID>2</Q_ID><Q_DESC>nima2</Q_DESC></Select><Select><Q_ID>3</Q_ID><Q_DESC>nima3</Q_DESC></Select></Selects>'

DECLARE @QuestionID     NVARCHAR(10);
SET @QuestionID='a5';

DECLARE @TblSelect  TABLE 
(
    Q_ID            INT,
    Q_DESC          NVARCHAR(500)
)

INSERT INTO @TblSelect
(
    Q_ID,Q_DESC
)
SELECT  Q_Select.value('(Q_ID)[1]', 'int') AS 'Q_ID',
        Q_Select.value('(Q_DESC)[1]', 'nvarchar(500)') AS 'Q_DESC'
FROM    @Selects.nodes('/Selects/Select') AS AllSelects(Q_Select) 

DECLARE @Query      NVARCHAR(4000);
SET @Query=N'SELECT Q_ID,COUNT(Q_ID) FROM @TblSelect LEFT OUTER JOIN tblbase tb ON @TblSelect.Q_ID = @Col_Select group by Q_ID';

EXECUTE sp_executesql @Query,@TblSelect,@Col_Select

How I can pass the table variable to my query?

wonea
  • 4,783
  • 17
  • 86
  • 139
Arian
  • 12,793
  • 66
  • 176
  • 300

2 Answers2

56

Here's an example of how to pass a table-valued parameter to sp_executesql. The variable has to be passed readonly:

if exists (select * from sys.types where name = 'TestTableType')
    drop type TestTableType

create type TestTableType as table (id int)
go
declare @t TestTableType
insert @t select 6*7

exec sp_executesql N'select * from @var', N'@var TestTableType readonly', @t

This prints the Answer to the Ultimate Question of Life, the Universe, and Everything.

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 1
    you mean that there is no way to pass Table variable?Is this better than us temprory table? – Arian Sep 07 '11 at 07:50
  • 1
    @Nima: You can pass a table variable, and in fact, this example does so. But the table variable must have a named type. So it has to be `declare @t TestTableType` (named type) and not `declare @t (id int)` (anonymous type) – Andomar Sep 07 '11 at 08:23
  • 1
    Readonly means: NO to the question. Basically,executesql and table type can only be used as read-only. – alejandrob Jun 30 '22 at 19:41
2

Try Table Type

DECLARE @Selects    XML ;
    SET @Selects='<Selects><Select><Q_ID>1</Q_ID><Q_DESC>nima1</Q_DESC></Select><Select><Q_ID>2</Q_ID><Q_DESC>nima2</Q_DESC></Select><Select><Q_ID>3</Q_ID><Q_DESC>nima3</Q_DESC></Select></Selects>'

    DECLARE @QuestionID     NVARCHAR(10);
    SET @QuestionID='a5';

    DECLARE TYPE TblSelect  AS TABLE 
    (
        Q_ID            INT,
        Q_DESC          NVARCHAR(500)
    )

    /* Declare a variable that references the type. */
    DECLARE @TblSelect 
    AS TblSelect ;

    INSERT INTO @TblSelect 
    (
        Q_ID,Q_DESC
    )

    SELECT  Q_Select.value('(Q_ID)[1]', 'int') AS 'Q_ID',
            Q_Select.value('(Q_DESC)[1]', 'nvarchar(500)') AS 'Q_DESC'
    FROM    @Selects.nodes('/Selects/Select') AS AllSelects(Q_Select) 

    DECLARE @Query      NVARCHAR(4000);
    SET @Query=N'SELECT Q_ID,COUNT(Q_ID) FROM @TblSelect LEFT OUTER JOIN tblbase tb ON @TblSelect.Q_ID = @Col_Select group by Q_ID';

    EXECUTE sp_executesql @Query,@TblSelect,@Col_Select
Nighil
  • 4,099
  • 7
  • 30
  • 56
  • I got this Error: `Msg 155, Level 15, State 2, Line 7 'TblSelect' is not a recognized CURSOR option` and I don't know hat is wrong with my `@Col_Select` I got error for it too – Arian Sep 07 '11 at 07:20
  • see this link for more referance http://msdn.microsoft.com/en-us/library/bb510489.aspx – Nighil Sep 07 '11 at 07:29
  • 4
    `DECLARE TYPE TblSelect AS TABLE` should be `CREATE TYPE TblSelect AS TABLE` – lazarus Sep 21 '15 at 10:30