2011-03-10 34 views
0

選擇單列我只想項目ID從這個結果EXCEPT語句:從EXCEPT結果

SELECT  ManufacturerID, ItemID, ItemName, Description, Notes, Dimensions, BasePrice, SpecialPrice, OrderMinimumQuantity, OrderMultipleQuantity, 
         OnHandQuantity, Category, IntroDate, BackOrderDate, UPC, PriceLevel1, PriceLevel2, PriceLevel3, PriceLevel4, PriceLevel5, PriceLevel6, PriceLevel7, PriceLevel8, 
         PriceLevel9, PieceBox, Cubes, UnitOfMeasure, UDF1, UDF2, UDF3, UDF4, UDF5, AdditionalImageCount, PhotoName, AppendProductModifiers, Discontinued, 
         IsDeleted 
FROM   StagingProducts 
WHERE  (ManufacturerID = 10) 
EXCEPT 
SELECT  ManufacturerID, ItemID, ItemName, Description, Notes, Dimensions, BasePrice, SpecialPrice, OrderMinimumQuantity, OrderMultipleQuantity, 
         OnHandQuantity, Category, IntroDate, BackOrderDate, UPC, PriceLevel1, PriceLevel2, PriceLevel3, PriceLevel4, PriceLevel5, PriceLevel6, PriceLevel7, PriceLevel8, 
         PriceLevel9, PieceBox, Cubes, UnitOfMeasure, UDF1, UDF2, UDF3, UDF4, UDF5, AdditionalImageCount, PhotoName, AppendProductModifiers, Discontinued, 
         IsDeleted 
FROM   Products 
WHERE  (ManufacturerID = 10) 

什麼將是非常好的是,如果我能救的結果EXCEPT語句用在INSERT INTO進一步向下查詢。

基本上,我打算刪除基於ItemID從Products表中這個EXCEPT語句返回的記錄,然後插入新記錄,這些記錄是同一個EXCEPT語句的結果 - 與舊的in結合使用new。

更新 - 工作液:

DECLARE @T TABLE (
    [ManufacturerID] [int] NOT NULL, 
    [ItemID] [nvarchar](50) NULL, 
    [ItemName] [nvarchar](100) NOT NULL, 
    [Description] [nvarchar](max) NULL, 
    [Notes] [nvarchar](200) NULL, 
    [Dimensions] [nvarchar](50) NULL, 
    [BasePrice] [money] NOT NULL, 
    [SpecialPrice] [money] NULL, 
    [OrderMinimumQuantity] [int] NOT NULL, 
    [OrderMultipleQuantity] [int] NOT NULL, 
    [OnHandQuantity] [int] NULL, 
    [Category] [nvarchar](100) NULL, 
    [IntroDate] [date] NULL, 
    [BackOrderDate] [date] NULL, 
    [UPC] [nvarchar](25) NULL, 
    [PriceLevel1] [decimal](18, 0) NULL, 
    [PriceLevel2] [decimal](18, 0) NULL, 
    [PriceLevel3] [decimal](18, 0) NULL, 
    [PriceLevel4] [decimal](18, 0) NULL, 
    [PriceLevel5] [decimal](18, 0) NULL, 
    [PriceLevel6] [decimal](18, 0) NULL, 
    [PriceLevel7] [decimal](18, 0) NULL, 
    [PriceLevel8] [decimal](18, 0) NULL, 
    [PriceLevel9] [decimal](18, 0) NULL, 
    [PieceBox] [int] NULL, 
    [Cubes] [decimal](18, 0) NULL, 
    [UnitOfMeasure] [nvarchar](10) NULL, 
    [UDF1] [nvarchar](50) NULL, 
    [UDF2] [nvarchar](50) NULL, 
    [UDF3] [nvarchar](50) NULL, 
    [UDF4] [nvarchar](50) NULL, 
    [UDF5] [nvarchar](50) NULL, 
    [AdditionalImageCount] [smallint] NULL, 
    [PhotoName] [nvarchar](50) NULL, 
    [AppendProductModifiers] [bit] NULL, 
    [Discontinued] [bit] NULL, 
    [IsDeleted] [bit] NOT NULL) 

;WITH T As 
(SELECT  ManufacturerID, ItemID, ItemName, Description, Notes, Dimensions, BasePrice, SpecialPrice, OrderMinimumQuantity, OrderMultipleQuantity, 
         OnHandQuantity, Category, IntroDate, BackOrderDate, UPC, PriceLevel1, PriceLevel2, PriceLevel3, PriceLevel4, PriceLevel5, PriceLevel6, PriceLevel7, PriceLevel8, 
         PriceLevel9, PieceBox, Cubes, UnitOfMeasure, UDF1, UDF2, UDF3, UDF4, UDF5, AdditionalImageCount, PhotoName, AppendProductModifiers, Discontinued, 
         IsDeleted 
FROM   StagingProducts 
WHERE  (ManufacturerID = @ManufacturerID) 
EXCEPT 
SELECT  ManufacturerID, ItemID, ItemName, Description, Notes, Dimensions, BasePrice, SpecialPrice, OrderMinimumQuantity, OrderMultipleQuantity, 
         OnHandQuantity, Category, IntroDate, BackOrderDate, UPC, PriceLevel1, PriceLevel2, PriceLevel3, PriceLevel4, PriceLevel5, PriceLevel6, PriceLevel7, PriceLevel8, 
         PriceLevel9, PieceBox, Cubes, UnitOfMeasure, UDF1, UDF2, UDF3, UDF4, UDF5, AdditionalImageCount, PhotoName, AppendProductModifiers, Discontinued, 
         IsDeleted 
FROM   Products 
WHERE  (ManufacturerID = @ManufacturerID) 
) 
INSERT INTO @T 
SELECT * 
FROM T 

    -- Kill the old products 
    Delete FROM Products where ManufacturerID = @ManufacturerID 
     AND ItemID IN(SELECT ItemID FROM @T) 

    -- insert the new products 
    INSERT INTO Products ([ManufacturerID] 
      ,[ItemID] 
      ,[ItemName] 
      ,[Description] 
      ,[Notes] 
      ,[Dimensions] 
      ,[BasePrice] 
      ,[SpecialPrice] 
      ,[OrderMinimumQuantity] 
      ,[OrderMultipleQuantity] 
      ,[OnHandQuantity] 
      ,[Category] 
      ,[IntroDate] 
      ,[BackOrderDate] 
      ,[UPC] 
      ,[PriceLevel1] 
      ,[PriceLevel2] 
      ,[PriceLevel3] 
      ,[PriceLevel4] 
      ,[PriceLevel5] 
      ,[PriceLevel6] 
      ,[PriceLevel7] 
      ,[PriceLevel8] 
      ,[PriceLevel9] 
      ,[PieceBox] 
      ,[Cubes] 
      ,[UnitOfMeasure] 
      ,[UDF1] 
      ,[UDF2] 
      ,[UDF3] 
      ,[UDF4] 
      ,[UDF5] 
      ,[AdditionalImageCount] 
      ,[PhotoName] 
      ,[AppendProductModifiers] 
      ,[Discontinued] 
      ,[CreatedOn] 
      ,[CreatedBy] 
      ,[ModifiedOn] 
      ,[ModifiedBy] 
      ,[DeletedOn] 
      ,[DeletedBy] 
      ,[IsDeleted]) 
    SELECT [ManufacturerID] 
     ,[ItemID] 
     ,[ItemName] 
     ,[Description] 
     ,[Notes] 
     ,[Dimensions] 
     ,[BasePrice] 
     ,[SpecialPrice] 
     ,[OrderMinimumQuantity] 
     ,[OrderMultipleQuantity] 
     ,[OnHandQuantity] 
     ,[Category] 
     ,[IntroDate] 
     ,[BackOrderDate] 
     ,[UPC] 
     ,[PriceLevel1] 
     ,[PriceLevel2] 
     ,[PriceLevel3] 
     ,[PriceLevel4] 
     ,[PriceLevel5] 
     ,[PriceLevel6] 
     ,[PriceLevel7] 
     ,[PriceLevel8] 
     ,[PriceLevel9] 
     ,[PieceBox] 
     ,[Cubes] 
     ,[UnitOfMeasure] 
     ,[UDF1] 
     ,[UDF2] 
     ,[UDF3] 
     ,[UDF4] 
     ,[UDF5] 
     ,[AdditionalImageCount] 
     ,[PhotoName] 
     ,[AppendProductModifiers] 
     ,[Discontinued] 
     ,[CreatedOn] 
     ,[CreatedBy] 
     ,[ModifiedOn] 
     ,[ModifiedBy] 
     ,[DeletedOn] 
     ,[DeletedBy] 
     ,[IsDeleted] from StagingProducts 
     Where ManufacturerID = @ManufacturerID 
     AND ItemID IN(SELECT ItemID FROM @T) 
+0

RE:您的更新。我的回答是'INSERT INTO @T SELECT ItemID FROM T'你的代碼有'INSERT INTO @ T'懸掛。 – 2011-03-10 17:16:26

回答

2

如您SQL Server 2008上,你可能要考慮MERGE爲您同步需求,但要回答這個問題問你可以做

DECLARE @T TABLE (ItemID INT PRIMARY KEY) 

;WITH T As 
(
Your Big Statement 
) 
INSERT INTO @T 
SELECT ItemID 
FROM T 
+0

MERGE聽起來像我所需要的,可能從你身上得到一個例子嗎? – Slee 2011-03-10 16:58:04

+0

@Slee - 這裏有[一篇文章](http://www.mssqltips.com/tip.asp?tip=1704),它給出了更新更改行,插入新行和刪除不匹配行的示例(所有部分都是可選的)。您還必須記住,'EXCEPT'將'NULL'視爲相等,而您的'UPDATE'條件需要在測試中明確處理NULLable列,以確定行是否不同。 – 2011-03-10 17:00:15

1

與Martin關於使用MERGE的答案分開...

您可以使用NOT EXISTS,它提供相同的查詢計劃並且更具可讀性。

SELECT  ManufacturerID, ItemID, ItemName, Description, Notes, Dimensions, BasePrice, SpecialPrice, OrderMinimumQuantity, OrderMultipleQuantity, 
         OnHandQuantity, Category, IntroDate, BackOrderDate, UPC, PriceLevel1, PriceLevel2, PriceLevel3, PriceLevel4, PriceLevel5, PriceLevel6, PriceLevel7, PriceLevel8, 
         PriceLevel9, PieceBox, Cubes, UnitOfMeasure, UDF1, UDF2, UDF3, UDF4, UDF5, AdditionalImageCount, PhotoName, AppendProductModifiers, Discontinued, 
         IsDeleted 
FROM   StagingProducts SG 
WHERE 
    (ManufacturerID = @ManufacturerID) 

NOT EXISTS (SELECT * FROM 
     Products P 
     WHERE 
      P.Key1 = SG.Key1 AND SG.Key2 = SG.Key2 AND SG.Key3 = SG.Key3) 
1

忘記CTE和表變量:只需使用MERGE

CTE只是在做的WHEN [NOT] MATCH部分做的任何事情。

您已經有臨時表(StagingProducts),因此您不需要@T

MERGE會是這個樣子(我已經縮短了與...列的列表):

MERGE INTO Products 
USING StagingProducts 
    ON Products.ManufacturerID = StagingProducts.ManufacturerID 
     AND Products.ItemID = StagingProducts.ItemID 
     AND Products.ManufacturerID = @ManufacturerID 
WHEN MATCHED THEN 
    UPDATE 
     SET ItemName = StagingProducts.ItemName, 
      Description = StagingProducts.Description, 
      Notes = StagingProducts.Notes, 
      ... 
      IsDeleted = StagingProducts.IsDeleted 
WHEN NOT MATCHED THEN 
    INSERT (ManufacturerID, ItemID, ItemName, Description, Notes, ..., IsDeleted) 
     VALUES (ManufacturerID, ItemID, ItemName, Description, Notes, ..., IsDeleted);