开发者

T-SQL copying a table variable

I'm trying to make a copy of a table variable:

DECLARE @lt_Sections TABLE
(
  teamId SMALLINT NOT NULL
)

DECLARE @lt_tempSections T开发者_如何转开发ABLE
(
  teamId SMALLINT NOT NULL
)

-- populate some values in @lt_Sections
-- take a copy of @lt_Sections

SET @lt_tempSections = @lt_Sections

This is giving me an error:

Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@lt_Sections".

What have I done wrong??

Thanks, Mark


Set (or select) can be applied only to a scalar variable not a Table variable.

Instead of setting values you should use Insert

DECLARE @lt_Sections TABLE 
    ( 
      teamId SMALLINT NOT NULL 
    ) 

DECLARE @lt_tempSections TABLE 
( 
  teamId SMALLINT NOT NULL 
) 

insert @lt_TempSections(teamId)
select teamId from @lt_sections


You can't copy table variables like this (think of them more as you would a real/temporary table).

You'd need to:

INSERT @lt_tempSections (teamId)
SELECT teamId FROM @lt_Sections
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜