Drop table that has the name in lower case from the command line - db2

I need to delete a table that has a lower case name, i.e. academy
However when I do db2 drop table academy or db2 drop table "academy" I get:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0204N "DB2INST1.ACADEMY" is an undefined name. SQLSTATE=42704
The same command worked for upper case table names though.
When I list my tables I have > db2 LIST TABLES
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
AA DB2INST1 T 2016-06-07-14.23.08.927146
MYNEWTABLE DB2INST1 T 2016-06-07-14.29.50.859806
academy DB2INST1 T 2016-06-07-17.05.27.510905

In db2 drop table "academy" quotes get swallowed by the shell. You'll need to escape them:
db2 drop table \"academy\"
or quote the entire statement:
db2 'drop table "academy"'

Try doing a select * from "academy" and see if it will even call the table. If it does you should be able to run the same query again, only replace the word "select" with "drop".

Related

Postgresql search_path issue

Based on past experience and documentation related to search_path:
"The first matching table in the search path is taken to be the one wanted. If there is no match in the search path, an error is reported, even if matching table names exist in other schemas in the database."
But when I run this:
financialdw=> show search_path;
search_path
------------------------------------------------------
"$user, prismdms, prism_nonrestrict, prism_restrict"
(1 row)
financialdw=> select count(*) from addr;
ERROR: relation "addr" does not exist
LINE 1: select count(*) from addr;
^
financialdw=> select count(*) from prismdms.addr;
count
-------
24428
(1 row)
prismdms is in my search_path and I have the necessary permissions on all tables within it. Shouldn't I be able, as the document states, query the table without the schema name qualifying it?
Your search_path has a single schema in it, and that single schema has the long and unlikely name of $user, prismdms, prism_nonrestrict, prism_restrict.
The double quotes should just be around $user, not the entire thing.

DB2 load fails with SQL3304N The table does not exist

I have created 2 tables in db2 11.5 LUW :
CREATE TABLE test.TABLE1(ONE INT,
TWO CHAR(10),
THREE DATE);
CREATE TABLE test.TABLE2(ONE INT,
TWO CHAR(10),
THREE DATE);
Both tables are selectable:
db2 => select * from test.table1;
ONE TWO THREE
----------- ---------- ----------
0 record(s) selected.
db2 => select * from test.table2;
ONE TWO THREE
----------- ---------- ----------
0 record(s) selected.
Trying to run load (even on empty tables):
db2 => DECLARE mycurs CURSOR FOR SELECT ONE, TWO, THREE FROM test.table1;
DB20000I The SQL command completed successfully.
db2 => LOAD FROM mycurs OF cursor INSERT INTO test.table2;
SQL3304N The table does not exist.
Any ideas why it happens?
I found the reason: it was the trailing semicolon.
This one fails - mind that the command ends with ;
db2 => LOAD FROM mycurs OF cursor INSERT INTO test.table2;
SQL3304N The table does not exist.
But, this one works - mind that the command does not end with ;
db2 => LOAD FROM mycurs OF cursor INSERT INTO test.table2
SQL3501W The table space(s) in which the table resides will not be placed in
backup pending state since forward recovery is disabled for the database.
SQL1193I The utility is beginning to load data from the SQL statement "
select * from test.table2".
...
I am used to oracle syntax , where ; is expected in the end of any command. I guess this is not the case in db2.

How to access table values in command line processor plus of db2?

I have recently installed db2 in my system and created the sample database which has 47 tables. I tried to retrieve data from the tables using clp plus.
used connect db2admin#localhost:50000/sample and gave password. it gave back database connection information.
when i use select * from employee (employee is a table in sample) iam getting 2.
what is this 2 how to check data present in the tables.
The reason that you get 2 is that you did not terminate the query with a semi-colon, so clpplus thinks that you want to enter line-2 (the second line) of the query and it displays that line number and waits for you to enter more text.
When you want to indicate that the statement is complete, enter a statement terminator character.
The default statement terminator is a semi-colon (;) so inside clpplus you should use:
select * from employee ;

SQL to querying a table with a dollar sign

I'm using the Looker Dashboarding software (see: looker.com). It creates temporary tables in your database's looker_scratch schema with long names, each containing a dollar symbol.
These are straightforward to query using the "SQL Runner" in Looker itself, which somehow is able to escape the dollar symbol, but I can't query them using a 3rd-party SQL client.
I'm trying to query this table:
SELECT *
FROM looker_scratch.LR$5UA5D3XQDBPAYU0Q9FLFE_test
but get the error:
the # of binded parameters < the # of parameter markers
How can I query the table?
I've tried:
...FROM looker_scratch."LR$5UA5D3XQDBPAYU0Q9FLFE_test" - says the relation does not exist
...FROM looker_scratch."LR\$5UA5D3XQDBPAYU0Q9FLFE_test" - says the relation does not exist
...FROM looker_scratch.$LR\$5UA5D3XQDBPAYU0Q9FLFE_test$ - says syntax error
...FROM looker_scratch.$$LR\$5UA5D3XQDBPAYU0Q9FLFE_test$$ - says syntax error
...FROM looker_scratch.E'LR\$5UA5D3XQDBPAYU0Q9FLFE_test' - says syntax error
try selecting exact identifier by pattern:
select oid::regclass from pg_class where relname ilike '%5ua5d%';
E.g:
so=# create table t."WeirdMix$" ();
CREATE TABLE
Time: 55.750 ms
so=# select oid::regclass from pg_class where relname ilike '%mix%';
oid
---------------
t."WeirdMix$"
(1 row)
Time: 90.814 ms

How to use a subquery as a database name in a DDL command?

I am wondering if it's possible to use the result of a subquery as database name in a PostgreSQL (9.5.1) DDL statement.
For example, I wanted to alter the current database with something like:
ALTER DATABASE (SELECT current_database()) SET a_var TO 'a_value';
If I run this, an error occurs:
ERROR: syntax error at or near "("
LINE 1: ALTER DATABASE (SELECT current_database()) SET ...
What's the correct way to use the sub-query (if possible)?
You need dynamic SQL for that:
DO
$do$
BEGIN
EXECUTE format($f$ALTER DATABASE %I SET x.a_var TO 'a_value'$f$, current_database());
END
$do$;
Using format() to escape the db name safely while being at it.
BTW, to unset:
ALTER DATABASE your_db RESET x.a_var;
To see the current setting:
SELECT current_setting('x.a_var');
(The DB default is not active before you start a new session.)
Related:
Table name as a PostgreSQL function parameter
Error when setting n_distinct using a plpgsql variable