SQL INSERT is not working with OUTPUT INSERTED statement - sql

My goal is to insert a new row in a database table and immediately get back the ID of the inserted row.
I saw some other posts on stackoverflow that suggested to use this command:
INSERT INTO selecttable (name) OUTPUT INSERTED.ID VALUES ('aName')
Unfortunately, the execution of this command fails with the following error message:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTPUT INSERTED.ID VALUES ('aName')'
The execution of the command without the output statement worked well:
INSERT INTO selecttable (name) VALUES ('aName')
Do you have an idea, why the command above is not working?

In general, I think you use LAST_INSERT_ID():
INSERT INTO selecttable (name)
VALUES ('aName');
SELECT LAST_INSERT_ID();
This is an imperfect equivalent construct. For instance, it doesn't work so well with multiple rows. But it is the MariaDB/MySQL solution to this problem.

Related

SQL Error: ORA-00933: SQL command not properly ended. When using a simple insert

When trying to execute the following command through my ASP.NET application, I get an error
SQL Error: ORA-00933: SQL command not properly ended
The statement is:
INSERT INTO SOMETABLE (GROUPID, USERID, REMOVED)
VALUES ('00000000000000000000000000000000', '00000000000000000000000000000000', 0);
However, if I run the exact same statement directly against the DB using SQL Developer, it works.
All of the articles I've read about this involve statements using additional criteria like order by, join, etc.
My connection strings are correct, and other statements are executing fine.
I'm feeding the statement to the database using this code:
string command = "insert into SOMETABLE (GROUPID, USERID, REMOVED) values ('00000000000000000000000000000000', '00000000000000000000000000000000', 0);";
int result = context.Database.ExecuteSqlCommand(command);
Why is this happening, and how can I fix it?
Remove semi-colon, here:
string command = "insert into SOMETABLE (GROUPID, USERID, REMOVED) values
('00000000000000000000000000000000', '00000000000000000000000000000000', 0);";
^
|
here

INSERT and OUTPUT

I'm executing a very simple SQL statement, which I have below:
INSERT INTO pictures OUTPUT Inserted.id DEFAULT VALUES
I feel that it should work, but for some reason I can't get this simple error. It prints the following:
OperationalError: near "OUTPUT": syntax error
I'm using sqlite
In SQLITE you need to use last_insert_rowid() function to get the last inserted ID like -
SELECT last_insert_rowid()
Demo.

INSERT error in Oracle

I am working with oracle (with Toad) but when I try to execute this query
INSERT INTO KEYUSER(NAME) VALUES(UNAME) WHERE ID = 1;
I get this error:
SQL command not properly ended. Found 'WHERE' expecting ;-or- LOG -or- RETURN RETURNING
What is wrong? Regards.
You can never use where clause in insert statement. It does not make any sense to have it while you are simply trying to insert a row.
General Insert statement is like below
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
So in your case you have to remove where clause from your statement and query will be like below
INSERT INTO KEYUSER(NAME) VALUES('UNAME');
Presumably, you intend an UPDATE rather than INSERT:
UPDATE KEYUSER
SET NAME = UNAME
WHERE ID = 1;
Sometimes new users of SQL get confused between INSERT and UPDATE. INSERT inserts new rows into a table. UPDATE changes values in existing rows. The confusion is become some people interpret INSERT as "inserting new values into a row".

Triggers in SQL

I am having trouble with the following problem, Just wondering if you could give me any guidance on how i would start this.
Write a trigger to capture an input before it's inserted into a table
so that the value of the insert will be modified to be appended with
the text -previous
Say I have a table called StaffDetails and i could insert info such as ID, How would i apply it to the above problem.
Thank You
Create Trigger trg_name
on StaffDetails
instead of insert
referencing new as new_row
as
begin
#new_row.ID = (#new_row.ID + '_previous')
insert into StaffDetails (.....) values (#new_row.ID, .....)
end
it would probably look something like this, maybe youll have to change some syntax because its not the same for mysql and sql server, for detailed information on syntax for sql server you can look at https://msdn.microsoft.com/en-us/library/ms189799.aspx

Semi Colon issue in Apache Derby

What i want is to store java statement with a semi colon in my embedded derby database. However i am not quite able to generate the simple SQL insert statement that would work. I believe that when the control encounters the semi colon statement in the string if considers it as the termination character and errors are thrown. Try the below:
create table dummy(keys int, vals varchar(255));
insert into dummy values (10,'System.out.println();');
The above statement fails with errors. However if i remove the semi colon it works
insert into dummy values (10,'System.out.println()');
Has anyone seen this before if so which escape character did you use.
Thanks.
It may help to see what context you are trying to execute the sql statement in, i.e. the surrounding code. If this is in a java program, something like this may work:
int n = stmt.executeUpdate("insert into dummy values (10, 'System.out.println()$;') {ESCAPE '$'}");
Your statements work fine when I run them using Derby's "ij" sql editor:
ij version 10.12
ij> connect 'jdbc:derby:tt;create=true';
ij> create table dummy(keys int, vals varchar(255));
0 rows inserted/updated/deleted
ij> insert into dummy values (10,'System.out.println();');
1 row inserted/updated/deleted
ij> select * from dummy;
KEYS |VALS
--------------------------------------------------------------------------------
------------------------------------------------------------
10 |System.out.println();
1 row selected
Perhaps the problem lies elsewhere. If you could include more details about the exact message you get, that might help.