-1
您好我試圖糾正一個Proc的數據加載時要插入基於表2插入記錄並更新現有記錄
在表1的記錄這是我來到與 它有2個條件
1) if the record does not exist create a new row of record
2) if the record already exist update the record based on keys
這是我的PROC需要一些幫助,感謝
DECLARE @TableKey INT --(it is passed by the user proc param),
DECLARE @TableCount INT,
DECLARE @CLassKey INT,
SELECT @TableCount= COUNT(*) FROM Table1 WHERE Tablekey= @TableKey
INSERT INTO @CLassKey
SELECT Distinct c.PK_ClassKey FROM CLASS as c
INNER JOIN BOOK as B ON B.FK_ClassKey=C.PK_ClassKEy
IF ((SELECT COUNT(*) FROM @ClassKey) > 0 AND @TableCount= 0)--- this will check
BEGIN
Insert into NOTE
n.note
Select
c.note
FROM Class where c.FK_Note = n.PK_Note.
END
----這將只需插入首次.. 如何d O I更新任何想法,因爲記錄只插入第一次提出使用相同的格式非常感謝
用MERGE語句,不更新。 – Avi
這是什麼語言,僞語言? – Drew
看看'插入重複密鑰更新'。 [手冊頁](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html)也許這將是有用的。它需要一個獨特的鑰匙來衝擊火災的更新。這裏是一個[視覺示例](http://stackoverflow.com/a/32468519) – Drew