【发布时间】:2014-11-14 21:09:15
【问题描述】:
我正在努力编写我的第一个过程,将多个表中的数据拉到一起,然后使用游标将其写入另一个表以循环所有数据。希望在这里找到一些帮助。
我将 6 个表连接回主表 Accounts,以显示所需的数据。使用第一个光标,我连接了五个表以获取所需的信息,然后我想再添加两个光标以从添加到表中的电话详细信息表中获取电话号码(主要和次要)。
希望这是有道理的。我确定我在 SQL 中遗漏了一些东西,但基本上我想遍历 Accounts 表并将数据写入一个新表,并遍历 Phone Detail 表并获取每个帐户的主要电话和然后辅助电话(同时考虑 NULL 值)也将其写入新表。
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 PD.AccountID=@AccountID
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 PD.AccountID=@AccountID
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可以让你在不使用游标的情况下到达那里。 -
我已经尝试过了,但是当从同一个表中提取多个电话号码时,我无法弄清楚如何编写单个选择。
-
所以一个帐户可以有多个电话号码,其中primaryphone = 1?一个帐户还可以有多个电话号码,其中primaryphone 1?我假设这是因为您使用了
TOP 1 -
你加入了一张桌子两次。你只需要用两种不同的方式给它起别名。我会加入 PhoneDetail 两次:一次是 PrimaryPhone = 1,另一次是 PrimaryPhone 1。
-
正确,一个帐户可以有多个电话号码,不幸的是,它可以有多个主要电话号码(尽管不应该)。不幸的是,我今天发现了一个案例并纠正了它,但我想让这个故障安全,以防它再次发生。
标签: sql stored-procedures cursor