2017-10-17 70 views
0

我們有一個使用SQL Server的CRM系統,並且正在尋找使用電子郵件地址填充所有客戶,即使該電子郵件地址爲NULL。從屬插入多行選擇

有一個客戶表,一個電子郵件地址表和一個鏈接器表。

我可以執行SELECT查詢誰需要一個新的電子郵件一切爲客戶排:

SELECT @CompanyId = Comp_CompanyId 
FROM Company 
WHERE Company.Comp_CompanyId NOT IN (
    SELECT ELink_RecordID 
    FROM EmailLink 
) 

,我可以執行INSERT報表的電子郵件地址添加到客戶:

DECLARE @EmailId int 
DECLARE @ELinkId int 

EXEC @ELinkId = eware_get_identity_id 'Email' 
EXEC @EmailId = eware_get_identity_id 'Email' 

INSERT INTO Email (
    Emai_EmailId, 
    Emai_CreatedBy, 
    Emai_CreatedDate, 
    Emai_UpdatedBy, 
    Emai_UpdatedDate, 
    Emai_TimeStamp, 
    Emai_EmailAddress 
) 
VALUES (
    @EmailId, 
    1, 
    GETDATE(), 
    1, 
    GETDATE(), 
    GETDATE(), 
    NULL 
) 
INSERT INTO EmailLink(
    ELink_LinkID, 
    ELink_CreatedBy, 
    ELink_CreatedDate, 
    ELink_UpdatedBy, 
    ELink_UpdatedDate, 
    ELink_TimeStamp, 
    ELink_EntityID, 
    ELink_RecordID, 
    ELink_Type, 
    ELink_EmailId 
) 
VALUES(
    @ELinkId, 
    1, 
    GETDATE(), 
    1, 
    GETDATE(), 
    GETDATE(), 
    5,   -- Person is 13, Company is 5 
    COMPANY_ID_GOES_HERE, 
    'Business', 
    @EmailId 
) 

有沒有辦法將這些組合成一個大的SQL語句,而無需使用外部工具?我可以編寫一個快速的Python程序來執行此操作,但維護這個長期操作的人員都是SQL的關注焦點。

最遠的我所得到的是結合SELECTINSERT的電子郵件,但無法弄清楚如何再INSERT鏈接行:

DECLARE @CompanyId int 
DECLARE @EmailId int 
DECLARE @ELinkId int 

EXEC @ELinkId = eware_get_identity_id 'Email' 
EXEC @EmailId = eware_get_identity_id 'Email' 

INSERT INTO Email (
    Emai_EmailId, 
    Emai_CreatedBy, 
    Emai_CreatedDate, 
    Emai_UpdatedBy, 
    Emai_UpdatedDate, 
    Emai_TimeStamp, 
    Emai_EmailAddress 
) 
VALUES (
    @EmailId, 
    1, 
    GETDATE(), 
    1, 
    GETDATE(), 
    GETDATE(), 
    NULL 
) 

SELECT @CompanyId = Comp_CompanyId 
FROM Company 
WHERE Company.Comp_CompanyId NOT IN (
    SELECT ELink_RecordID 
    FROM EmailLink 
) 

回答

2

將這項工作的第二部分?

DECLARE @CompanyId int 
DECLARE @EmailId int 
DECLARE @ELinkId int 

EXEC @ELinkId = eware_get_identity_id 'Email' 
EXEC @EmailId = eware_get_identity_id 'Email' 

INSERT INTO Email (
    Emai_EmailId, 
    Emai_CreatedBy, 
    Emai_CreatedDate, 
    Emai_UpdatedBy, 
    Emai_UpdatedDate, 
    Emai_TimeStamp, 
    Emai_EmailAddress 
) 
VALUES (
    @EmailId, 
    1, 
    GETDATE(), 
    1, 
    GETDATE(), 
    GETDATE(), 
    NULL 
) 
;WITH cteX 
AS(
    SELECT CompanyId = Comp_CompanyId 
    FROM Company 
    WHERE Company.Comp_CompanyId 
    NOT IN (
      SELECT ELink_RecordID 
      FROM EmailLink 
      ) 
) 
INSERT INTO EmailLink(
    ELink_LinkID, 
    ELink_CreatedBy, 
    ELink_CreatedDate, 
    ELink_UpdatedBy, 
    ELink_UpdatedDate, 
    ELink_TimeStamp, 
    ELink_EntityID, 
    ELink_RecordID, 
    ELink_Type, 
    ELink_EmailId 
) 
SELECT 
    @ELinkId, 
    1, 
    GETDATE(), 
    1, 
    GETDATE(), 
    GETDATE(), 
    5,   -- Person is 13, Company is 5 
    X.CompanyId, --COMPANY_ID_GOES_HERE, 
    'Business', 
    @EmailId 
FROM 
    cteX X 
+0

不幸的不是。我得到「cteX'附近的語法不正確。」回。還必須添加DECLARE和EXEC行。只有一個INSERT不在這兩個中,並且不會從EXEC存儲過程中獲取所需的ID。 – Metalshark

+0

我已經更新了答案,不正確的語法新cteX是由於缺少分號 –

+0

我仍然有一些工作要做,以使這項工作,但額外的工作不包括在這個問題。您的解決方案適用於所要求的內容。例如必須爲每次迭代'EXEC'存儲過程。 – Metalshark