将查询从Oracle PL / SQL转换为Microsoft T-SQL

时间:2012-01-30 21:58:19

标签: sql tsql plsql

更新:感谢您的帮助!我只需要花一点时间来查看结果,我一定会给出答案。非常感谢大家的反馈!

我有以下查询需要从Oracel PL / SQL转换为T-SQL以适应数据源更改。我已经完成了隔离逻辑所需的工作(仍然用PL / SQL编写),只需要调整其余部分。像TO_DATE这样的东西特别难以转换,所以我决定转向StackOverflow。

我已经听说过SwisSQL工具,但由于这只是一个单独查询的孤立实例,需要使用该产品进行转换。任何和所有帮助转换查询以使用适当的T-SQL synthax将不胜感激。感谢您的时间,这是有问题的查询:

 SELECT
            F.TYPE_ID,
            TRIM(f.event_type_name),
            TRIM(e.event_name),
            NVL(trim(e.event_title),' '),
            e.cur_event_state,
            TO_CHAR(D.EV_START_DT, 'YYYYMMDD') ,
            TO_CHAR(D.EV_START_DT,'HH24MI') ,
            TO_CHAR(D.EV_END_DT, 'YYYYMMDD') ,
            TO_CHAR(D.EV_END_DT,'HH24MI') ,
            TO_char(d.EV_START_DT, 'D')
    from rooms C,
         SP_RESERVATIONS D,
         EVENTS E,
         event_types f
    where @Room = TRIM(replace(C.room_short(+),'-','*'))
AND C.ROOM_ID = D.ROOM_ID
      AND D.EVENT_ID = E.EVENT_ID
      and e.event_type_id = f.type_id
      and f.type_id in ('22','40','70','71','72','105','121','119')
      AND (D.EV_START_DT
                     BETWEEN TO_DATE(:WS-TERM-START-DATE,'YYYYMMDD')
                        AND TO_DATE(:WS-TERM-END-DATE,'YYYYMMDD')
       OR  D.EV_END_DT
                     BETWEEN TO_DATE(:WS-TERM-START-DATE,'YYYYMMDD')
                        AND TO_DATE(:WS-TERM-END-DATE,'YYYYMMDD'))
      and not e.cur_event_state = '59'

谢谢!

3 个答案:

答案 0 :(得分:2)

以下是作为SQL小提琴程序的Oracle代码:http://sqlfiddle.com/#!4/80d20/4

以下是SQL Server的修改版本:http://sqlfiddle.com/#!3/5fe1b/2

SELECT
            F.TYPE_ID,
            RTRIM(LTRIM(f.event_type_name)),
            RTRIM(LTRIM(e.event_name)),
            COALESCE(RTRIM(ltrim(e.event_title)),' '),
            e.cur_event_state,
            convert(varchar, D.EV_START_DT, 112) ,
            datepart(hh, D.EV_START_DT) ,
            convert(varchar, D.EV_END_DT, 112) ,
            datepart(hh, D.EV_END_DT) ,
            datepart(d, d.EV_START_DT)
    from rooms C,
         SP_RESERVATIONS D,
         EVENTS E,
         event_types f
    where 'Big Room' = RTRIM(LTRIM(replace(C.room_short,'-','*')))
AND C.ROOM_ID = D.ROOM_ID
      AND D.EVENT_ID = E.EVENT_ID
      and e.event_type_id = f.type_id
      and f.type_id in ('22','40','70','71','72','105','121','119')
      AND (D.EV_START_DT
                     BETWEEN '2012-01-01'
                        AND '2012-02-01'
       OR  D.EV_END_DT
                     BETWEEN '2012-01-01'
                        AND '2012-02-01')
      and not e.cur_event_state = '59'

编辑添加rtrim与ltrim

答案 1 :(得分:1)

好的,试试这个查询:

SELECT f.type_id, 
       LTRIM(RTRIM(f.event_type_name)), 
       LTRIM(RTRIM(e.event_name)), 
       ISNULL(LTRIM(RTRIM(e.event_title)), ' '), 
       e.cur_event_state, 
       CONVERT(VARCHAR(8), d.ev_start_dt, 112), 
       CONVERT(VARCHAR(5), d.ev_start_dt, 114), 
       CONVERT(VARCHAR(8), d.ev_end_dt, 112), 
       CONVERT(VARCHAR(5), d.ev_end_dt, 114), 
       DATENAME(DAY, d.ev_start_dt) 
FROM   rooms c 
       INNER JOIN sp_reservations d 
         ON c.room_id = d.room_id 
       INNER JOIN [EVENTS] e 
         ON d.event_id = e.event_id 
       INNER JOIN event_types f 
         ON e.event_type_id = f.[type_id] 
WHERE  @Room = RTRIM(LTRIM(REPLACE(c.room_short, '-', '*'))) 
       AND f.[type_id] IN ( '22', '40', '70', '71', 
                            '72', '105', '121', '119' ) 
       AND ( d.ev_start_dt BETWEEN CONVERT(DATETIME, [WS-TERM-START-DATE], 112) 
                                   AND 
                                         CONVERT(DATETIME, [WS-TERM-END-DATE], 
                                         112) 
              OR d.ev_end_dt BETWEEN CONVERT(DATETIME, [WS-TERM-START-DATE], 112 
                                     ) AND 
                                         CONVERT(DATETIME, [WS-TERM-END-DATE], 
                                         112) ) 
       AND NOT e.cur_event_state = '59' 

答案 2 :(得分:0)

这是我的答案。如果它不起作用,请告诉我。

SELECT
   f.type_id                                                    ,
   RTRIM(LTRIM(f.event_type_name))                              ,
   RTRIM(LTRIM(e.event_name     ))                              ,
   ISNULL(RTRIM(LTRIM(e.event_title)),' ')                      ,
   e.cur_event_state                                            ,
   CONVERT(VARCHAR(8),d.ev_start_dt , 112)                      ,      -- 'YYYYMMDD'
   CONVERT(VARCHAR(2),d.ev_start_dt , 114) +
   SUBSTRING(CONVERT(VARCHAR(12),d.ev_start_dt , 114),4,2)      ,      -- 'HH24MI'  
   CONVERT(VARCHAR(8),d.ev_end_dt   , 112)                      ,      -- 'YYYYMMDD'
   CONVERT(VARCHAR(2),d.ev_end_dt   , 114) +    
   SUBSTRING(CONVERT(VARCHAR(12),d.ev_end_dt   , 114),4,2)      ,      -- 'HH24MI'  
   DATENAME(DAY, d.ev_start_dt)                                        -- 'D'       
FROM 
   rooms c
      INNER JOIN sp_reservations d
      ON c.room_id = d.room_id
      INNER JOIN events e
      ON d.event_id = e.event_id
      INNER JOIN event_types f
      ON e.event_type_id = f.type_id
WHERE 
   --@Room = RTRIM(LTRIM(REPLACE(c.room_short(+),'-','*'))) and 
   --variable = LOJ table.column above is not a real filter
   f.type_id IN ('22','40','70','71','72','105','121','119')
   and ( d.ev_start_dt
            BETWEEN CONVERT(datetime,[WS-TERM-START-DATE],112)
                AND CONVERT(datetime,[WS-TERM-END-DATE]  ,112)
         or d.ev_end_dt
            BETWEEN CONVERT(datetime,[WS-TERM-START-DATE],112)
                AND CONVERT(datetime,[WS-TERM-END-DATE]  ,112) )
   and e.cur_event_state != '59';
GO