优化在T-SQL中为本地变量赋值

时间:2012-06-27 12:24:13

标签: sql-server tsql

有没有办法合并以下内容并在@FH_RecordKey@VF_dept_seq声明中设置SETSELECT

    set @FH_RecordKey =(
        case @lnktble
            when 'ems' then (select FH_inci_id from FH_Catherine_live.dbo.FH_MAP_EMS_INCI where VF_ems_seq = @tempseq)
            when 'imaster' then (select FH_insp_id from FH_Catherine_live.dbo.FH_MAP_INSPID where VF_in_seq = @tempseq)
            when 'n5basic' then (select inci_id from [dbo].[VF_IncWorkTable] where inc_seq = @tempseq)
        END)


    set @VF_dept_seq = (
        case @lnktble
            when 'ems' then (select VF_dept_seq from FH_Catherine_Live.dbo.FH_MAP_EMS_INCI where VF_ems_seq = @tempseq)
            when 'imaster' then (select VF_dept_seq from FH_Catherine_Live.dbo.FH_MAP_INSPID where VF_in_seq = @tempseq)
            when 'n5basic' then (select dept_seq from [dbo].[VF_IncWorkTable] where inc_seq = @tempseq)
        end)

2 个答案:

答案 0 :(得分:1)

使用IF语句而不是CASE

IF @lnktble = 'ems'
select  @FH_RecordKey = FH_inci_id ,@VF_dept_seq = VF_dept_seq
from FH_Catherine_live.dbo.FH_MAP_EMS_INCI 
where VF_ems_seq = @tempseq
ELSE IF @lnktble = 'imaster' /*...*/

答案 1 :(得分:0)

Select @FH_RecordKey = 
  (case statement here),
@VF_dept_seq = 
  (case statement here)