Hi i keep getting this error message when i try to execute this sql script: Msg 512, Level 16, State 1, Line 12 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The thing we are trying to do is to duplicate some purchasing list stored at some customers from an old database to a new one, which somehow got lost during the transfer to the new database we made a couple of weeks ago, so we made this script, which is working for a couple of customers but for some it just gets the error code i wrote in bold.
I did find some old answers to this but i couldn't get it quite to work on my own, so i hope some of you could lend me a helping hand, as i cannot duplicate thousands of customer purchasing list manually, that would take some time.
Declare @OldWebuserNo INT
Declare @NewWebuserNo INT
Declare @OldCustNo INT
Declare @ZpiderOrderNo INT
/* Sett kundenummeret som skal endres for*/
SET @OldCustNo = 12001
while @OldCustNo <= 80000
BEGIN
/* Henter ut det nye webusernummeret for kunden */
use ZF0010
select @NewWebuserNo = ( select ZpiderOrder.WebUserNo
from ZF0010.WebUser
join ZF0010.ZpiderOrder On ZF0010.ZpiderOrder.WebUserNo = ZF0010.WebUser.WebuserNo
where CustomerNo = @OldCustNo )
/* Lister ut ordrenummer på kunden med gammelt webusernummer til en midlertidig tabell, og oppdaterer WebuserNo på ordrene*/
use ZF0001
DECLARE @MyCursor CURSOR;
DECLARE @MyField INT;
BEGIN
SET @MyCursor = CURSOR FOR
select ZpiderOrderNo
from ZF0001.ZpiderOrder
join ZF0001.WebUser On ZF0001.ZpiderOrder.WebUserNo= ZF0001.WebUser.WebuserNo
where CustomerNo = @OldCustNo
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @MyField
WHILE @@FETCH_STATUS = 0
BEGIN
update ZF0010.ZF0010.ZpiderOrder
set WebUserNo = @NewWebuserNo
where ZpiderOrderNo = @MyField
FETCH NEXT FROM @MyCursor
INTO @MyField
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END
/* Lister ut ordrene som hører til kunden etter operasjonen er kjørt */
use ZF0010
select ZF0010.ZpiderOrder.WebUserNo, ZF0010.WebUser.CustomerNo, ZpiderOrderNo, ZF0010.WebUser.Name
from ZF0010.ZpiderOrder
join ZF0010.WebUser On ZF0010.ZpiderOrder.WebUserNo= ZF0010.WebUser.WebuserNo
where ZF0010.ZpiderOrder.WebUserNo = @NewWebuserNo
SET @OldCustNo = @OldCustNo + 1
END
'