How to access tables with same name different case in DB2 - command-line

I create the following two tables that only differs the case:
tables.sql:
CREATE TABLE T1 (C1 INTEGER);
INSERT INTO T1 VALUES (1);
CREATE TABLE "t1" (C1 INTEGER);
INSERT INTO "t1" VALUES (2);
CREATE TABLE T2 (C1 INTEGER, "c1" integer);
INSERT INTO T2 VALUES (3, 4);
Command
db2 -tvf tables.sql
When I want to query the tables, directly from the CLP, I cannot differentiate the two types of case. How can I do a query to table T1 and another to table t1. The same for both columns C1 and c1?

In order to query those tables from CLP in Windows, you have to do:
For table T1 (The simplest one):
db2 select * from T1
or (This is the way in Linux because of the *)
db2 "select * from T1"
For table t1
db2 "select * from ""t1"""
For the columns is similar
db2 select C1 from T2
For the other
db2 "select ""c1"" from T2"
Make sure that the whole command is involve in quotes. If you issue this command:
db2 select ""c1"" from T2
It will return C1 instead. The same for this command:
db2 select * from ""t1""
with T1 being returned.

If you're using CLP on a linux shell, the shell will interpret the quotes and strip them. On Linux / Unix escape the quotes with backslashes
db2 "INSERT INTO \"t1\" VALUES (...)"

Related

Liquibase insert select multiple rows postgres

I want to insert into table1 multiple rows from table2. The problem is that I have some fields in table1 that I want to compute, and some rows that I want to select from table2. For example something like this:
insert into table1 (id, selectField1, selectField2, constant)
values ((gen_random_uuid()), (select superField1 from table2), (select superField2 from table2), 'test');
So the logic is to select superField1 and superField2 from all the rows in the table2 and insert them into table1 with constant value test and generated uids. superField1 and superField2 should be from the same row in table2 when inserting in table1. How can I achieve something like this using liquibase?
P.S: I'm using <sql> tag since it's easier to implement using SQL than using XML changeset, but if you know how to do it in XML that would be appreciated too, but just in SQL will be enough too. DBMS is postgres.
Don't use the VALUES clause if the source is a SELECT statement:
insert into table1 (id, selectField1, selectField2, constant)
select gen_random_uuid(), superField1, superField2, 'test'
from table2;

How to use the same common table expression in two consecutive psql statements?

I'm trying to perform a pretty basic operation with a few steps:
SELECT data from table1
Use id column from my selected table to remove data from table2
Insert the selected table from step 1 into table2
I would imagine that this would work
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
But I'm getting an error saying that temp is not defined during my insert step?
Only other post I found about this is this one but it didn't really answer my question.
Thoughts?
From Postgres documentation:
WITH provides a way to write auxiliary statements for use in a larger
query. These statements, which are often referred to as Common Table
Expressions or CTEs, can be thought of as defining temporary tables
that exist just for one query.
If you need a temp table for more than one query you can do instead:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;

How can I insert union tables to table in PostgreSQL?

I have this query and insert rows to MYSQl database and work perfect.
insert int test(id,user)
select null,user from table2
union
select null,user from table3
But when run the above query in PostgreSQL not work. And I get this error column "id" is of type integer but expression is of type text, But when I run two query below as shown as worked.
When I run below query in PostgreSQL it works properly:
insert into test(id,user)
select null,user from table2
Or below query in PostgreSQL it works properly:
insert int test(id,user)
select null,user from table3
Or below query in PostgreSQL it works properly:
select null,user from table2
union
select null,user from table3
null is not a real value and thus has no data type. The default assumed data type is text, that's where the error message comes from. Just cast the value to int in the first SELECT:
insert into test(id, "user")
select null::int, "user" from table2
union
select null, "user" from table3
Or even better, leave out the id completely so that any default defined for the id column is used. It sounds strange to try and insert null into a column named id
insert into test("user")
select "user" from table2
union
select "user" from table3
Note that user is a reserved keyword and a built-in function, so you will have to quote it to avoid problems. In the long run I recommend to find a different name for that column.

Db2: Query for searching table with different column name

In my database, all tables should have a column (let's say "abc") and I want to find out the tables which do not have this column. Do we have any such query to fulfill this requirement?
Database: Db2 v11.1 LUW
You can build a query against SYSCAT.COLUMNS (and SYSCAT.TABLES) to find those tables not having such column:
select tabname from syscat.tables t1
where not exists
(select colname from syscat.columns c
where c.tabname=t1.tabname and colname='foo')
and tabname like 'SYSX%'
Above is just an example and not optimized.
Non-system tables only. Column name must be in uppercase, unless you specified the column name as “abc” (in double quotes) upon the table creation intentionally.
select tabschema, tabname
from syscat.tables t
where not exists
(
select 1
from syscat.columns c
where c.tabschema=t.tabschema and c.tabname=t.tabname
and c.colname='ABC'
)
and tabschema not like 'SYS%'
and type='T';

Postgresql: Unexpected Insertion Result

Version: PostgreSQL 9.4.2
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('T1_id_seq'::regclass)
name | text |
value | text |
parent_id | integer |
Indexes:
"T1_pkey" PRIMARY KEY, btree (id)
"T1_id_idx" btree (id)
I have two tables like this in Postgresql, say T1 and T2 with tree like data structure referencing data from own table.
I need to modify some rows in T1 and insert it to T2 in the exact order as the rows appeared in T1. What I have done thus far is copy the relevant rows from table T1 to a temporary table T3 for data modification and insert everything from T3 to T2 when changes' made.
T3 is created using
CREATE TABLE T3 (LIKE T1 INCLUDING ALL)
INSERT * INTO T3 SELECT * FROM T1
The end result is rather strange. All the data from T3 were copied to T2, but the order of the ids seems to be random.
However the result is correct if I invoke the same script to copy data from T1 to T3 directly. What is even more bizarre is it's also correct if if I split the above script into two separate script to
Create T3 from T1 and copy data from T1 to T3
Copy T3 to T2 using INSERT method.
Any clues?
You didn't specify an ORDER BY clause. Without one, PostgreSQL might fetch the rows for your SELECT in whatever order happens to be fastest to execute.
Try:
CREATE TABLE T3 (LIKE T1 INCLUDING ALL);
INSERT INTO T3
SELECT * FROM T1 ORDER BY T1.id;
Note that strictly there is no guarantee that the INSERT of multiple rows will process rows in the order they are read from the SELECT, but in practice PostgreSQL at this time will always process them in order and it's not likely to change in a hurry.