SQL SERVER 建临时表、循环插入数据、游标遍历数据库

xiaoxiao2021-02-28  99

create table #tmp (id int)  --建立临时数据表    declare @x int  --循环插入数据  set @x=1  while @x<=10  begin      insert into #tmp values(@x)      set @x=@x+1  end    --建立游标 遍历数据库  declare tmpCursor CURSOR for    select * from #tmp  open tmpCursor    declare @id int  fetch next from tmpCursor into @id    while  @@FETCH_STATUS =0  begin      print @id      fetch next from tmpCursor into @id  end create  procedure  findName --创建一个名为findName的存储过程   AS   declare  @result  VARCHAR (30) --用来处理结果的变量     begin     --声明一个游标      Declare  curStudentFee  Cursor  for       SELECT  NAME  FROM  SYSOBJECTS  WHERE  XTYPE= 'P' ; ---查询语句(查询所有用户存储过程名称)             --打开游标      Open  curStudentFee       --循环并提取记录      Fetch  Next  From  curStudentFee  Into  @result --取第一条记录存入@result中      While ( @@Fetch_Status=0 )               begin            print  '' '' +@result+ '' '' + ',' ; ---处理结果         Fetch  Next  From  curStudentFee  into  @result ----下一条           end       --关闭游标          Close  curStudentFee    --释放游标     Deallocate  curStudentFee      end  
转载请注明原文地址: https://www.6miu.com/read-35780.html

最新回复(0)