开发者

Subquery as table name in INSERT INTO statement

I'm in a situation where I'm dynamically creating tables. I'm basically wondering if there is a way to use the result of a sub query as the table in a INSERT INTO statement.

I have a working solution that uses dynamic SQL and the table name, but for more complex inserts that could get very messy.

开发者_运维百科Here's an example of what I mean.

INSERT INTO (SELECT name 
             FROM sys.objects 
             WHERE object_id = 914102297)


you can do something like this:


DECLARE @Query nvarchar(4000)


set @Query = 'INSERT INTO ' + (SELECT name FROM sys.objects WHERE object_id = 914102297)
set @Query = @Query + ...
exec sp_executesql @query


0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜