Cannot drop existing index - db2

Service I'm using is DB2 on cloud.
I have created an index but didn't specify the schema
CREATE INDEX IDX_TAB1     
ON SCHEMA1.TAB1 (RECLASS_MONTH DESC)
;
It was created in the following INDSCHEMA
SELECT * FROM syscat.indexes
WHERE TABNAME ='TAB1'
INDSCHEMA
INDNAME
TABSCHMA
506TEFMHSB
IDX_TAB1
SCHEMA1
I want to drop it and specify the schema,
however running the following DROP sql gives me error
DROP INDEX "506TEFMHSB".IDX_TAB1
;
Error message
"506TEFMHSB.IDX_TAB1" is an undefined name.. SQLCODE=-204, SQLSTATE=42704, DRIVER=4.27.25
Or
DROP INDEX IDX_TAB1
;
Error message
"506TEFMHSB.IDX_TAB1" is an undefined name.. SQLCODE=-204, SQLSTATE=42704, DRIVER=4.27.25
Not sure how I'm supposed to drop this, any help will be appreciated

Apparently this worked for some reason
DROP INDEX "506TEFMHSB".IDX_COGNOS_COMPLIANCE_DASHBOARD_DATA     ;
I'm not sure whether there was some issue with the fact that I used newline between the end of the statement and the semicolon and the whitespace inbetween...but the issue is solved

Related

Redshift Correlated Subquery within copy command

I am trying to query from a table within a copy command however, I have continually gotten errors. Here is the example SQL statement.
copy schema.table
from 's3://bucket/folder`
iam_role (select value from roles.iam where key = 'IAMRole');
The inner select statement on its own returns a value however, when I run the above, I get the following error:
SQL Error [500310] [42601]: [Amazon](500310) Invalid operation: syntax error at or near "("
The COPY command, as you must suspect, does not support embedded SQL.
If you want to do something like this, you can, but you'll need a procedure.

Create History Trigger Db2

I am trying to create a trigger that copys a newly INSERTED and/or UPDATED record from one table to a History version of this table. The purpose is to keep a record every time a record in a table is inserted and/or updated.
This is the Create Trigger statement syntax that I have, following the IBM Db2 Trigger site.
CREATE TRIGGER "TADB2"."HIST_CSEMASTER_Z_LOG"
AFTER UPDATE ON TADB2.CSEMASTER_Z
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO TADB2.HIST_CSEMASTER_Z
values (N.CSEMASTERID, N.CURRID, N.SUBCURRID, N.EXPIREDATE, N.CREATEDATE, N.CUSTVIEWNUM, N.CSEMSTRNAME, N.JOINCODE, N.CONTACTCOUNTRYCD, N.CONTACTSERIALNUM, N.AUDIENCECD, N.SPONSORNAME, N.AVAILABLEDATE, N.OWNERCOUNTRYCD, N.OWNERSERIALNUM, N.PRMWWCHAPTERID, N.PRMWWSUBCHAPTERID, N.SECWWCHAPTERID, N.SECWWSUBCHAPTERID, N.INTRANETIND, N.TINSERTTS, N.TLASTUPDTS, N.IBM_TRADEMARK, N.OTHER_TRADEMARK, N.INTERNETIND, N.BRANDID, N.BUSINESSID, N.LIFECYCLE_STATUS);
END;
The error I am receiving is:
[Code: -104, SQL State: 42601] An unexpected token "END-OF-STATEMENT" was found following "N.LIFECYCLE_STATUS)". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.72.44
I've tried:
Removing the END
Removing the semi-colon after the END
Removing the semi-colon after the N.LIFECYCLE_STATUS);
Any help would be great! Thank you.
I highly recommend Db2 Temporal Tables for that. If you just use System Temporal functionality it would provide already the functionality you describe.
Worth to check it out
You must use different from ';' statement terminator in your tool.
Below is an example for DB2 Command Line Processor, which understands the first line as a directive to do this.
--#SET TERMINATOR #
CREATE TRIGGER "TADB2"."HIST_CSEMASTER_Z_LOG"
AFTER UPDATE ON TADB2.CSEMASTER_Z
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
INSERT INTO TADB2.HIST_CSEMASTER_Z
values (N.CSEMASTERID, N.CURRID, N.SUBCURRID, N.EXPIREDATE, N.CREATEDATE, N.CUSTVIEWNUM, N.CSEMSTRNAME, N.JOINCODE, N.CONTACTCOUNTRYCD, N.CONTACTSERIALNUM, N.AUDIENCECD, N.SPONSORNAME, N.AVAILABLEDATE, N.OWNERCOUNTRYCD, N.OWNERSERIALNUM, N.PRMWWCHAPTERID, N.PRMWWSUBCHAPTERID, N.SECWWCHAPTERID, N.SECWWSUBCHAPTERID, N.INTRANETIND, N.TINSERTTS, N.TLASTUPDTS, N.IBM_TRADEMARK, N.OTHER_TRADEMARK, N.INTERNETIND, N.BRANDID, N.BUSINESSID, N.LIFECYCLE_STATUS);
END#

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

Continue sql statement after an error in postgresql database

I am working on a view which this is its sql
-- DROP VIEW bd_segment_id;
CREATE OR REPLACE VIEW bd_segment_id AS
SELECT final.gid,
row_number() OVER (ORDER BY final.gid) AS row_number,
degrees(st_azimuth(ff.sp, ff.ep) - 1.57079633::double precision) AS az_deg,
st_length(ff.st_makeline) AS st_length,
ff.st_makeline
FROM bd_segment_geom ff
JOIN final ON st_touches(ff.st_makeline, final.geom)
GROUP BY final.gid, ff.sp, ff.ep, ff.st_makeline;
ALTER TABLE bd_segment_id
OWNER TO postgres;
while running each query of this table I have faced this error
ERROR: GEOSTouches: TopologyException: side location conflict at 553655.77720000315 3474241.5185000021
********** Error **********
ERROR: GEOSTouches: TopologyException: side location conflict at 553655.77720000315 3474241.5185000021
SQL state: XX000
Is there any way for sql to pass errors and continues to do the rest of sql?I know the problem is an internal error but I want sql jump of it..
I have searched and found this in section 39.6.6. Trapping Errors is says how we can use an EXCEPTION clause but I don't know how to write handler_statements that says sql to continue.
I need sth like try catch in C# or jave
can some one please help me with this problem?
thank you

ERROR In Sequences query if postgres

I create one sequence in postgres and fire one query which is mentioned below
SELECT M_PRODUCTSEQ.NEXTVAL from DUAL;
but it gives me the below error:
ERROR: relation "dual" does not exist.
Kindly help me out. How can i made the relation with dual?
PostgreSQL does NOT support the from DUAL syntax. It does however make the from portion of a query like this optional, so getting the next value (nextval) of a sequence you would do something like this:
SELECT nextval('m_productseq');