0

我在变量中使用表类型@temporaltable

FETCH MyCursor Into @temporaltable 

但是当我尝试进入 e 领域时,我得到了一个错误......

insert into T1 (t1field1,t1field2, t1field3) 
values(@temporaltable.field1, @temporaltable.field2, @temporaltable.field3)   

它无法识别@temporaltable,我得到了错误

必须声明标量变量@temporaltable

4

1 回答 1

0

这种语法应该会有所帮助,显然可以替代正确的数据类型、表名和选择语句。

DECLARE @myTable TABLE 
( 
    ... 
) 

DECLARE @var1 [DataType]
DECLARE @var2 [DataType]
DECLARE @var3 [DataType]

DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT ....
    FROM .....
OPEN myCursor

FETCH NEXT FROM myCursor INTO @var1, @var2, @var3

WHILE @@FETCH_STATUS = 0 BEGIN
    INSERT INTO T1 ([Col1], [Col2], [Col3]) VALUES (@var1, @var2, @var3)

    FETCH NEXT FROM myCursor INTO @var1, @var2, @var3
END

CLOSE myCursor
DEALLOCATE myCursor
于 2012-06-14T04:40:16.347 回答