权限管理系统

/****** 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

 

posted @ 2014-03-30 17:40  美丽的矩阵  阅读(315)  评论(0编辑  收藏  举报