SQL SERVER 动态游标
CREATE TABLE [dbo].[mytable1] (
[Id] [int] IDENTITY (1, 1) NOT NULL ,
[content] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[date] [datetime] NULL
) ON [PRIMARY]
[Id] [int] IDENTITY (1, 1) NOT NULL ,
[content] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[date] [datetime] NULL
) ON [PRIMARY]
declare @sql varchar(500)
declare @strCursor varchar(500)
set @sql ='select id,content from mytable1'
set @strCursor ='declare table_cursor cursor scroll dynamic for '+@sql
--scroll表示可以向前向后
--dynamic表示可以可读可写
exec (@strCursor)
open table_cursor
declare @i varchar(50)
declare @content varchar(50)
fetch next from table_cursor into @i,@content
while(@@fetch_status=0)
begin
if @i < 10
begin
print 'id: ' + @i
print '内容: ' + @content
end
fetch next from table_cursor into @i,@content
end
close table_cursor
deallocate table_cursor