CURSOR


DECLARE @ID nvarchar(MAX)
DECLARE @NAME nvarchar(MAX)

DECLARE @STR nvarchar(MAX)
SET @STR = ''

DECLARE cur CURSOR FOR--cur前面不加@
    SELECT TOP(10) USER_ACCT, NAME FROM ZZ_ACCOUNT

OPEN cur

FETCH Next from cur INTO @ID, @NAME
WHILE @@FETCH_STATUS = 0 --0代表沒有錯誤,1代表有錯誤
BEGIN
    SET @STR = @STR + '「' + @ID + ', ' + RTRIM(@NAME) + '」' + ', ';
    FETCH cur INTO @ID, @NAME
END
CLOSE cur
DEALLOCATE cur--丟掉CURSOR

SELECT REVERSE(SUBSTRING(REVERSE(@STR), 3, DATALENGTH(@STR))) AS '結果'

results matching ""

    No results matching ""