使用Cursor将TABLEA中的值复制到TABLEB

时间:2014-05-23 09:01:10

标签: sql sybase

我想使用游标将TableA中的值复制到TableB,但总是遇到麻烦。这是我的代码:

DROP TABLE #Firmy
DROP TABLE #Faktury
CREATE TABLE #Firmy (
                idfirmy VARCHAR(255)
)

INSERT INTO #Firmy VALUES ('00001936')
INSERT INTO #Firmy VALUES ('00002059')
CREATE TABLE #Faktury (
                idfirmy VARCHAR(255)--,
                --idfaktury VARCHAR(255)
)
DECLARE fakturyc CURSOR FOR SELECT * FROM #Firmy 
FOR loop1 AS fakturyc
DO 
    INSERT INTO #Faktury VALUES fakturyc 
END FOR

获取" loop1"附近的语法错误。有人可以帮我解决这个问题吗?谢谢:))

1 个答案:

答案 0 :(得分:1)

这实际上是T-sql:但也可以在sybase中使用

DROP TABLE #Firmy
DROP TABLE #Faktury
CREATE TABLE #Firmy (
                idfirmy VARCHAR(255)
)

INSERT INTO #Firmy VALUES ('00001936')
INSERT INTO #Firmy VALUES ('00002059')
CREATE TABLE #Faktury (
                idfirmy VARCHAR(255)--,
                --idfaktury VARCHAR(255)
)

declare @col1 varchar(10)
declare copy cursor for
select idfirmy from #Firmy -- select top 1 * from #Firmy

open copy 
fetch next from copy into @col1

while @@FETCH_STATUS=0
begin
insert into #Faktury(idfirmy) values(@col1)
fetch next from copy into @col1
end
close copy
deallocate copy

select * from #Faktury
select * from #Firmy

simplified Query将是

insert into #Faktury
select * from #Firmy