on 03-15-2016 8:51 PM
I've been scratching my head (and a few other parts) for a while - maybe still brain dead from the clock forward thing 🙂
Need to insert values where one of the fields is an ID number selected from another table, a bit like this
(SELECT Customer_ID FROM Customers WHERE Cust_Name LIKE '%Unique String%') AS Cust_ID
INSERT INTO A_Sales_Ledger(xxx, yyyy, Customer_ID) VALUES (yyyy, yyyy, Cust_ID)
So the select will return, say, 65 records and it will insert 65 new rows into A_Sales_Ledger with the corresponding Customer_ID
Thanks
Hello Dave,
Turn it on its head and massage the syntax a little bit......
INSERT INTO A_Sales_Ledger(xxx, yyyy, Customer_ID)
SELECT xxx, yyy, Customer_ID FROM Customers WHERE Cust_Name LIKE '%Unique String%';
HTH, Paul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Dave,
No, you don't need VALUES if you use the INSERT...SELECT.. syntax as the inserting values are provided from the select. If you use the values syntax you are going to supply the data 'by hand', usually from literals or variables.
It depends on the shape of the pear but you could try something along the lines of....
BEGIN
DECLARE varSaveCode INTEGER;
INSERT INTO A_Sales_Ledger(xxx, yyyy, Customer_ID)
SELECT xxx, yyy, Customer_ID FROM Customers WHERE Cust_Name LIKE '%Unique String%';
SET varSaveCode = SQLCode;
IF varSaveCode <> 0 THEN
-- Some appropriate error handling, maybe....
MESSAGE 'Oops - a Nashi happened, code '||varSaveCode TO CONSOLE;
ROLLBACK;
ELSE
COMMIT;
END IF;
END
-- NB, untested code - I think MESSAGE can cope with the mix of types
(You could try MESSAGE to CLIENT - it depends on the ability of your client software to trap the message.)
HTH, Paul
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.