/****** Script for SelectTopNRows command from SSMS ******/ alter PROCEDURE QueryWeight @myResult NVARCHAR(MAX) OUTPUT AS BEGIN IF((SELECT COUNT(column_name) FROM myWeight WHERE fname = '曲宗雪' and 查看 = '1') < 2) BEGIN SET @myResult = (SELECT column_name FROM myWeight WHERE fname = '曲宗雪' and 查看 = '1') END ELSE BEGIN DECLARE myCursor CURSOR FOR ( select column_name from myweight where fname = '曲宗雪' and 查看 = '1' ) DECLARE @myItem NVARCHAR(MAX) DECLARE @myTemp NVARCHAR(256) open myCursor fetch next from myCursor into @myItem while(@@FETCH_STATUS = 0) begin FETCH NEXT FROM myCursor INTO @myTemp SET @myItem = @myItem + ',' + @myTemp SET @myTemp = '' end close myCursor DEALLOCATE myCursor set @myResult = @myItem END END DECLARE @theResult NVARCHAR(MAX) EXEC QueryWeight @theResult output declare @mySql nvarchar(max) set @mySql = 'select ' + @theResult + ' from MyTestView' set @mySql = REPLACE(@mySql,', from',' from') exec sp_executesql @mySql