在我的产品表中,我有14个产品,并且对于每个产品,我需要在美国的每个州都有一行。我想通过以下来实现:嵌套SQL游标
begin tran
declare @state varchar(2),
@productId int
declare stateCursor CURSOR FOR
select distinct [State]
from lookUpAreaFactor
declare productCursor CURSOR FOR
select distinct productid
from Product
open stateCursor
open productCursor
FETCH NEXT from stateCursor into @state
fetch next from productCursor into @productId
while @@FETCH_STATUS = 0
BEGIN
while @@FETCH_STATUS = 0
BEGIN
insert into ProductToState (ProductID,[State]) values (@productId,@state)
fetch next from productCursor into @productId
END
fetch next from stateCursor into @state
END
close stateCursor
close productCursor
select * from producttostate
rollback
它的第一个产品后,轰炸了 - 但它甚至没有插入行的所有50个州。我可以用另一双眼睛 - 我在这里做错了什么?
你使用哪个DBMS? –
我正在使用MSSMS – reds184
MSSQL?哪个版本? –