将DB2 Select查询更改为sql Server查询

时间:2014-05-01 13:25:14

标签: sql sql-server db2-400

我正在将我的应用程序从DB2数据库迁移到sql server 2008,为此我必须根据sql server替换一些查询。

我的查询是:

SELECT '' 
       || table1.openid AS OPENID, 
       table2.mfg_part_no, 
       table2.description, 
       binnumber, 
       storageid, 
       tracktime, 
       table1.userid 
FROM   table1 
       LEFT JOIN table2 
              ON table2.openid = table1.openid 
       LEFT JOIN table3 
              ON table3.locationid = table2.locationid 
WHERE  value = '14' 
       AND field = 'STATUSID' 
       AND tracktime > '2012-01-01 00:00:00.00' 
       AND tracktime < '2014-01-02 00:00:00.00' 

如何在sql server中编写此查询?

1 个答案:

答案 0 :(得分:1)

我认为这样做会:

SELECT cast(table1.openid as varchar(255)) AS OPENID, 
       table2.mfg_part_no, 
       table2.description, 
       binnumber, 
       storageid, 
       tracktime, 
       table1.userid 
FROM   table1 
       LEFT JOIN table2 
              ON table2.openid = table1.openid 
       LEFT JOIN table3 
              ON table3.locationid = table2.locationid 
WHERE  value = '14' 
       AND field = 'STATUSID' 
       AND tracktime > '2012-01-01' 
       AND tracktime < '2014-01-02' ;

我看到的唯一DB2特定的东西是字符串连接运算符。我还从日期中删除了时间组件。任何一个数据库都不应该这样做。