2014-11-14 63 views
1

我很努力地編寫我的第一個過程,將多個表中的數據放在一起,並使用遊標將其寫入另一個表中,以遍歷所有數據。希望能在這裏找到一些幫助。在存儲過程中使用光標從多個表中寫入表

我加入6張表返回到主表,帳戶,以顯示所需的數據。使用第一個遊標,我加入了五個表以獲取所需的信息,然後我想再添加兩個遊標以從Phone Detail表中添加電話號碼(主要和次要)到表中。

希望這是有道理的。我確定我在SQL中錯過了一些東西,但基本上我想循環訪問Accounts表並將數據寫入一個新表,並通過Phone Detail表循環,併爲每個帳號獲取主要電話號碼。那麼第二個電話(而佔空值)寫這個新的表也是如此。

CREATE PROCEDURE [dbo].[CRM_Account_Info] 
    @AccountID int, 
    @AccountName nvarchar(128), 
    @Bus_Type nvarchar(50), 
    @AccountAddr1 nvarchar(128), 
    @AccountAddr2 nvarchar(128), 
    @AccountCity nvarchar(32), 
    @AccountState nvarchar(10), 
    @AccountZip nvarchar(10), 
    @Account_Coll_Area_CodeID int, 
    @Account_Coll_Area nvarchar(50), 
    @Account_CRC_ID int, 
    @Account_CRC_Name nvarchar(100), 
    @Account_Prime_Number nvarchar(120), 
    @Account_2nd_Number nvarchar(120) 
AS 
BEGIN 
    -- Truncate Accounts table 
    Execute Immediate 'Truncate DBO.CRM_Accounts'; 

    -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Grab Account ID, Account Name, Account Type, Address, City, State, Zip, Collection Area ID, Collection Area Description, 
    -- Recruiter ID, Recruiter Full Name from the Accounts Table 

    Declare Acct_Info cursor for 
     Select 
      Acct.AccountID, Acct.Internalname, 
      QC.Descshort, 
      AD.Addr1, AD.Addr2, AD.City, AD.State, AD.Zip, 
      Sub.CodeID, Sub.Description, 
      Peo.PersonID, Peo.Fullname 
     from 
      Accounts as Acct 
     inner join 
      AddressDetail AD on Acct.AccountID = AD.AccountID 
     inner join 
      CenterDetail CD on Acct.Centerid = CD.CenterID 
     inner join 
      People Peo on Acct.LeaderID = Peo.PersonID 
     inner join 
      IDViewOrgSubCenter SUB on CD.OrgSubCenter = SUB.CodeID 
     inner join 
      QuickCodes QC on Acct.AccountType = QC.CodeID 

    Open Acct_Info -- Open cursor 

    Fetch Next from Acct_Info into @AccountID, @AccountName, @Bus_Type, @AccountAddr1, 
            @AccountAddr2, @AccountCity, @AccountState, @AccountZip, 
            @Account_Coll_Area_CodeID, @Account_Coll_Area, 
            @Account_CRC_ID, @Account_CRC_Name, @Account_Prime_Number, 
            @Account_2nd_Number 

    Close Acct_Info -- Close cursor 

    -- Grab the Primary Phone for the Account 
    Declare Primary_Phone cursor for 

Select top 1 Acct.AccountID, PD.FormattedNumber 
From PhoneDetail PD 
inner join Accounts Acct on PD.AccountID=Acct.AccountID 
Where PD.PrimaryPhone=1 
And [email protected] 

Close Primary_Phone -- Close cursor 

-- Grab the second phone for an account 

Declare Secondary_Phone cursor for 

Select top 1 Acct.AccountID, PD.FormattedNumber 
From PhoneDetail PD 
inner join Accounts Acct on PD.AccountID=Acct.AccountID 
Where PD.PrimaryPhone<>1 
And [email protected] 

Close Secondary_Phone -- Close cursor 

-- Insert the values into the CRM table 
Insert CRM_Accounts (
AccountID, 
AccountName, 
Bus_Type, 
AccountAddr1, 
AccountAddr2, 
AccountCity, 
AccountState, 
AccountZip, 
Account_Coll_Area_CodeID, 
Account_Coll_Area, 
Account_CRC_ID, 
Account_CRC_Name, 
Account_Prime_Number, 
Account_2nd_Number 
) 
Values (
@AccountID, 
@AccountName, 
@Bus_Type, 
@AccountAddr1, 
@AccountAddr2, 
@AccountCity, 
@AccountState, 
@AccountZip, 
@Account_Coll_Area_CodeID, 
@Account_Coll_Area, 
@Account_CRC_ID, 
@Account_CRC_Name, 
@Account_Prime_Number, 
@Account_2nd_Number 
) 
END 
GO 
+0

你能寫一個選擇來獲取你需要的所有數據嗎?如果是這樣,那麼一個簡單的'INSERT INTO' ...'SELECT'會在沒有使用遊標的情況下到達那裏。 – SQLChao 2014-11-14 21:13:46

+0

我試過這個,但是我不知道如何從同一張表中拉出多個電話號碼時如何編寫單個選擇。 – MISNole 2014-11-14 21:17:16

+0

這樣一個帳戶可以有多個電話號碼,其中primaryphone = 1?一個帳戶可以有多個電話號碼,其中主要電話<> 1?我假設這是因爲你使用了'TOP 1' – SQLChao 2014-11-14 21:20:18

回答

0

試試這個把一切在一個查詢。我做了2代派生表的小一和中一併給他們排由ACCOUNTID分區號碼。然後在加入我只得到ROWNUMBER = 1,因此它只返回每一個ACCOUNTID數。

SELECT Acct.accountid, 
     Acct.internalname, 
     QC.descshort, 
     AD.addr1, 
     AD.addr2, 
     AD.city, 
     AD.state, 
     AD.zip, 
     Sub.codeid, 
     Sub.description, 
     Peo.personid, 
     Peo.fullname, 
     pd1.formattedNumber, 
     pd2.formattedNumber 
FROM accounts AS Acct 
     INNER JOIN addressdetail AD 
       ON Acct.accountid = AD.accountid 
     INNER JOIN centerdetail CD 
       ON Acct.centerid = CD.centerid 
     INNER JOIN people Peo 
       ON Acct.leaderid = Peo.personid 
     INNER JOIN idvieworgsubcenter SUB 
       ON CD.orgsubcenter = SUB.codeid 
     INNER JOIN quickcodes QC 
       ON Acct.accounttype = QC.codeid 
     LEFT JOIN (select accountid, formattednumber, primaryphone, row_number() over(partition by accountid order by formattednumber) as rNum 
          from phonedetail where primaryphone = 1) pd1 
       ON acct.accountid = pd1.accountid and pd1.rNum = 1 
     LEFT JOIN (select accountid, formattednumber, primaryphone, row_number() over(partition by accountid order by formattednumber) as rNum 
          from phonedetail where primaryphone <> 1) pd2 
       ON acct.accountid = pd2.accountid and pd2.rNum = 1 
+0

謝謝@趙超。小modificatino,我不得不改變「和pd.rNum = 1到pd1.rNum和pd2相同,但這似乎很好地工作。 – MISNole 2014-11-14 21:41:21

+0

@MISNole對此感到抱歉。我發佈後不久看到錯誤 – SQLChao 2014-11-14 21:41:59

+0

不需要道歉,我一直試圖弄清楚這一點,我非常感謝你的幫助。 – MISNole 2014-11-14 21:52:49

0

@JChao,我將標記你的答案是正確的,因爲SQL工作得非常出色,但是我想根據收到的建議發佈,並且現在要額外注意看看這個過程。它似乎工作,但我只希望更有經驗的用戶檢查它,看看他們是否發現任何明顯的錯誤:

第一步是放下(或截斷)當前表中的所有數據,然後寫入選擇語句到我的新表中:

Create Procedure Update_CRM_Accts 
--Alter Procedure Update_CRM_Accts 
As 
Begin 
Truncate Table CRM_Accounts 
SET NOCOUNT ON; 
Insert into CRM_Accounts 
Select Acct.AccountID, 
    Acct.InternalName, 
    QC.DescShort, 
    AD.Addr1, 
    AD.Addr2, 
    AD.City, 
    AD.State, 
    AD.Zip, 
    Sub.CodeID, 
    Sub.Description, 
    Peo.PersonID, 
    Peo.FullName, 
    PD1.FormattedNumber as 'Primary_Number', 
    PD2.FormattedNumber as 'Secondary_Number' 
From Accounts As Acct 
INNER JOIN addressdetail AD ON Acct.AccountID = AD.AccountID 
INNER JOIN CenterDetail CD ON Acct.CenterID = CD.CenterID 
INNER JOIN People Peo ON Acct.LeaderID = Peo.PersonID 
INNER JOIN IDViewOrgSubCenter SUB ON CD.OrgSubcenter = SUB.CodeID 
INNER JOIN quickcodes QC ON Acct.AccountType = QC.CodeID 
LEFT OUTER JOIN (Select AccountID, FormattedNumber, PrimaryPhone, row_number() over(partition by AccountID order by FormattedNumber) as rNum 
     From PhoneDetail where PrimaryPhone = 1) PD1 
      ON Acct.accountid = PD1.AccountID and PD1.rNum = 1 
LEFT OUTER JOIN (Select AccountID, FormattedNumber, PrimaryPhone, row_number() over(partition by AccountID order by FormattedNumber) as rNum 
     From PhoneDetail where PrimaryPhone <> 1) PD2 
      ON Acct.AccountID = PD2.AccountID and PD2.rNum = 1 
End 
Go