DB2 Temp Tables: Not storing or not retrieving information - sql

I'm an MSSQL guy, but I'm working on a DB2 query that needs to create a temp table, insert into it, and do stuff with it. As a much-shortened test, I'm using the following query, which is providing the same result..
declare global temporary table tt_testingSyntax (id int);
insert into session.tt_testingSyntax (id) values (1);
insert into session.tt_testingSyntax (id) values (2);
insert into session.tt_testingSyntax (id) values (3);
insert into session.tt_testingSyntax (id) values (4);
select * from session.tt_testingSyntax;
Zero rows are returned. Why would that be? I've created the tablespace and verified the table is in scope throughout the query.

Try:
declare global temporary table tt_testingSyntax (id int)
ON COMMIT PRESERVE ROWS NOT LOGGED;
insert into session.tt_testingSyntax (id) values (1);
insert into session.tt_testingSyntax (id) values (2);
insert into session.tt_testingSyntax (id) values (3);
insert into session.tt_testingSyntax (id) values (4);
select * from session.tt_testingSyntax;
There are two options...ON COMMIT DELETE ROWS (the default) or ON COMMIT PRESERVE ROWS.

I ended up unknowingly having access to create my own tables (i.e. for user X, I could create X.temp1). Since this query need only be run once, this works fine. Thanks.

Related

INSERT unless date is the same

Is it possible (in Postgres) to do the following 2 INSERTs, or something that's logically equivalent (the proposed INSERTs don't work as they are, but maybe they can be slightly modified?):
CREATE TABLE IF NOT EXISTS table01(
userid int8 NOT NULL,
save date NOT NULL,
followers int4
);
CREATE UNIQUE INDEX ON table01 (userid,save);
INSERT 0:
INSERT INTO table01 (userid,save,followers) VALUES (%s,%s,%s)
ON CONFLICT (userid) DO INSERT INTO table01 (userid,save,followers) VALUES (%s,%s,%s)
WHERE table01.save!=save;
INSERT 1:
INSERT INTO table01 (userid,save,followers) VALUES (%s,%s,%s) WHERE table01.save!=save;
The logic is:
Try to insert a row
If there's a conflict of userid, then insert the row anyway UNLESS the date (save) is the same
Summary:
Are the 2 shown INSERTs (or something equivalent) possible?
Is it possible to do ON CONFLICT DO INSERT (just like one does ON CONFLICT DO UPDATE)?
Is it possible to do INSERT INTO WHERE (just like one does SELECT FROM WHERE)?
A simple insert would seem to do what you want:
INSERT INTO table01 (userid, save, followers)
VALUES (%s, %s, %s);
This will insert a new row unless the userid/save pair is already there. In that case, it would generate an error. If you don't want an error, you can use on conflict do nothing:
INSERT INTO table01 (userid, save, followers)
VALUES (%s, %s, %s)
ON CONFLICT (userid, save) DO NOTHING;

SQL error for creating table and inserting values in it. Error -12233 and 12101

So I'm trying to create a table in SQL and then insert values into it. However, I seem to be getting this error:
[Error Code: -12101, SQL State: 42000] Syntax error, 'CHECK' assumed
missing
and
[Error Code: -12233, SQL State: 42000] The number of insert values is
not the same as the number of object columns
Here is my SQL code:
CREATE TABLE Server(
Nummer INTEGER NOT NULL
PRIMARY KEY(Nummer)
);
INSERT INTO Server(Nummer)
VALUES (1,2,3,4,5);
So I want to create a table named Server which has a primary key named nummer. Nummer then has the values 1,2,3,4,5
UPDATE--------------------------------------------------------------------
So my new code is:
CREATE TABLE Server(
Nummer INTEGER NOT NULL,
PRIMARY KEY(Nummer),
);
INSERT INTO Server(Nummer)
VALUES (1);
INSERT INTO Server(Nummer)
VALUES (2);
INSERT INTO Server(Nummer)
VALUES (3);
INSERT INTO Server(Nummer)
VALUES (4);
INSERT INTO Server(Nummer)
VALUES(5);
I solved the check problem by simply putting a comma after every statement in the create section.
But I got a new problem which is this error code:
[Error Code: -12101, SQL State: 42000] Syntax error, IDENTIFIER
IDENTIFIER assumed missing
You have a table with a single colums so you can use multiple insert as
INSERT INTO Server(Nummer)
VALUES (1);
INSERT INTO Server(Nummer)
VALUES (2);
.....
OR If you want batch insert you should use this way
INSERT INTO Server(Nummer)
VALUES (1),(2),(3),(4),(5);
You can't insert a list of values like that. You have to create 5 insert clauses. If you're using Sql server, you can create one insert clause, with five values.

Using ##identity for consecutive inserts

I have this situation,
INSERT INTO TABLE1()...
--Get the primary key from the above insert
SELECT ##identidy
INSERT INTO TABLE2()...
The auto generated primary key has to be a foreign key in TABLE 2. How can I construct my second INSERT to have the value of ##identity?
This doesn't seem to work,
INSERT INTO TABLE1 (user_id, name) (##identity, 'ABC')
I get an error saying Must declare variable '##identidy'.
Cheers!!
1) you spelled ##identity wrong (##identidy)
2) You should create a local variable (#LastIdentity) to store the last inserted identity immediately after the first insert. Then use that variable as the input to the second INSERT:
DECLARE #LastIdentity int
INSERT INTO TABLE1()...
--Get the primary key from the above insert
SELECT #LastIdentity = ##identity
INSERT INTO TABLE2(...) VALUES (#LastIdentity, ...

Import from .sql file Oracle SQL Developer excluding duplicates

I have exported a table from one database using Oracle SQL Developer as .sql file and want to import it into another database that has exactly the same table, but the problem is that some entries appear in both tables.
Is there a way when importing using Oracle SQL Developer to exclude entries that already exist in the destination table?
There are at least two ways:
You can use the SQLLDR tool or external table to load
You can load your sql file into the database and then remove duplicates.
Let's use this file:
data.sql
insert into test_table (id) values (1);
insert into test_table (id) values (1);
insert into test_table (id) values (2);
insert into test_table (id) values (2);
insert into test_table (id) values (2);
insert into test_table (id) values (3);
insert into test_table (id) values (3);
SQLLDR:
At first you create integrity constraints for your table for example:
SQL> create table test_table (
2 id number(10) primary key
3 );
Table created.
Then create a control file:
LOAD DATA
INFILE data.sql
INTO TABLE test_table
(
id position(37:37)
)
After running you will see:
SQL> select * from test_table;
ID
----------
1
2
3
And the bad file (This lines were rejected because of the integrity violation):
data.bad
insert into test_table (id) values (1);
insert into test_table (id) values (2);
insert into test_table (id) values (2);
insert into test_table (id) values (3);
You can generate the external table using this control file, so I don't show you how to use it.
Load and remove duplicates:
Let's recreate the test_table table:
SQL> create table test_table (
2 id number(10)
3 );
Table created.
Using SQL Developer load your sql file and check the content:
SQL> select * from test_table;
ID
----------
1
1
2
2
2
3
3
7 rows selected.
and then remove duplicates:
SQL> delete test_table where rowid not in (select min(rowid) from test_table group by id);
4 rows deleted.
SQL> select * from test_table;
ID
----------
1
2
3
I believe there is a way more ways to complete your tasks, I showed only ways that came in my head right away.

how to assign null to a field in sqlite

I would like to assign null to a field in SQLite but am not getting anywhere with this:
update t set n=null where n=0;
I can not reproduce your problem. From what you write, I guess your table looks like this:
CREATE TABLE t (n integer);
Inserting data:
insert into t values (1);
insert into t values (2);
insert into t values (3);
insert into t values (0);
Updating the data with your UPDATE:
update t set n = null where n = 0;
Now the table looks like this:
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t (n integer);
INSERT INTO "t" VALUES(1);
INSERT INTO "t" VALUES(2);
INSERT INTO "t" VALUES(3);
INSERT INTO "t" VALUES(NULL);
COMMIT;
There might be no output after the UPDATE but it has the desired effect.