6

「ユーザー定義のテーブルタイプ」パラメーターを動的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」を宣言しなければならない

結果が8000文字を超えるため、これを機能させることができず、coalesce(bigintsの場合)を回避することもできません。

4

1 に答える 1

8

次のように設定@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
于 2011-11-03T17:58:41.830 に答える