限制在SQL视图中使用DECLARE

时间:2016-11-03 16:19:22

标签: sql view

我有一个我需要放入视图的SQL查询。我有它作为存储过程工作,但我的要求是它是一个视图。这意味着我无法像在存储过程中那样声明动态SQL。

 DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(t.Data_Term) 
                    from [UD_FldValues] t
                    inner join Surgery p
                        on t.Ud_Form_Id = p.Ud_Form_Id where t.Data_term!=''
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'');

set @query = 'SELECT Tran_Id,Section_Id,SubSection_Id,R_Id,Hospital_Id,Surgeon_Id,Procedure_ICD,Procedure_Details,DtSurgery_TimeIn,DtSurgery_TimeOut,Intra_oper_compl_Id, p.Discharge_Date,Discharge_Status_Id,Entered_By,Entered_Date,p.Status,p.Ud_Form_Id,p.Complication_ICD,p.Complication_Name, Procedure_Name, ' + @cols + ' from 
             (
                 select t.Tran_Id
                      , t.[R_Id]
                      , p.Ud_Form_Id
                      ,p.Procedure_Name
                      ,p.Section_Id
                      ,p.SubSection_Id
                      ,p.Hospital_Id
                      ,p.Surgeon_Id
                      ,p.Procedure_ICD
                      ,p.Procedure_Details
                      ,p.DtSurgery_TimeIn
                      ,p.DtSurgery_TimeOut
                      ,p.Intra_oper_compl_Id
                      ,p.Discharge_Date
                      ,p.Discharge_Status_Id
                      ,p.Entered_By,
                      p.Entered_Date
                      ,p.Status
                      ,p.Complication_ICD
                      ,p.[Complication_Name]
                      , case when (t.Data_Type=''Text'') THEN t.Text_Value Else Convert(varchar(MAx),Num_Value) END as txtvl
                      ,t.Data_term
                    from Surgery p 
                    left outer join [UD_FldValues] t
                        on t.Ud_Form_Id = p.Ud_Form_Id and t.Tran_Id=p.Surgery_Id
                        left outer join UD_Formmap fm ON fm.Ud_Form_Id = p.Ud_Form_Id 
                    where fm.module_id = 1

            ) x
            pivot 
            (
                min(txtvl)
                for Data_Term in (' + @cols + ')
            ) p '

exec(@query)

请帮我找一个替代方法,但不是存储程序。我需要它在视野中

1 个答案:

答案 0 :(得分:0)

对于一个视图,它需要是一个单独的select语句,

@cols语句返回列列表,在主select语句中使用两次。所以用填充它的查询替换主查询中的@cols应该可行。

设置@query然后exec(@query)也不是必需的,所以你只需要整理并确保正确数量的括号到位。

它应该看起来像这样。

 SELECT Tran_Id
,Section_Id
,SubSection_Id
,R_Id,Hospital_Id
,Surgeon_Id,Procedure_ICD
,Procedure_Details
,DtSurgery_TimeIn
,DtSurgery_TimeOut
,Intra_oper_compl_Id
,p.Discharge_Date
,Discharge_Status_Id
,Entered_By,Entered_Date
,p.Status,p.Ud_Form_Id
,p.Complication_ICD
,p.Complication_Name
,Procedure_Name,
 (select STUFF((SELECT distinct ',' + QUOTENAME(t.Data_Term) 
                      from [UD_FldValues] t
                      inner join Surgery p
                      on t.Ud_Form_Id = p.Ud_Form_Id where t.Data_term!=''
                      FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)') 
                    ,1,1,'');))
 from 
                     (
                    select t.Tran_Id
                          , t.[R_Id]
                          , p.Ud_Form_Id
                          ,p.Procedure_Name
                          ,p.Section_Id
                          ,p.SubSection_Id
                          ,p.Hospital_Id
                          ,p.Surgeon_Id
                          ,p.Procedure_ICD
                          ,p.Procedure_Details
                          ,p.DtSurgery_TimeIn
                          ,p.DtSurgery_TimeOut
                          ,p.Intra_oper_compl_Id
                          ,p.Discharge_Date
                          ,p.Discharge_Status_Id
                          ,p.Entered_By,
                          p.Entered_Date
                          ,p.Status
                          ,p.Complication_ICD
                          ,p.[Complication_Name]
                          , case when (t.Data_Type=''Text'') THEN t.Text_Value Else Convert(varchar(MAx),Num_Value) END as txtvl
                          ,t.Data_term
                        from Surgery p 
                        left outer join [UD_FldValues] t
                            on t.Ud_Form_Id = p.Ud_Form_Id and t.Tran_Id=p.Surgery_Id
                            left outer join UD_Formmap fm ON fm.Ud_Form_Id = p.Ud_Form_Id 
                        where fm.module_id = 1

                ) x
                pivot 
                (
                    min(txtvl)
                        for Data_Term in ( 
select STUFF((SELECT distinct ',' + QUOTENAME(t.Data_Term) 
                            from [UD_FldValues] t
                            inner join Surgery p
                                on t.Ud_Form_Id = p.Ud_Form_Id where t.Data_term!=''
                    FOR XML PATH(''), TYPE
                    ).value('.', 'NVARCHAR(MAX)') 
                ,1,1,'');)

 ) p