我很努力地編寫我的第一個過程,將多個表中的數據放在一起,並使用遊標將其寫入另一個表中,以遍歷所有數據。希望能在這裏找到一些幫助。在存儲過程中使用光標從多個表中寫入表
我加入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
你能寫一個選擇來獲取你需要的所有數據嗎?如果是這樣,那麼一個簡單的'INSERT INTO' ...'SELECT'會在沒有使用遊標的情況下到達那裏。 – SQLChao 2014-11-14 21:13:46
我試過這個,但是我不知道如何從同一張表中拉出多個電話號碼時如何編寫單個選擇。 – MISNole 2014-11-14 21:17:16
這樣一個帳戶可以有多個電話號碼,其中primaryphone = 1?一個帳戶可以有多個電話號碼,其中主要電話<> 1?我假設這是因爲你使用了'TOP 1' – SQLChao 2014-11-14 21:20:18