2013-10-27 44 views
0

而不是鍵入'NXT_US.dbo。'一遍又一遍,我還能怎麼做?SQL將數據庫添加到所有表引用

SELECT DISTINCT 
         NXT_US.dbo.icsp.prod, NXT_US.dbo.icsp.descrip##1, NXT_US.dbo.icsp.descrip##2, NXT_US.dbo.icsw.whse, NXT_US.dbo.sastn.descrip AS branch, NXT_US.dbo.icsw.qtyonhand, NXT_US.dbo.icsp.unitstock AS UOM, NXT_US.dbo.apsv.vendno AS VendorNumber, 
         NXT_US.dbo.apsv.name AS VendorName, NXT_US.dbo.icsp.prodcat, NXT_US.dbo.sasta.descrip, NXT_US.dbo.icsw.prodline, NXT_US.dbo.icsw.statustype, NXT_US.dbo.icsw.lastinvdt, NXT_US.dbo.oeel.invoicedt, NXT_US.dbo.icsw.avgcost, NXT_US.dbo.oeel.vendno, 
         NXT_US.dbo.icsw.avgcost * NXT_US.dbo.icsw.qtyonhand AS ExtCost, NXT_US.dbo.oeel.prodcost, NXT_US.dbo.oeel.orderno 
INTO InventoryView 
FROM   NXT_US.dbo.sasta INNER JOIN 
         NXT_US.dbo.icsp ON NXT_US.dbo.sasta.codeval = NXT_US.dbo.icsp.prodcat LEFT OUTER JOIN 
         NXT_US.dbo.icswu ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icswu.prod LEFT OUTER JOIN 
         NXT_US.dbo.icsw LEFT OUTER JOIN 
         NXT_US.dbo.oeel ON NXT_US.dbo.icsw.lastinvdt = NXT_US.dbo.oeel.invoicedt AND NXT_US.dbo.icsw.whse = NXT_US.dbo.oeel.whse AND NXT_US.dbo.icsw.prod = NXT_US.dbo.oeel.shipprod RIGHT OUTER JOIN 
         NXT_US.dbo.sastn RIGHT OUTER JOIN 
         NXT_US.dbo.icsd ON NXT_US.dbo.sastn.cono = NXT_US.dbo.icsd.cono AND NXT_US.dbo.sastn.codeval = NXT_US.dbo.icsd.divno ON NXT_US.dbo.icsw.whse = NXT_US.dbo.icsd.whse ON NXT_US.dbo.icsp.prod = NXT_US.dbo.icsw.prod LEFT OUTER JOIN 
         NXT_US.dbo.apsv ON NXT_US.dbo.icsw.arpvendno = NXT_US.dbo.apsv.vendno 
WHERE  (NXT_US.dbo.sasta.cono = '1') AND (NXT_US.dbo.sasta.codeiden = 'c') AND (NXT_US.dbo.icsw.statustype = 's') AND (NXT_US.dbo.icsp.cono = '1') AND (NXT_US.dbo.sastn.codeiden = 'V') and NXT_US.dbo.icsw.whse = 'G010' 
+3

一旦表被引用,那麼你可以通過表名引用它。此外,您可能需要閱讀[別名](http://technet.microsoft.com/en-us/library/ms187455(v = SQL.105).aspx)。 –

+0

所以NXT_US是數據庫,dbo是模式。如果所有表都在相同的數據庫和相同的模式中,那麼您希望將此DB和模式作爲前綴添加到所有表名的原因是什麼?使用完全合格的對象名稱是一種很好的做法,但如果只是您公司的開發實踐,則必須這樣做。 –

回答

0

在您的FROM子句中第一次使用它時爲每個表添加別名。然後你可以在你的SELECT,WHERE,GROUP BY子句中使用別名。這裏是示例(但使用比我放在這裏的通用更好的別名):

SELECT DISTINCT 
         t2.prod, t2.descrip##1, t2.descrip##2, t4.whse, t6.descrip AS branch, t4.qtyonhand, t2.unitstock AS UOM, t8.vendno AS VendorNumber, 
         t8.name AS VendorName, t2.prodcat, t1.descrip, t4.prodline, t4.statustype, t4.lastinvdt, t5.invoicedt, t4.avgcost, t5.vendno, 
         t4.avgcost * t4.qtyonhand AS ExtCost, t5.prodcost, t5.orderno 
INTO InventoryView 
FROM   NXT_US.dbo.sasta AS t1 INNER JOIN 
         NXT_US.dbo.icsp AS t2 ON t1.codeval = t2.prodcat LEFT OUTER JOIN 
         NXT_US.dbo.icswu AS t3 ON t2.prod = t3.prod LEFT OUTER JOIN 
         NXT_US.dbo.icsw AS t4 LEFT OUTER JOIN 
         NXT_US.dbo.oeel AS t5 ON t4.lastinvdt = t5.invoicedt AND t4.whse = t5.whse AND t4.prod = t5.shipprod RIGHT OUTER JOIN 
         NXT_US.dbo.sastn AS t6 RIGHT OUTER JOIN 
         NXT_US.dbo.icsd AS t7 ON t6.cono = t7.cono AND t6.codeval = t7.divno ON t4.whse = t7.whse ON t2.prod = t4.prod LEFT OUTER JOIN 
         NXT_US.dbo.apsv AS t8 ON t4.arpvendno = t8.vendno 
WHERE  (t1.cono = '1') AND (t1.codeiden = 'c') AND (t4.statustype = 's') AND (t2.cono = '1') AND (t6.codeiden = 'V') and t4.whse = 'G010' 
相關問題