declare @DJBH varchar(50) declare @DM2 varchar(50) declare @SQLText varchar(3000) declare myCursor cursor for select top 10 DJBH from SPJHD ORDER BY DJBH open myCursor fetch next from myCursor into @DJBH while @@fetch_status=0 begin print 'CategoryId='+cast(@DJBH as varchar(20)) Set @SQLText = 'Select DM2 From SPJHD Where DJBH=' + ''''+@DJBH+'''' print @SQLText fetch next from myCursor into @DJBH end close myCursor deallocate myCursor SELECT * FROM SPJHD