gpt4 book ai didi

sql - 将用户定义的表参数传递给动态 sql sp_executesql

转载 作者:行者123 更新时间:2023-12-04 14:33:25 25 4
gpt4 key购买 nike

我需要帮助将我的“用户定义表类型”参数传递给动态 sql sp_executesql。

这是我的示例代码:

DECLARE  @str as nvarchar(Max)
DECLARE @IDLIST AS ListBigintType /* this is my table type, with ItemId column (bigint)*/

INSERT INTO @IDLIST

SELECT DISTINCT bigintid FROM tableWithBigInts WITH(NOLOCK)


set @str ='select * from SomeTable where ID in (select ItemId from @IdTable) '

EXEC sp_executesql @str , @ParamDefs, @IdTable = @IDLIST

它说:必须声明表变量“@IdTable”

我无法让它工作,也无法通过合并(对于 bigint)获得解决方法,因为结果将超过 8000 个字符。

最佳答案

尝试设置 @ParamDefs到:

EXEC sp_executesql @str , N'@IdTable ListBigintType readonly', @IdTable = @IDLIST

这是一个完整的工作示例:
create type ListBigintType as table (ItemId bigint)
go
declare @t as ListBigintType
insert @t select 6*7

exec sp_executesql
N'select ItemId from @IdTable',
N'@IdTable ListBigintType readonly', @t

关于sql - 将用户定义的表参数传递给动态 sql sp_executesql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7999415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com