SQL 游标使用





 
 --  DROP TABLE #tb_name


CREATE TABLE #tb_name(
                       t_name VARCHAR(20)


                        ) 




INSERT INTO #tb_name


SELECT TOP 1   name   FROM SysObjects Where XType='U' order BY Name 




 declare s_sursor cursor local fast_forward for     
 
 SELECT t_name FROM #tb_name


  open s_sursor


   declare @c NVARCHAR(50),
           @tablename NVARCHAR(50),
           @strsql NVARCHAR(500)
   


    fetch next from s_sursor  INTO @c
 WHILE @@FETCH_STATUS=0
 BEGIN
  print @c
    SET @tablename=@c
   SET @strsql='select  tablename=  ''' + @tablename+ ''' , cid , cdt from  '+@tablename
  EXEC(@strsql)
  print @strsql
 fetch next from s_sursor  INTO @c
 
end
  close s_sursor                                
 deallocate s_sursor       













猜你喜欢

转载自blog.csdn.net/suheonline/article/details/25966877