I am trying to run a simple insert statement in db2 but getting invalid statement:
my sql :
insert into schema1.table1 select * from schema2.table1
Error:
SQLCODE = -20186, ERROR: A CLAUSE SPECIFIED FOR THE DYNAMIC SQL; STATEMENT BEING PROCESSED IS NOT VALID
I had gone through ibm knowledge center and found this page.
I understand db2 allows 3 forms of insert:
There are three forms of this statement:
The INSERT via VALUES form is used to insert a single row into the table or view using the values provided or referenced.
The INSERT via SELECT form is used to insert one or more rows into the table or view using values from other tables, or views, or both.
The INSERT via FOR n ROWS form is used to insert multiple rows into the table or view using values provided or referenced.
My question is why db2 is looking for "for n rows" clause in my query.
Note: this query is being sent through SSGODBC.
Might want to try and explicitly state the columns.
INSERT INTO Schema1.table1 (col1,col2,...) SELECT col1,col2,... FROM Schema2.table2
How do I write the SQL INSERT command to add the data in the screenshot provided when I have already created a table?
CREATE TABLE Term (Term_Name VARCHAR(25), Term_StartDate DATE, TermEndDate DATE);
Term Table
I've attempted to type in the code, and this is the error that I get; screenshot attached.
Attempt at INSERT
You're very close! You don't want the SELECT to be bundled with the INSERTs.
You simply want the DML (Data Manipulation Language) statements to INSERT the data into the data. So, just use the INSERTS.
Then, use a SELECT statement to obtain the data that you want.
Here is a modified sample of what you did using SQL Fiddle.
How would I insert into multiple tables with one sql script in db2
For example, insert a row into T1 DOCK_DOOR and then insert into T2 DOCK_DOOR_LANE multiple times based on the dock_door_sysid from the first table.
My first approach was the following. I was attempting to use a with with three inserts. on the other hand, doing to inserts on the second table is not and option if this can be automated with one insert.
thanks for any feedback
sql example
WITH ins AS (
INSERT INTO DBF1.DOCK_DOOR (DOCK_DOOR_SYSID,DOOR_NUMBER,DOOR_NAME,DOCK_SYSID,DOOR_SEQ,ENCRYPTION_CODE,RFID_ENBLD_FLAG,LANES_COUNT,CMNT_TEXT,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER,ACTIVE_FLAG,STATUS_SYSID,DOOR_TYPE_SYSID)
VALUES (nextval for DBF1.DOCK_DOOR_SEQ,'026','DOOR025',61,25,NULL,'N','2',NULL,current timestamp,'SQL_INSERT',current timestamp,'SQL_INSERT',0,NULL,1723,1142)
RETURNING door_number,dock_door_sysid),
ins2 AS (
INSERT INTO SIT.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER)
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'A',dock_door_sysid,'A',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR
RETURNING door_number,dock_door_sysid)
INSERT INTO DBF1.DOCK_DOOR_lane (DOCK_DOOR_LANE_SYSID,DOOR_LANE_ID,DOCK_DOOR_SYSID,LANE_ID,CREATE_TS,CREATE_USERID,UPDATE_TS,UPDATE_USERID,VER_NUMBER)
VALUES (nextval for DBF1.DOCK_DOOR_LANE_seq,door_number||''||'B',dock_door_sysid,'B',current timestamp},'SQL_INSERT',current timestamp,'SQL_INSERT',0)
SELECT door_number,dock_door_sysid FROM DBF1.DOCK_DOOR;
Table 1 = dock_door
Table 2 = Dock_door_lane
You could do it with a trigger on the dock_door table.
However, if you're on a recent, version on IBM i. You might be able to make use of data change table reference
Your statement would look something like this
insert into dock_door_lane
select <....>
from final table (insert into dock_door <...>)
I'm not sure it will work, as this article indicates that at least at a couple of years ago DB2 for i didn't support the secondary insert required.
This old SO question also seems to confirm that at least at v7.1, the double insert isn't supported.
If I get a chance, I'll run a test on a 7.2 system Monday.
I'm currently working with DB2 v10.5 and I need to log all SQL Statements that occurr on a specific table (A). For instance, if an INSERT occur on table A, I need to "grab" that SQL Statement and log it into another table (A_LOGGER).
The solution I've reached was to create a TRIGGER (for each CRUD operation) over table A that looks into the table SYSIBMADM.SNAPDYN_SQL and try to save the last executed statement on table A.
Example for the INSERT Statement:
CREATE OR REPLACE TRIGGER OPERATIONS_INSERT_TRIGGER
AFTER INSERT ON REPLDEMO.OPERATIONS
REFERENCING NEW AS OBJ
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
INSERT INTO REPLDEMO.OPERATIONS_LOGGER (LAST_SQL_STATEMENT)
SELECT STMT_TEXT FROM SYSIBMADM.SNAPDYN_SQL
WHERE 1=1
AND STMT_TEXT LIKE 'INSERT INTO REPLDEMO.OPERATIONS (%'
AND STMT_TEXT NOT LIKE '%?%';
END%
But looking at table SYSIBMADM.SNAPDYN_SQL is not the best solution because you cannot guarantee that you'll get the truly last SQL Statement executed on table A. Moreover, if there's a massive number of sql statements executed over table A in a very short period the TRIGGER will replicate many of the statements already saved on A_LOGGER.
So, my question is: Is there an effective and secure way to get the last SQL Statement executed over a table?
Thanks.
I have problem. I need trigger after insert. But trigger have to insert few rows depends on select from other table.
is it possibe in sql?
Update:
ok, sorry.
Example (no-logic)
tables:
users(id,name,type_user)
type_user(id,type)
items(id,name,type_user) - some type of users can posses only few items users_items(id,item_id,user_id)
And when i insert into users i want to insert into users_items all items which user can posses
Yes it is possible. Inside SQL Triggers you can do Insert statements.
You can find SQL Trigger information
here.
Try something like
CREATE TRIGGER RelateItems
ON User
AFTER INSERT
AS
--INSERT STATEMENT HERE - INSERT INTO users_items
GO