将表变量与 sp_executesql 一起使用

Posted

技术标签:

【中文标题】将表变量与 sp_executesql 一起使用【英文标题】:using Table variable with sp_executesql 【发布时间】:2011-09-07 06:58:58 【问题描述】:

我有一个包含表变量的查询:

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

如何将表变量传递给我的查询?

【问题讨论】:

【参考方案1】:

这是一个如何将表值参数传递给sp_executesql 的示例。变量必须通过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

这将打印Answer to the Ultimate Question of Life, the Universe, and Everything。

【讨论】:

你的意思是没有办法传递Table变量?这比我们的temprory table好吗? @Nima:你可以传递一个表变量,事实上,这个例子就是这样做的。但表变量必须具有命名类型。所以它必须是declare @t TestTableType(命名类型)而不是declare @t (id int)(匿名类型)【参考方案2】:

试试表格类型

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

【讨论】:

我收到了这个错误:Msg 155, Level 15, State 2, Line 7 'TblSelect' is not a recognized CURSOR option,我不知道我的@Col_Select 有问题,我也收到了错误 查看此链接以获取更多参考msdn.microsoft.com/en-us/library/bb510489.aspx DECLARE TYPE TblSelect AS TABLE 应该是CREATE TYPE TblSelect AS TABLE

以上是关于将表变量与 sp_executesql 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

将表字段转换为jCheckBox [复制]

如何将 sp_executesql 结果转换为变量?

[mysql-connector-python在将表作为参数传递时,在表名周围添加单引号。表名来自Flask会话变量

SQL Server - 执行直接查询与使用 sp_executesql 执行

sqlserver 动态sql执行execute和sp_executesql,用动态SQL语句给变量赋值

SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别