Update multiple values from temporary table - sql

I'm very puzzled because when I'm running this query on SQLite.
On MacOS Mojave SQLITE, I'm getting a syntax error on the on "FROM". There is no more detail.
This does work on Postgres.
Am I reading the SQLite documentation the wrong way? https://sqlite.org/lang_update.html
Here's the query:
BEGIN;
-- Statement 1
CREATE TEMP TABLE tempEdits (identifier text, serverEditTime double precision);
-- Statement 2
INSERT INTO tempEdits (identifier, serverEditTime)
VALUES
('uuid1', 1.5),
('uuid2', 2.2),
('uuid3', 3.3);
-- Statement 3
UPDATE
"pEdits"
SET
"serverEditTime" = t.serverEditTime
FROM
"pEdits" AS e JOIN tempEdits AS t ON e.identifier = t.identifier
WHERE
e.identifier = t.identifier;
END;
Setup query:
CREATE TABLE "pEdits" (identifier text, serverEditTime double precision);
INSERT INTO (identifier)
VALUES
('uuid1'),
('uuid2'),
('uuid3');

SQLite does not support joins in the UPDATE statement.
Instead you should use a correlated subquery:
UPDATE pEdits
SET serverEditTime = (
SELECT t.serverEditTime
FROM tempEdits AS t
WHERE t.identifier = pEdits.identifier
);
See the demo.
Edit: Starting from version 3.33.0+ (2020-08-14), SQLite supports the Postgresql-like FROM clause. See https://www.sqlite.org/lang_update.html#upfrom

The logic you want is:
UPDATE "pEdits"
SET "serverEditTime" = t.serverEditTime
FROM tempEdits t
WHERE "pEdits".identifier = t.identifier;
In other words, the table being updated should not be repeated in the FROM clause -- well, unless your intention is a self-join.

Related

I am getting Syntax error in sql (insert)

So, I am trying to add a data if a value in field does not exist. I am keep getting syntax error and not sure where I am getting it wrong.
INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
VALUES ('test','010-4843-0000','www.company.com')
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');
This is my code.
I am using H2 database
You're trying to combine a values table constructor with syntax of a select query
You can insert into a table using select:
INSERT INTO COMPANY_TABLE(company_name, company_phone, company_url)
SELECT 'test','010-4843-0000','www.company.com'
WHERE NOT EXISTS (SELECT * FROM COMPANY_TABLE WHERE company_name = 'test');

INSERT row in table a for every row in table b [duplicate]

If I have an SQL table with all default columns (e.g. identity column + any number of columns all with default values), what is the SQL statement to insert a row with no explicit values given?
insert MyTable /* ( doh, no fields! ) */
-- values( doh, no values! )
What's the trick?
This is a part of the INSERT syntax
INSERT INTO TableName DEFAULT VALUES
Read more here:
https://learn.microsoft.com/en-us/sql/t-sql/statements/insert-transact-sql
You can use the DEFAULT keyword.
The accepted answer only works for one row, not for multiple rows.
Let us assume you know how many rows to insert, but you want all default values. You cannot do the following, for instance
INSERT MyTable
SELECT DEFAULT VALUES -- Incorrect syntax near the keyword 'DEFAULT'.
FROM SomeQueryOrView;
-- or
INSERT MyTable
DEFAULT VALUES -- Incorrect syntax near the keyword 'FROM'.
FROM SomeQueryOrView;
Instead we can hack MERGE to do this
MERGE INTO myTable
USING (SELECT SomeValue FROM SomeQueryOrView) s
ON 1 = 0 -- never match
WHEN NOT MATCHED THEN
INSERT DEFAULT VALUES;
A bonus benefit is that we can OUTPUT data from columns which are not being inserted:
MERGE INTO myTable
USING (SELECT SomeValue FROM SomeQueryOrView) s
ON 1 = 0 -- never match
WHEN NOT MATCHED THEN
INSERT DEFAULT VALUES
OUTPUT inserted.Id, s.SomeValue;

How to retrieve data from Table-Function in DB2

I have a Table Function it return a table of (student_id,student_name)
I want to call it and insert the result into another table
I use
INSERT INTO STUDENT_TMP SELECT Table(MyDB.fn_getStudent())
but i did not get the result
I have got an error :
ERROR: DB2 SQL Error: SQLCODE=-390, SQLSTATE=42887,
SQLERRMC=MyDB.AA;SQL131208155041300,DRIVER=3.67.26
Error Code: -390
I found the followin exsample on ibm sites:
select t1.timeid, t1.storeid, t1.sales
from time, store, table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
where time.timeid = t1.timeid and store.storeid = t1.storeid;
notice the syntax: table (cvsample.salesfunc(time.timeid, store.storeid)) as t1
so you prob dont need fields and 'as' you still need '*' and the 'FROM'
so
INSERT INTO STUDENT_TMP SELECT * FROM Table (MyDB.fn_getStudent())

SQL script replacing column's data

I have a PostgreSQL database and I want to make a script that replaces the data of one column of the table A with the data of an other tables column. I've written this PL/PgSQL function:
BEGIN;
CREATE TEMPORARY TABLE tmp_table (id bigint PRIMARY KEY,
registrationnumber character varying(255));
INSERT INTO tmp_table
select id,registrationnumber from tableB;
for d in tmp_table loop
update TABLEA set registrationnumber=d.id where
registrationnumber=d.registrationnumber;
return next d;
end loop;
END;
What is going wrong with my script?
There's no reason to do this in a loop - let the database engine do it for you.
UPDATE tablea
SET registrationnumber = tableb.id
FROM tableb
WHERE tablea.registrationnumber = tableb.registrationnumber;
select * from tablea;
See this SQLFiddle:
http://sqlfiddle.com/#!1/1281b/1
Note that you are implicitly casting a varchar value to a bigint. If any of these varchar values don't cast correctly, the statement will fail.

Informix: Select null problem

Using Informix, I've created a tempory table which I am trying to populate from a select statement. After this, I want to do an update, to populate more fields in the tempory table.
So I'm doing something like;
create temp table _results (group_ser int, item_ser int, restype char(4));
insert into _results (group_ser, item_ser)
select
group_ser, item_ser, null
from
sometable
But you can't select null.
For example;
select first 1 current from systables
works but
select first 1 null from systables
fails!
(Don't get me started on why I can't just do a SQL Server like "select current" with no table specified!)
You don't have to write a stored procedure; you simply have to tell IDS what type the NULL is. Assuming you are not using IDS 7.31 (which does not support any cast notation), you can write:
SELECT NULL::INTEGER FROM dual;
SELECT CAST(NULL AS INTEGER) FROM dual;
And, if you don't have dual as a table (you probably don't), you can do one of a few things:
CREATE SYNONYM dual FOR sysmaster:"informix".sysdual;
The 'sysdual' table was added relatively recently (IDS 11.10, IIRC), so if you are using an older version, it won't exist. The following works with any version of IDS - it's what I use.
-- #(#)$Id: dual.sql,v 2.1 2004/11/01 18:16:32 jleffler Exp $
-- Create table DUAL - structurally equivalent to Oracle's similarly named table.
-- It contains one row of data.
CREATE TABLE dual
(
dummy CHAR(1) DEFAULT 'x' NOT NULL CHECK (dummy = 'x') PRIMARY KEY
) EXTENT SIZE 8 NEXT SIZE 8;
INSERT INTO dual VALUES('x');
REVOKE ALL ON dual FROM PUBLIC;
GRANT SELECT ON dual TO PUBLIC;
Idiomatically, if you are going to SELECT from Systables to get a single row, you should include 'WHERE tabid = 1'; this is the entry for Systables itself, and if it is missing, the fact that your SELECT statement does return any data is the least of your troubles. (I've never seen that as an error, though.)
This page says the reason you can't do that is because "NULL" doesn't have a type. So, the workaround is to create a sproc that simply returns NULL in the type you want.
That sounds like a pretty bad solution to me though. Maybe you could create a variable in your script, set it to null, then select that variable instead? Something like this:
DEFINE dummy INT;
LET dummy = NULL;
SELECT group_ser, item_ser, dummy
FROM sometable
SELECT group_ser, item_ser, replace(null,null) as my_null_column
FROM sometable
or you can use nvl(null,null) to return a null for your select statement.
Is there any reason to go for an actual table? I have been using
select blah from table(set{1})
select blah from table(set{1})
is nice when you are using 10.x database. This statement doesn't touch database. The amount of read/write operations is equal to 0,
but
when you're using 11.x it will cost you at least 4500 buffer reads because this version of Informix creates this table in memory and executes query against it.
select to_date(null) from table;
This works when I want to get a date with null value
You can use this expression (''+1) on the SELECT list, instead of null keyword. It evaluates to NULL value of type DECIMAL(2,0).
This (''+1.0001) evaluates to DECIMAL(16,4). And so on.
If you want DATE type use DATE(''+1) to get null value of type DATE.
(''+1)||' ' evaluates to an empty string of type VARCHAR(1).
To obtain NULL value of type VARCHAR(1) use this expression:
DATE(''+1)||' '
Works in 9.x and 11.x.