从查询结果插入表中

时间:2015-02-23 14:45:10

标签: sql sql-server

我有一个查询从表中提取数据,然后我需要将结果放入另一个数据库中的另一个表中。这是我获取数据的查询。

with
    BARCODEINNER(name, CODE, BARCODE_INNER) as
                (select
                    STOCKITEM.NAME,
                    STOCKITEM.CODE,
                    left(STOCKITEMPRICE.PRICE, 12) as EXPR1
                from [ PENNINE 2009 ] .DBO.PRICEBAND
                inner join [ PENNINE 2009 ] .DBO.STOCKITEMPRICE on PRICEBAND.PRICEBANDID = STOCKITEMPRICE.PRICEBANDID
                inner join [ PENNINE 2009 ] .DBO.STOCKITEM on STOCKITEMPRICE.ITEMID = STOCKITEM.ITEMID
                where (PRICEBAND.PRICEBANDID = '5045261')),
    BARCODEOUTER(CODE, BARCODE_OUTER) as
                (select
                    STOCKITEM_1.CODE,
                    left(STOCKITEMPRICE_1.PRICE, 12) as EXPR1
                from [ PENNINE 2009 ] .DBO.PRICEBAND as PRICEBAND_1
                inner join [ PENNINE 2009 ] .DBO.STOCKITEMPRICE as STOCKITEMPRICE_1 on PRICEBAND_1.PRICEBANDID = STOCKITEMPRICE_1.PRICEBANDID
                inner join [ PENNINE 2009 ] .DBO.STOCKITEM as STOCKITEM_1 on STOCKITEMPRICE_1.ITEMID = STOCKITEM_1.ITEMID
                where (PRICEBAND_1.PRICEBANDID = '5048507'))
select
    BARCODEINNER.CODE          as PRODUCTCODE,
    BARCODEINNER.NAME          as DESCRIPTION,
    BARCODEINNER.BARCODE_INNER as BARCODESINGLE,
    BARCODEOUTER.BARCODE_OUTER as BARCODEPACK
from BARCODEINNER as BARCODEINNER
inner join BARCODEOUTER as BARCODEOUTER on BARCODEINNER.CODE = BARCODEOUTER.CODE
order by BARCODEINNER.CODE

所以我想从此查询中获取结果集,然后将其放入此数据库

INSERT INTO [Label Software].dbo.[Label Information]

我怎样才能实现这个目标?

3 个答案:

答案 0 :(得分:1)

两个数据库是否在同一台服务器上?

如果是这样,你几乎回答了自己的问题......

你试过这个吗?

WITH BarCodeInner(Name, Code, Barcode_Inner) AS
(SELECT StockItem.Name,
      StockItem.Code,
      LEFT(StockItemPrice.Price, 12) AS Expr1
FROM [Pennine 2009].dbo.PriceBand
INNER JOIN [Pennine 2009].dbo.StockItemPrice ON PriceBand.PriceBandID = StockItemPrice.PriceBandID
INNER JOIN [Pennine 2009].dbo.StockItem ON StockItemPrice.ItemID = StockItem.ItemID
WHERE (PriceBand.PriceBandID = '5045261')),
  BarCodeOuter(Code, Barcode_Outer) AS
(SELECT StockItem_1.Code,
      LEFT(StockItemPrice_1.Price, 12) AS Expr1
FROM [Pennine 2009].dbo.PriceBand AS PriceBand_1
INNER JOIN [Pennine 2009].dbo.StockItemPrice AS StockItemPrice_1 ON PriceBand_1.PriceBandID = StockItemPrice_1.PriceBandID
INNER JOIN [Pennine 2009].dbo.StockItem AS StockItem_1 ON StockItemPrice_1.ItemID = StockItem_1.ItemID
WHERE (PriceBand_1.PriceBandID = '5048507'))
INSERT INTO [Label Software].dbo.[Label Information](Name, Code, Barcode_Inner)
SELECT BarCodeInner.Code AS ProductCode,
   BarCodeInner.Name AS Description,
   BarCodeInner.Barcode_Inner AS BarcodeSingle,
   BarCodeOuter.Barcode_Outer AS BarcodePack
FROM BarCodeInner AS BarCodeInner
INNER JOIN BarCodeOuter AS BarCodeOuter ON BarCodeInner.Code = BarCodeOuter.Code

答案 1 :(得分:0)

只需在查询前放置INSERT INTO [Label Software].dbo.[Label Information]行。

答案 2 :(得分:0)

你可以尝试这样的事情:

    WITH BarCodeInner(Name, Code, Barcode_Inner) AS
(SELECT StockItem.Name,
      StockItem.Code,
      LEFT(StockItemPrice.Price, 12) AS Expr1
FROM [Pennine 2009].dbo.PriceBand
INNER JOIN [Pennine 2009].dbo.StockItemPrice ON PriceBand.PriceBandID = StockItemPrice.PriceBandID
INNER JOIN [Pennine 2009].dbo.StockItem ON StockItemPrice.ItemID = StockItem.ItemID
WHERE (PriceBand.PriceBandID = '5045261')),
  BarCodeOuter(Code, Barcode_Outer) AS
(SELECT StockItem_1.Code,
      LEFT(StockItemPrice_1.Price, 12) AS Expr1
FROM [Pennine 2009].dbo.PriceBand AS PriceBand_1
INNER JOIN [Pennine 2009].dbo.StockItemPrice AS StockItemPrice_1 ON PriceBand_1.PriceBandID = StockItemPrice_1.PriceBandID
INNER JOIN [Pennine 2009].dbo.StockItem AS StockItem_1 ON StockItemPrice_1.ItemID = StockItem_1.ItemID
WHERE (PriceBand_1.PriceBandID = '5048507'))
SELECT BarCodeInner.Code AS ProductCode,
   BarCodeInner.Name AS Description,
   BarCodeInner.Barcode_Inner AS BarcodeSingle,
   BarCodeOuter.Barcode_Outer AS BarcodePack

INTO [Label Software].dbo.[Label Information]

FROM BarCodeInner AS BarCodeInner
INNER JOIN BarCodeOuter AS BarCodeOuter ON BarCodeInner.Code = BarCodeOuter.Code
ORDER BY BarCodeInner.Code

只要这两个数据库在同一个实例上。 如果没有,请尝试使用链接服务器执行相同的操作。