2014-05-01 91 views
0

我正在將我的應用程序從DB2數據庫遷移到sql server 2008,爲此我必須根據sql server替換一些查詢。更改DB2選擇查詢到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

如果你嘗試運行它,它會給你什麼樣的錯誤? – DaImTo

+0

它給出了以下錯誤。 '|'附近的語法不正確。 – Shrikant

+0

您可能想要'tracktime> ='2012-01-01 00:00:00',否則您將跳過午夜進來的任何內容。如果你想嘗試2年的數據,你實際上需要'<'2014-01-01'' - 你現在擁有的是2年和1天。 –

回答

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具體的事情是字符串連接運算符。我也從日期中刪除了時間組件。這兩個數據庫都不需要。

+0

其工作.. :)謝謝 – Shrikant