INSERT and OUTPUT - sql

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.

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

SQL INSERT is not working with OUTPUT INSERTED statement

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.

What is wrong with the syntax of this sqlite code?

My SQLite SQL code looks like this;
INSERT OR IGNORE INTO box_infos (symbol, name)
VALUES( 'DXSS.SI', 'DXSS Group')
UPDATE box_infos
SET name = 'DXSS Group'
WHERE symbol = 'DXSS.SI';
However, I get
Syntax error near "UPDATE"
What is wrong with the code? I used the answer below as a guide.
https://code.i-harness.com/en/q/377728
"Chained" SQLite commands each need to be terminated by ';'. In this case, SQLite does not know where the INSERT ends and the UPDATE begins. All things being equal, this should work, as long as the INSERT query is terminated with ;.

SQL query regarding Sequence

I made a sequence in Net Beans. But when I tried to insert data in the table I get an error.
My code is:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');
The error is:
[Exception, Error code 30,000, SQLState 42X04] Column
'SEQ_PERSON.NEXTVAL' is either not in any table in the FROM list or
appears within a join specification and is outside the scope of the
join specification or appears in a HAVING clause and is not in the
GROUP BY list. If this is a CREATE or ALTER TABLE statement then
'SEQ_PERSON.NEXTVAL' is not a column in the target table.
The error message Error code 30,000, SQLState 42X04 indicates you are using Derby DB not Oracle. That being the case you need to use Derby syntax for getting the next value. So your insert should look like this:
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (NEXT VALUE FOR seq_person,'Lars','Monsen')

OPENQUERY update on linked server

I want to execute the following statement through from a linked server (openquery):
UPDATE SAP_PLANT
SET (OWNER, OWNER_COUNTRY) = (SELECT import.AFNAME, import.COUNTRY
FROM SAP_IMPORT_CUSTOMERS import, SAP_PLANT plant
WHERE plant.SAP_FL = import.SAP_NO
AND import.role ='OWNER')
I've tried to form it into the following syntax, without success :(
update openquery(‘my_linked_server, ‘select column_1, column_2 from table_schema.table_name where pk = pk_value’)
set column_1 = ‘my_value1′, column_2 = ‘my_value2′
I hope for you this is no problem?
I guess this is not really a query you want to open, rather an SQL statement you want to execute. So instead of openquery, you shoud use execute. See example G here: http://msdn.microsoft.com/en-us/library/ms188332.aspx
So your script shoul look like
execute ('your sql command here') at my_linked_server
Are you getting syntax error? Your server parameter in the update openquery is missing a trailing quote. Change ```my_linked_servertomy_linked_server'`.