我有7个表 - 公司,人员,Person_Link,地址,Address_Link,电话,Phone_Link。我想有一个存储过程,它接受comp_companyid的参数,然后它可以复制7个表中的所有行,每个表都有新的主键和外来行。
表:
公司: PK - comp_companyid FK - comp_primarypersonid,comp_primaryaddressid
人: PK - pers_personid FK - pers_companyid
Person_Link: PK - peli_personlinkid FK - peli_personid,peli_companyid
Address_Link: PK - adli_addresslinkid FK - adli_addressid,adli_companyid
地址: PK - addr_addressid
电话: PK - phon_phoneid
Phone_Link: PK - plink_linkid FK - plink_phoneid
我的努力:
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
适用于公司和公司。人与人Person_Link但在其他表上显示错误。错误消息是:
“违反PRIMARY KEY约束'PK__Address___533839D503317E3D'。无法在对象'dbo.Address_Link'中插入重复键。” (dbo.Address也是)
它没有指定电话表的错误,但电话表不会创建任何新行。任何人都知道如何解决这个问题?谢谢!
答案 0 :(得分:0)
我想您想知道,在公司中插入第一行(例如)后,这是您刚刚插入的公司的ID。
foo.config(["$httpProvider", function ($httpProvider) {
$httpProvider.interceptors.push("HttpErrorInterceptor");
}]);
会帮助您