SQL to querying a table with a dollar sign - postgresql

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

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.

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

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".

Executing dynamic SQL in Postgres 8.2

I am trying to create a table inside a function using dynamic SQL and immediately copy it into another table.
execute 'create table week_temp as
select w.*, ww.*
from employer_weekly w
left join employer_weekly_' || $1 || '_2 ww
on w.w_employer::int = ww.emp_' || $1 || '::int';
drop table if exists employer_weekly;
create table employer_weekly as select * from week_temp;
I am receiving the following error:
Error : ERROR: relation with OID 9288742 does not exist
CONTEXT: SQL statement "create table employer_weekly as select * from
week_temp"
Checking manually, I can see week_temp and can access it correctly.
Would appreciate any clues!
What #a_horse_with_no_name said.
But - the problem you are seeing is that the non-dynamic statements in your function are being compiled. This means the "FROM week_temp" is looking at an older version of week_temp that no longer exists. Make that statement dynamic SQL too and it should work.

postgresql error when attempting to do a select all from table

I am trying to do the postgresql equivalent of mysql select * from table on a postgresql specific database. I can find the name of the table I need within that database when I do:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
But when I try a select all on the table, I get:
SELECT * from Sample;
SELECT * from Sample;
ERROR: relation "sample" does not exist
LINE 1: SELECT * from Sample;
^
Any ideas?
Postgresql is case sensitive.
I usually use all lower char for field, tables and functions.
Anyway, you can double quote them.
To full answer your question and see why and when useing quote, i suggest to read this specific section:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
The manes of the tables are stored in information_schema.tables so you have to see your table by using select * from information_schema.tables. IF your table_schema is "public"
try for a table select select * from public.sample, is your table_schema a differed schema chance it to the right one.
this link will help you Psotgresql doc
I tried single-quoting 'Sample' and it didn't work. Fixed by double-quoting "Sample".

ERROR: relation "stg_data_bt_sur" does not exist

I've created a table in postgresql which is OK and I'm able to do select/insert using SQL manager tool or Navicat Lite tool.
But, when I'm trying to make simple select from LINUX(ubuntu) I have following message:
postgres=# select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist
STATEMENT: select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist.
I also used table name with double quotes - same result.
Any idea what's the issue?
Chances are the schema isn't in your search path, Try \dn to list namespaces and then you can either add the schema like:
SELECT * from "schema"."table";
Or you can set your search path:
SET search_path="schema";
SELECT * FROM "table";
RESET search_path;