我在Microsoft SQL Server的工作,我有我的數據庫中的一個問題,即我試圖插入新的數據到一個表,但數據在過去被意外插入一次,因爲這樣的重新插入會導致重大違規行爲。我正在尋找一種方法來插入我需要的數據並更新已經意外包含的內容。一些代碼的詳情已更改爲簡潔,但我的代碼,因爲它現在代表的是:SQL INSERT和UPDATE
BEGIN TRANSACTION
INSERT INTO dbs_salesinternals.acc_saleshistory
SELECT a,
b AS n
FROM dbs_sales.orders AS O
INNER JOIN dbs_salesinternals.acc_report_months AS ARM
ON ARM.acc_report_month_id = dbo.Reportmonth(CASE
WHEN O.ordertypeid = 2 THEN shipduedate
ELSE shipdate
END)
INNER JOIN dbo.contactfullnames
ON O.customerid = dbo.contactfullnames.contact_id
INNER JOIN dbs_sales.ordertypes
ON dbs_sales.ordertypes.ordertypeid = O.ordertypeid
LEFT JOIN(SELECT OD.orderid,
Sum(CASE
WHEN PB.format LIKE '%kit%'
AND units.curriculumcode = 'CUR' THEN
OD.price
ELSE 0
END) AS KitPart,
Sum(CASE
WHEN PB.format = 'PD'
AND units.curriculumcode = 'CUR' THEN
OD.price
ELSE 0
END) AS PDPart,
Sum(CASE
WHEN (PB.format <> 'PD'
AND PB.format NOT LIKE '%kit%'
AND units.curriculumcode = 'CUR') THEN
OD.price
ELSE 0
END) AS OtherPart,
Sum(CASE
WHEN PB.format LIKE '%kit%'
AND units.curriculumcode = 'WEB' THEN
OD.price
ELSE 0
END) AS EKitPart,
Sum(CASE
WHEN PB.format = 'PD'
AND units.curriculumcode = 'WEB' THEN
OD.price
ELSE 0
END) AS EPDPart,
Sum(CASE
WHEN (PB.format <> 'PD'
AND PB.format NOT LIKE '%kit%'
AND units.curriculumcode = 'WEB') THEN
OD.price
ELSE 0
END) AS EGuidePart
FROM dbs_salesinternals.eieproductsbase AS PB
INNER JOIN dbs_salesinternals.productpricing AS PP
ON PB.productid = PP.product_id
INNER JOIN dbs_sales.[order details] AS OD
ON PP.productpricing_id = OD.productpricing_id
INNER JOIN units
ON PB.unit_id = units.unit_id
GROUP BY OD.orderid) OP
ON OP.orderid = O.orderid
LEFT JOIN [dbo].[grant_codes]
ON [dbo].[grant_codes].grantcodemap_id = O.accountcode
WHERE O.ordertotal <> 0
AND O.ordertypeid IN (1, 2, 3, 4)
AND ARM.acc_report_month_id = 166
AND ([dbo].[grant_codes].granttype IS NULL
OR [dbo].[grant_codes].granttype <> 2)
COMMIT TRANSACTION
我怎樣才能改變這個來完成我需要什麼?
這聽起來像你需要調查MERGE:http://msdn.microsoft.com/en-us/library/bb510625.aspx –
你可能想編輯你的問題,並刪除問題的格式,使一切粗體將無濟於事。 –
是的,合併可以是一個非常有用的工具 –