我通過搜索UniqueColID並將結果插回到表中來複制記錄....這樣做我有一個重複的條目,這是我想要的除了我想要一列不重複和該列UniqueColID ..用SQL插入重複記錄
下面是我的代碼:
CREATE PROCEDURE [dbo].[InsertDuplicateFields]
(@UniqueColID varchar(50), @IndividualID varchar(50) = null)
AS
Begin
INSERT INTO TableCollisionBegin
SELECT * FROM TableCollisionBegin
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableCollisionDetails
SELECT * FROM TableCollisionDetails
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableCollisionLocation
SELECT * FROM TableCollisionLocation
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableDriver
SELECT * FROM TableDriver
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableFollowUp
SELECT * FROM TableFollowUp
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableOfficerLogs
SELECT * FROM TableOfficerLogs
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TablePolice
SELECT * FROM TablePolice
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableRecVerified
SELECT * FROM TableRecVerified
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableSignature
SELECT * FROM TableSignature
WHERE [IndividualID] = @IndividualID;
INSERT INTO TableTrailer
SELECT * FROM TableTrailer
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableValidateLog
SELECT * FROM TableValidateLog
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableVehicle
SELECT * FROM TableVehicle
WHERE [UniqueColID] = @UniqueColID;
INSERT INTO TableWitness
SELECT * FROM TableWitness
WHERE [UniqueColID] = @UniqueColID;
End
每個表有許多列,但他們都有UniqueColID。
當我插入重複我想所有的細節重複,但我想插入一個新的UniqueColID ..我可以做到這一點?
新的UniqueColID由ASP.NET代碼中的特殊方法生成。我想有2或3或4不同的UniqueColID與其餘的列是重複的。
我想你說的代碼2號地塊,但得到的錯誤:
"Unknown object type 'TEMPORARY' used in a CREATE, DROP, or ALTER statement."
CREATE PROCEDURE [dbo].[AmendInsertDuplicateFields] (@UniqueColID varchar(50), @NewUniqueColID varchar(50))
AS
Begin CREATE TEMPORARY TABLE Temp AS SELECT * FROM [MVCNew].[dbo].[CollisionBegin] WHERE [UniqueColID] = @UniqueColID;
UPDATE Temp SET UniqueColID = @NewUniqueColID;
INSERT INTO [MVCNew].[dbo].[CollisionBegin] SELECT * FROM Temp;
DROP TEMPORARY TABLE IF EXISTS Temp;
End
我只是在想....是否有可能對我來說,插入一些隨機UniqueColID值的新紀錄但所有其他列都插入值爲「o」或「u」或「一些隨機文本」,然後我插入到此新記錄的所有UniqueColID列除了UniqueColID?這樣我會有一個新的UniqueColID與其餘的列具有相同的值。
KCD我曾經嘗試都你的代碼,並沒有工作塊.. :(
我個人沒有看到如何選擇@NewUniqueColID ....甚至工作,因爲在命名的值的表沒有列@NewUniqueColID
你是說我手動輸入列名,例如ColB,ColC,ColD?在這些表格中有200個ish列我會堅持試圖做到這一點... – Bulvak
是的,想你可以這麼說!希望第二個建議有幫助... – KCD
我試過你提到的第二個塊,我得到:在CREATE,DROP或ALTER語句中使用的未知對象類型'TEMPORARY'。 – Bulvak