开发者

Passing table variable into dynamic SQL 2008

开发者 https://www.devze.com 2023-01-10 18:24 出处:网络
I need to run a dynamic sql which uses table variable created in the scope of the parent. How do I pass table variable into 开发者_如何转开发dynamic sql in SQL2008 ?Here\'s an end-end example:

I need to run a dynamic sql which uses table variable created in the scope of the parent. How do I pass table variable into 开发者_如何转开发dynamic sql in SQL2008 ?


Here's an end-end example:

-- Define a custom TABLE type
CREATE TYPE IntegerTableType AS TABLE (ID INTEGER);

-- Fill a var of that type with some test data
DECLARE @MyTable IntegerTableType
INSERT @MyTable VALUES (1),(2),(3)

-- Now this is how you pass that var into dynamic statement
EXECUTE sp_executesql N'SELECT * FROM @MyTable', 
    N'@MyTable IntegerTableType READONLY', 
    @MyTable
0

精彩评论

暂无评论...
验证码 换一张
取 消