I need to make changes to database in bulk from information provided on a spreadsheet.
At the moment the Table Variable values are added manually, but the hope is to do this automatically from an existing Excel front end (It's a Legacy Application that isn't worth completely re-developing)
I've created the script so that Amendments to a table in SQL can be done in one Execute, the problem I ave is checking where the code should insert a new record to the database.
With amendments there is an Old Ref which is updated to a New Ref, so all columns of the Table variable contain values.
With New records there is only a New Ref so the Old Ref will be Blank, 0 or Null (whichever it needs to be for the code.)
Here is what I have so far: (I've substituted in a 'Print' for each branch rather than showing the rest of the code which is quite long.
@T_VAR TABLE (
OldRef int,
NewRef int,
Name varchar(255),
CustGp varchar(55),
DelGp varchar(55),
)
---BULK INSERT VALUES TO TABLE VARIABLE---
INSERT INTO @T_VAR (
OldRef,
NewRef,
Name,
CustGp,
DelGp
)
Values
--------//**ENTER VALUES HERE**//--------
(1001,2010,'TestUpdate01','Group 1','Delivery 1'),
(NULL,2012,'TestUpdate02','Group 2','Delivery 1'),
(1547,2018,'TestUpdate03','Group 1','Delivery 3'),
(NULL,2022,'TestUpdate04','Group 3','Delivery 1'),
(1752,2050,'TestUpdate05','Group 1','Delivery 2')
-----CHECK IF VARIABLE OldREF IS 0/NULL (Is this a New Record or an Amendment?)----
IF (Select OldRef from @T_VAR) IS NULL
Print 'Follow script to Insert New Record'
else
print 'Follow script to Amend Existing Record'
When running this I get the following error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The code needs to go through each row and then divert it down the correct path- If this is possible.
I know the rest of the code works for just amendments as it's been tested.
Any help that can be given in resolving this would be very much appreciated.