Ich habe 7 Tabellen - Firma, Person, Person_Link, Adresse, Address_Link, Telefon, Phone_Link. Ich möchte eine Stored Procedure haben, die den Parameter comp_companyid eingibt, dann kann sie alle Zeilen in den 7 Tabellen mit neuen Primärschlüsseln und fremden für jede Tabelle duplizieren.SQL - Wie Stored Procedure verwenden, um Zeile mit neuem Primärschlüssel zu duplizieren?
Tables:
Firma: PK - comp_companyid FK - comp_primarypersonid, comp_primaryaddressid
Person: PK - pers_personid FK - pers_companyid
Person_Link: PK - peli_personlinkid FK - peli_personid, pe
li_companyidAddress_Link: PK - adli_addresslinkid FK - adli_addressid, adli_companyid
Adresse: PK - addr_addressid
Telefon: PK - phon_phoneid
Phone_Link: PK - plink_linkid FK - plink_phoneid
Meine Anstrengung:
INSERT INTO Company
(
Comp_CompanyId, Comp_PrimaryPersonId, Comp_PrimaryAddressId, Comp_Name, Comp_Type, Comp_Status, Comp_CreatedBy, Comp_CreatedDate, Comp_UpdatedBy, Comp_UpdatedDate, Comp_TimeStamp,
Comp_SecTerr, Comp_WebSite
)
SELECT @companyId, @PersonId, @AddressId, Comp_Name, Comp_Type, Comp_Status, Comp_CreatedBy, Comp_CreatedDate, Comp_UpdatedBy, Comp_UpdatedDate, Comp_TimeStamp,
Comp_SecTerr, Comp_WebSite
FROM Company
WHERE Comp_CompanyId = @comp_companyid
INSERT INTO Person
(
Pers_PersonId, Pers_CompanyId, Pers_PrimaryUserId, Pers_FirstName, pers_SecTerr, Pers_CreatedBy, Pers_CreatedDate, Pers_UpdatedBy, Pers_UpdatedDate, Pers_TimeStamp
)
SELECT @PersonId, @companyId, Pers_PrimaryUserId, Pers_FirstName, pers_SecTerr, Pers_CreatedBy, Pers_CreatedDate, Pers_UpdatedBy, Pers_UpdatedDate, Pers_TimeStamp
FROM Person
WHERE Pers_CompanyId = @comp_companyid
INSERT INTO Person_Link
(
PeLi_PersonLinkId, PeLi_PersonId, PeLi_CompanyID, PeLi_CreatedBy, PeLi_CreatedDate, PeLi_UpdatedBy, PeLi_UpdatedDate, PeLi_TimeStamp
)
SELECT @PersonLinkId, @PersonId, @CompanyId, PeLi_CreatedBy, PeLi_CreatedDate, PeLi_UpdatedBy, PeLi_UpdatedDate, PeLi_TimeStamp
FROM Person_Link
WHERE PeLi_CompanyID = @comp_companyid
INSERT INTO Address_Link
(
AdLi_AddressLinkId, AdLi_AddressId, AdLi_CompanyID, AdLi_CreatedBy, AdLi_CreatedDate, AdLi_UpdatedBy, AdLi_UpdatedDate, AdLi_TimeStamp, AdLi_Type
)
SELECT @AddressLinkId, @AddressId, @companyId, AdLi_CreatedBy, AdLi_CreatedDate, AdLi_UpdatedBy, AdLi_UpdatedDate, AdLi_TimeStamp, AdLi_Type
FROM Address_Link
WHERE AdLi_CompanyID = @comp_companyid
INSERT INTO [Address]
(
Addr_AddressId, Addr_Address1, Addr_Address2, Addr_Address3, Addr_Address4, addr_postcode, Addr_CreatedBy, Addr_CreatedDate,
Addr_UpdatedBy, Addr_UpdatedDate, Addr_TimeStamp
)
SELECT @AddressId, Addr_Address1, Addr_Address2, Addr_Address3, Addr_Address4, addr_postcode, Addr_CreatedBy, Addr_CreatedDate,
Addr_UpdatedBy, Addr_UpdatedDate, Addr_TimeStamp
FROM Address
LEFT JOIN Address_Link
ON Addr_AddressId = AdLi_AddressId
AND AdLi_CompanyID = @comp_companyid
INSERT INTO Phone
(
Phon_PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp
)
SELECT @PhoneId, Phon_Number, Phon_CreatedBy, Phon_CreatedDate, Phon_UpdatedBy, Phon_UpdatedDate, Phon_TimeStamp
FROM Phone
WHERE Phon_PhoneId = @@IDENTITY
INSERT INTO PhoneLink
(
PLink_LinkID, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, PLink_TimeStamp,
PLink_EntityID, PLink_RecordID, PLink_Type, PLink_PhoneId
)
SELECT @PhoneLinkId, PLink_CreatedBy, PLink_CreatedDate, PLink_UpdatedDate, PLink_TimeStamp,
PLink_EntityID, PLink_RecordID, PLink_Type, @PhoneId
FROM PhoneLink
WHERE PLink_LinkID = @@IDENTITY
-- EXEC @PhoneId = crm_next_id 14
-- EXEC @PhoneLinkId = crm_next_id 10208
Er arbeitete für Unternehmen & Person & Person_Link aber zeigt Fehler an den anderen Tischen. Die Fehlermeldung lautet:
"Verletzung der PRIMARY KEY-Einschränkung 'PK__Address___533839D503317E3D'. Kann keinen doppelten Schlüssel im Objekt 'dbo.Address_Link' einfügen." (dbo.Address als auch)
Es gab nicht Telefon-Tabelle Fehler, aber Telefon Tabelle wird keine neue Zeile erstellen. Hat jemand eine Idee, wie man das löst? Vielen Dank!
Hallo, ich habe die NextID erklärt und bekannt. Ich habe meinen Beitrag bearbeitet und meinen Code-Aufwand gezeigt, der für Unternehmen und Personen funktioniert, aber nicht für andere. –
jetzt funktionierte es auch für personlink. –