Insert into dynamic tableName
Posted by:
John Noble
Date: August 17, 2019 10:43AM
Hi folks,
I am struggling trying to insert into a table where the table name is sent to the sproc as a parameter...
DECLARE vLedgerYear varchar(20) default "";
DECLARE vInsert varchar(255) default "";
SET @vLedgerYear = "ledger" + substring(p_period, 1, 2);
SET @vInsert = CONCAT("INSERT INTO ledger19 (credit, reference1,vesselID) VALUES ('", p_credit, "','" , p_reference1, "','" , p_vesselID, "')");
The above works fine.
But I need to make like...
SET @vInsert = CONCAT("INSERT INTO ", @vLedgerYear, " (credit,reference1, vesselID) VALUES ('", p_credit, "','" , p_reference1, "','" , p_vesselID, "')");
Any ideas on how to do this ??
J