按顺序执行两个参数

时间:2015-05-07 18:57:03

标签: sql-server sql-server-2012

我的部分查询看起来像这样,

LEFT JOIN (
SELECT 
*
FROM
    (select L.LOG_ID
    ,L.PAT_ID
    ,t.PANEL_START_TIME
    ,'Row' = ROW_NUMBER() over (partition by l.pat_id order by panel_start_time)
    from or_log l
    inner join PATIENT p ON p.PAT_ID = l.PAT_ID
    left join OR_LOG_PANEL_TIME1 t on t.LOG_ID = l.LOG_ID and t.PANEL_TIME_EVENT_C = x
    where p.PAT_MRN_ID = @PatMRN AND l.surgery_date = @SurgDate                                 
    AND l.CASE_CLASS_C IN ('xx', 'xx')
    )a

如果我有两个参数,并且每个参数都有多个值,

@MRN = 1, 2, 3
@SurgDate = 2012, 2013, 2014

我有什么方法可以按顺序传递参数进行查询?

1 2012 first parameter            
2 2013 2nd                          
3 2014 3rd

然后生成以下输出

PatMRN   SurgDate     ORD_NUM_VALUE
  1          2012         3
  2          2013         3
  3          2014         3

1 个答案:

答案 0 :(得分:1)

假设您有一个表变量:

declare @t table(mrn int, surg int)
insert into @t values
(1,2012),
(2,2012),
(3,2014)

现在你可以重写你的陈述:

LEFT JOIN (
SELECT 
*
FROM
    (select L.LOG_ID
    ,L.PAT_ID
    ,t.PANEL_START_TIME
    ,'Row' = ROW_NUMBER() over (partition by l.pat_id order by panel_start_time)
    from or_log l
    inner join PATIENT p ON p.PAT_ID = l.PAT_ID
    left join OR_LOG_PANEL_TIME1 t on t.LOG_ID = l.LOG_ID and t.PANEL_TIME_EVENT_C = x
    join @t t on t.mrn = p.PAT_MRN_ID AND t.surg = l.surgery_date
    Where l.CASE_CLASS_C IN ('xx', 'xx')
    )a

现在您想知道可以创建自定义表值类型:

CREATE TYPE MRNType AS TABLE 
    ( mrn int,
      surg int);
GO

并在proc中使用它,例如:

Create someProc
@t MTNType
AS
....
your statement
....

并将此proc用作:

declare @t MRNType

insert into @t values
(1,2012),
(2,2012),
(3,2014)

Exec someProc @t = @t