將資料表所有欄位組成SELECT句

CREATE PROCEDURE up_Select (
    @TABLE_NAME NVARCHAR(128) -- 資料表名稱
)AS
DECLARE @BUF NVARCHAR(1000)
DECLARE @COLUMN_NAME NVARCHAR(128)

SET @BUF=''

DECLARE curView CURSOR  FOR
    SELECT COLUMN_NAME
    FROM [INFORMATION_SCHEMA].[COLUMNS]
    WHERE TABLE_NAME=@TABLE_NAME

OPEN curView
FETCH curView
INTO @COLUMN_NAME

WHILE @@FETCH_STATUS = 0
    BEGIN -- WHILE
        SELECT @BUF=@BUF + @COLUMN_NAME + ','
        FETCH curView INTO @COLUMN_NAME
    END -- WHILE

CLOSE curView
DEALLOCATE curView

IF Len(@BUF)>0
BEGIN
    SELECT @BUF = LEFT(@BUF,LEN(@BUF)-1)
    PRINT 'SELECT'
    PRINT @BUF
    PRINT 'FROM ['+@TABLE_NAME+']'
END
ELSE
    PRINT '(none)'