循環中添加使用的sqlserver光標增加值
CREATE TABLE [dbo].[Table1](
[PID] [int] NULL,
[ProductDesc] [nvarchar](50) NULL,
[ProductCode] [nvarchar](10) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Table1] ([PID], [ProductDesc], [ProductCode], [Person_Id_Helper]) VALUES (1, N'Packet-Eye', N'P001', NULL)
GO
INSERT [dbo].[Table1] ([PID], [ProductDesc], [ProductCode], [Person_Id_Helper]) VALUES (2, N'Wiggy', N'W099 ', NULL)
GO
INSERT [dbo].[Table1] ([PID], [ProductDesc], [ProductCode], [Person_Id_Helper]) VALUES (3, N'Wimax-Lite', N'W001', NULL)
GO
INSERT [dbo].[Table1] ([PID], [ProductDesc], [ProductCode], [Person_Id_Helper]) VALUES (4, N'Wimax-Home', N'e W002 ', NULL)
GO
Declare @count int
DECLARE @PID int
set @count=0
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT [PID] FROM [Table1]
OPEN c1
FETCH NEXT FROM c1 INTO @PID
WHILE @@FETCH_STATUS = 0
BEGIN
set @[email protected]+1
update Table1 set [email protected] where [PID][email protected]
FETCH NEXT FROM c1 INTO @PID END
CLOSE c1
DEALLOCATE c1
select * from Table1
@PeterSmith NO!動態SQL **主要**不好,CURSOR ** **非常糟糕。動態PIVOT或與性能相關的特殊情況需要動態SQL。有**非常罕見的事情,你真的需要一個光標來解決它... – Shnugo
這就是爲什麼我upvoted你的答案。我刪除了我的評論!謝謝 –