日期:2014-05-18  浏览次数:20524 次

MSSQL的一个问题
我现在有个语句
select   id   from   user
然后我如何用T-SQL语句获得每个ID值了
请高手解决,最好是把所以的ID相加成为一个字符串,或者是一个数组的形式.

------解决方案--------------------
用游标呀

DEALLOCATE myCursor


declare myCursor cursor for

select id from user

open myCursor

declare @IdList varchar(500)
declare @C varchar(700)
set @C = ' '
fetch next from myCursor

Into @IdList

while @@FETCH_STATUS = 0

begin

set @C = @C + @IdList + ', '

FETCH NEXT FROM myCursor
INTO @IdList

end

select @C
------解决方案--------------------
游标
DECLARE @RtnStr VARCHAR(8000)
DECLARE @TempStr VARCHAR(10)
set @RtnStr = ' '
set @TempStr = ' '

DECLARE My_Cursor CURSOR
FOR SELECT ID FROM [USER]

OPEN My_Cursor
FETCH NEXT FROM My_Cursor INTO @TempStr
IF @@FETCH_STATUS = 0
BEGIN
SET @RtnStr = @RtnStr +@TempStr + ', '
SET @TempStr = ' '
FETCH NEXT FROM My_Cursor INTO @TempStr
END