I have the following code in a stored procedure, in essence it creates a temp table to hold some data, it then runs two update statements on a main table with details from the temp table and another table, all the values in the CIG_URN field are unique (I have already stripped out the duplicates via another SP running before this one, however I am getting the following error:-
Msg 512, Level 16, State 1, Procedure Full_Data_Load_Address, Line 54
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Msg 512, Level 16, State 1, Procedure Full_Data_Load_Address, Line 67
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
I can't see why, any chance anyone can see what I am missing?
Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.
Msg 512, Level 16, State 1, Procedure Full_Data_Load_Address, Line 54
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Msg 512, Level 16, State 1, Procedure Full_Data_Load_Address, Line 67
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
I can't see why, any chance anyone can see what I am missing?
Code:
Create table ##ClientID
(CIG_URN varchar(50),
PoBox varchar(50),
Abode varchar(50),
BName varchar(50),
BNumber varchar(50),
Street1 varchar(50),
Street2 varchar(50),
Sublocality varchar(50),
Locality varchar(50),
Town varchar(50),
Postcode varchar(50))
Insert INTO ##ClientID (CIG_Urn,POBox,Abode,BName,BNumber,Street1,Street2,SubLocality,Locality,Town,Postcode)
Select Distinct CIG_URN,POBox,Abode,BName,BNumber,Street1,Street2,SubLocality,Locality,Town,Postcode
from Call_Trace_Import where AddressLink = 'Y' and SequenceNo = '1' and CIG_URN is not NULL
SET NOCOUNT ON;
/* First, declare items in the cursor */
DECLARE @cur_CustomerID varchar (50),
@POBox varchar (50),
@Abode varchar (50),
@BName varchar (50),
@BNumber varchar (50),
@Street1 varchar (50),
@Street2 varchar (50),
@Sublocality varchar (50),
@Locality varchar (50),
@Town varchar (50),
@Postcode varchar (50)
/* Next, declare the cursor itself */
DECLARE changes_cursor cursor
for SELECT Distinct CIG_Urn,POBox,Abode,BName,BNumber,Street1,Street2,SubLocality,Locality,Town,Postcode
FROM ##ClientID
OPEN changes_cursor
/* first fetch starts the process */
FETCH changes_cursor INTO @cur_CustomerID,@POBox,@Abode,@BName,@BNumber,@Street1,@Street2,@Sublocality,
@Locality,@Town,@Postcode
WHILE @@fetch_status = 0 /* while there's still records to process */
BEGIN
UPDATE dbo.Call_Trace_Import
SET Previous_Address1 = @PoBox,
Previous_Address2 = @Abode,
Previous_Address3 = @BName,
Previous_Address4 = @BNumber,
Previous_Address5 = @Street1,
Previous_Address6 = @Street2,
Previous_Address7 = @Sublocality,
Previous_Address8 = @Locality,
Previous_Address9 = @Town,
Previous_Postcode = @Postcode
WHERE @cur_CustomerID = (Select Distinct CIG_URN from Call_Trace_Import)
UPDATE dbo.Call_Trace_Import
SET PoBox = (SELECT distinct n.PoBox FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Abode = (SELECT distinct n.Abode FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
BName = (SELECT distinct n.BName FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
BNumber = (SELECT distinct n.BNumber FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Street1 = (SELECT distinct n.Street1 FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Street2 = (SELECT distinct n.Street2 FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Sublocality = (SELECT distinct n.Sublocality FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Locality = (SELECT distinct n.Locality FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Town = (SELECT distinct n.Town FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN),
Postcode = (SELECT distinct n.Postcode FROM dbo.Call_Trace_Output n,Call_Trace_import c WHERE c.CIG_URN = n.CIG_URN)
WHERE @cur_CustomerID = CIG_URN and SequenceNo = '1'
Delete from ##ClientID where CIG_URN = @cur_CustomerID
/* keep fetching till done */
FETCH changes_cursor INTO @cur_CustomerID,@POBox,@Abode,@BName,@BNumber,@Street1,@Street2,@Sublocality,
@Locality,@Town,@Postcode
END
CLOSE changes_cursor
DEALLOCATE changes_cursor
Drop Table ##ClientID
END
Didn't anyone ever tell you? There's one thing you never put in a trap — if you're smart, if you value your continued existence, if you have any plans about seeing tomorrow — there's one thing you never — ever, put in a trap. … Me.