2011-11-03 3 views
5

「ユーザ定義テーブルタイプ」パラメータを動的SQL、sp_executesqlに渡す際に助けが必要です。ここでユーザ定義のテーブルパラメータを動的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」

私はこの仕事を得ることができない、と合体して回避策を得ることができません(bigintsの場合)結果が8000文字を超えるためです。

答えて

7

はに@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 
+0

、私はここに質問を書いているときことを書くのを忘れていました。クエリーには既にそれがあります。同じエラーが発生します。 –

+0

実例を追加しました。 'select name、compatibility_level from sys.databases'からデータベースの互換性レベルをチェックしますか? SQL Server 2005以降の場合は、90以上に設定する必要があります。 – Andomar

+0

私が掲載した例はインストール時にうまく動作しますので、 'sp_executesql'を使ってテーブル値のパラメータを渡すことができます。 – Andomar

関連する問題