How to run Postgres psql "slash" commands using Sequel - ruby

Using Sequel, is it possible to run PostgreSQL "slash" commands, such as \d, \dn or \copy table...?
I tried using
DB.run(‘\dn’)
but I got a syntax error, because, presumably Sequel is trying to parse the SQL, or Postgres can’t understand the SQL, because these are commands that are handled by the psql application.

The slash commands are features of psql, not the server so they're only accessible in psql. However, some of them (such as \copy) have equivalents that the server does understand and the rest are shortcuts to queries of the system tables. If you run psql -E:
-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN to on.
then you can see the queries and you should be able to use those queries with DB.run or DB.fetch(...).all. For example:
$ psql -E -d...
=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
=# \dt
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************

Related

ORA 00907 Missing Right Parenthesis

I am getting this Error of "Missing Right Parenthesis" on the Query below. Can someone rectify the mistake please?
SELECT
DESCRIPTION,
TOTAL_ADMISSION,
EMERGENCY,
NORMAL
FROM TABLE (ORDERENTRY.PKG_S04REP00031.ADMISSION_SUMMARY(P_START_DATE = to_date('31/08/2021', 'dd/mm/yyyy')
P_END_DATE = to_date('01/09/2021', 'dd/mm/yyyy')
P_ADMISSION_TYPE = 'NORMAL'
/*P_ORDER_LOCATION_ID => :P_ORDER_LOCATION_ID, */
P_LOCATION_ID = 'K01'));
This should work
SELECT
DESCRIPTION,
TOTAL_ADMISSION,
EMERGENCY,
NORMAL
FROM TABLE (ORDERENTRY.PKG_S04REP00031.ADMISSION_SUMMARY(P_START_DATE => to_date('31/08/2021', 'dd/mm/yyyy') ,
P_END_DATE => to_date('01/09/2021', 'dd/mm/yyyy') ,
P_ADMISSION_TYPE => 'NORMAL' ,
--P_ORDER_LOCATION_ID => :P_ORDER_LOCATION_ID,
P_LOCATION_ID => 'K01')
);
You are using select from table(package.function, therefore
Parameters values in functions or packages follow the sign => not =
Parameters are separated by comma
I replaced /* */ just for --, just for cosmetic purposes, as both represent comments, but the later is more common when you want to comment just one line

How to fix ORA-12801 error in SQL Developer when display tables in connection browser

Using SQL Developer 18.4 MacOSX, I'm getting this error
ORA-12801: error signaled in parallel query server P000
ORA-01722:
invalid number
When I try to display the tables list in the connections tab.
I've isolated the Query.
When I execute each part of the "union all" one by one, there is no error.
when I execute the all query you got the error.
When I disable the parallel option it works fine (ALTER SESSION disable PARALLEL query).
select * from (
SELECT o.OBJECT_NAME, o.OBJECT_ID ,'' short_name, NULL partitioned,
o.sharded,
case when o.sharded <> 'Y' then o.duplicated else 'N' end duplicated,
NULL iot_type,
o.OWNER OBJECT_OWNER, o.CREATED, o.LAST_DDL_TIME, O.GENERATED, O.TEMPORARY, NULL EXTERNAL
FROM SYS.Dba_OBJECTS O
WHERE O.OWNER = :SCHEMA
AND O.OBJECT_TYPE = 'TABLE'
union all
SELECT OBJECT_NAME, OBJECT_ID , syn.SYNONYM_NAME short_NAME, NULL partitioned,
o.sharded,
case when o.sharded <> 'Y' then o.duplicated else 'N' end duplicated,
NULL iot_type,
SYN.TABLE_OWNER OBJECT_OWNER, o.CREATED, o.LAST_DDL_TIME, O.GENERATED, O.TEMPORARY, NULL EXTERNAL
FROM SYS.Dba_OBJECTS O, sys.user_synonyms syn
WHERE syn.table_owner = o.owner
and syn.TABLE_NAME = o.object_NAME
and o.object_type = 'TABLE'
and :INCLUDE_SYNS = 1
)

Join not working in Oracle error is nctb : phone_number invalid identifier

I have 2 queries and want to retrieve data using join.
1st query is as below and retrieve column "Phone_Number"
SELECT p.VALUE "Phone_Number"
FROM netcracker_rdb.RDB_BPI rd
JOIN netcracker_rdb.RDB_PROD_OFFER rpo ON rd.PRODUCT_OFFERING = rpo.OBJECT_ID
JOIN netcracker_rdb.nc_params_pim p ON p.object_id = rd.object_id
AND attr_id = 9138903692913092143
2nd query is as below and retrieve 2 columns "MSISDN" and "shi"
SELECT TRIM (REPLACE (shid.msisdn, CHR (10), '')) "MSISDN", sc.imsi
FROM
(SELECT shl.iccid, MAX (shl.sim_history_id) AS shi
FROM ninas_dba.sim_history_tb shl
GROUP BY shl.iccid) shs,
ninas_dba.sim_history_tb shid,
ninas_dba.sim_history_tb sh,
ninas_dba.sim_card_tb sc,
ninas_dba.sim_card_type_tb sct,
ninas_dba.Tenant_tb smt
WHERE
shid.iccid = shs.iccid
AND sh.sim_history_id = shs.shi
AND shid.sim_status_id = 102
AND sc.iccid = shid.iccid
I want to join these 2 queries using 1st query's "Phone_Number" and 2nd query's "MSISDN" column
Get rid of double quotes. Nothing good in them.
Should be
SELECT "Phone_Number" --> double quotes here as well, if you used them ...
FROM ( (SELECT p.VALUE "Phone_Number" --> ... here
Or, as I suggested, no double quotes:
select phone_number
from ((select p.value phone_number
perhaps best to include the phone_number as a sub-query and include in the where statement?
so something like this....
SELECT
TRIM (REPLACE (shid.msisdn, CHR (10), '')) Phone_Number
FROM
(
SELECT
shl.iccid,
MAX (shl.sim_history_id) AS shi
FROM
ninas_dba.sim_history_tb shl
GROUP BY shl.iccid
) shs,
ninas_dba.sim_history_tb shid,
ninas_dba.sim_history_tb sh,
ninas_dba.sim_card_tb sc,
ninas_dba.sim_card_type_tb sct,
ninas_dba.Tenant_tb smt
WHERE
shid.iccid = shs.iccid
AND sh.sim_history_id = shs.shi
AND shid.sim_status_id = 102
AND sc.iccid = shid.iccid
AND TRIM (REPLACE (shid.msisdn, CHR (10), '')) in (
SELECT
p.VALUE
FROM netcracker_rdb.RDB_BPI rd
JOIN netcracker_rdb.RDB_PROD_OFFER rpo
ON rd.PRODUCT_OFFERING = rpo.OBJECT_ID
JOIN netcracker_rdb.nc_params_pim p
ON p.object_id = rd.object_id
AND attr_id = 9138903692913092143
)
As mentioned in other answers the quote is only necessary when there is a space in your alias so "Phone Number" would require quotes.
I also believe you need to re-evaluate what you are trying to achieve, there is a lot happening in this query to retrieve a single attribute. Perhaps there are different avenues to pursue to achieve the same result?
Hope it helps.

Issue while running a query within a procedure

I have a procedure that expects a SELECT statement as a parameter.
If I run the SELECT Statement out of the procedure, it runs fine, but when I run it within the procedure i get the error:
The procedure :
data_dump(query_in => 'select * from reservation where type not in ('H','PURGE','E') ',
file_in => 'export_'||months_from||''||months_to||'.csv',
directory_in => 'C:\Users\Administrator\Desktop\test',
delimiter_in => '|' );
The query itself gives me the expected result.
select * from reservation where type not in ('H','PURGE','E', '|| other ||')
The error is: encountered the symbol 'H'.
Can it be because of the single quotes around the IN condition?
getting error missing right parenthesis:
(trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'|| months_from ||q'[') ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'|| months_to ||q'[') from dual))]'
You're passing quoted arguments within a string. You need to either escape those internal quotes:
data_dump(query_in => 'select * from reservation where type not in (''H'',''PURGE'',''E'') ',
file_in => 'export_'||months_from||''||months_to||'.csv',
directory_in => 'C:\Users\Administrator\Desktop\test',
delimiter_in => '|' );
Or use the alternative quoting mechanism:
data_dump(query_in => q'[select * from reservation where type not in ('H','PURGE','E') ]',
...
With your 'other' value you need to still include that in its own escape quotes:
data_dump(query_in => 'select * from reservation where type not in (''H'',''PURGE'',''E'','''|| other ||''')',
...
or (getting less readable now, perhaps):
data_dump(query_in => q'[select * from reservation where type not in ('H','PURGE','E',']' || other || q'[')]',
...
You can use dbms_output to debug this sort of thing, printing the generated argument and seeing if that matches what you've been running manually.
With your third partial code you've put closing single quotes after the (presumably numeric) variables:
(trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'|| months_from ||q'[') ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'|| months_to ||q'[') from dual))]'
^ ^
and you are, as the message says, missing a closing parenthesis in the second add_month call; so it should be (where ... is the rest of the statement that you haven't shown):
q'[ ... (trunc(a.update_date) between (select add_months(TRUNC(SYSDATE), -(]'
|| months_from ||q'[) ) from dual) and (select add_months(TRUNC(SYSDATE), -(]'
|| months_to ||q'[) ) from dual))]'
Again, this is basic debugging you can do by printing the generated statement and inspecting it or trying to run it manually.
Also you don't need the inner select from dual stuff or all the parentheses; you can just do:
q'[ ... trunc(a.update_date) between add_months(TRUNC(SYSDATE), -]'
|| months_from ||q'[) and add_months(TRUNC(SYSDATE), -]'|| months_to ||q'[)]'

Split owner and object name in Oracle

Given an object identified by the form owner.tablename; how do I split the owner and table name up?
Both my ideas of either string tokenization or select owner, object_name from all_objects where owner || '.' || object_name = 'SCHEMA.TABLENAME' seem like hacks.
You can use DBMS_UTILITY.name_tokenize for this purpose.
This procedure calls the parser to parse the given name as "a [. b [.
c ]][# dblink ]". It strips double quotes, or converts to uppercase if
there are no quotes. It ignores comments of all sorts, and does no
semantic analysis. Missing values are left as NULL.
e.g.
DBMS_UTILITY.NAME_TOKENIZE
( name => 'SCHEMA.TABLENAME'
, a => v_schema
, b => v_object_name
, c => v_subobject -- ignore
, dblink => v_dblink
, nextpos => v_nextpos -- ignore
);
http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_util.htm#BJEFIFBJ
SELECT SUBSTR('SCHEMA.TABLENAME', 0, INSTR('SCHEMA.TABLENAME', '.') - 1) OWNER,
SUBSTR('SCHEMA.TABLENAME', INSTR('SCHEMA.TABLENAME', '.') + 1) TABLE_NAME
FROM DUAL

Resources