is it possible to do multiple activitycount check in teradata? - teradata

using bteq script, I want to delete the temporary error and log tables created during TPT load namely ErrorTable1, ErrorTable2, LogTable.
I have prepared following bteq script with if-else, which will drop tables if it exists
.LOGON ${HOST}/${USER},${PASS};
SELECT 1 FROM dbc.TablesV WHERE CONCAT(DataBaseName, '.', TableName) = 'Test.ErrorTable1';
.IF ACTIVITYCOUNT = 0 THEN GOTO CHECK_1;
DROP TABLE Test.ErrorTable1;
.LABEL CHECK_1;
SELECT 1 FROM dbc.TablesV WHERE CONCAT(DataBaseName, '.', TableName) = 'Test.ErrorTable2';
.IF ACTIVITYCOUNT = 0 THEN GOTO CHECK_2;
DROP TABLE Test.ErrorTable2;
.LABEL CHECK_2;
SELECT 1 FROM dbc.TablesV WHERE CONCAT(DataBaseName, '.', TableName) = 'Test.LogTable';
.IF ACTIVITYCOUNT = 0 THEN GOTO DONE;
DROP TABLE Test.LogTable;
.LABEL DONE;
.LOGOFF
.EXIT
My question is, in teradata bteq, can I use ACTIVITYCOUNT multiple times, like in above script, to check various table's existence?

Related

Liquibase : Expected something between the "TABLE" keyword and the "IF" keyword

I am trying to run the below sql by liquibase and I am getting an error expected something between "TABLE" and the keyword "IF" keyword .This is for teradata database
CREATE MULTISET TABLE IF NOT EXISTS SCHEMA_NAME.TABLE_NAME, NO, FALLBACK,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO AS ( I TRIED WITHOUT "AS")
(
COL1 INTEGER,
COL1 INTEGER....ETC)
PRIMARY INDEX( COL1,COL2);
REPLACE PROCEDURE <database_name>.drop_if_exists( in_object varchar(50)) begin IF EXISTS(
SELECT 1
FROM dbc.tables
WHERE tablename = in_object
and databasename='<database_name>') THEN
CALL DBC.SysExecSQL('DROP TABLE ' || in_object);
END IF; END;
Followed by the regular create table statment.

Create trigger on delete that resets auto increment id if the table is empty on sqlite

I want to create a trigger that will reset the auto increment id to 0 if the table have just become empty. I've tried the following:
CREATE TRIGGER reset_autoincrement AFTER DELETE ON temp WHEN count(*) = 0
BEGIN
UPDATE sqlite_sequence SET seq = 0 WHERE name = 'temp';
END
Although the SQL seems correct, it doesn't do what I want.
Any suggestions?
The expression in the WHEN clause is not in the context of the table; count(*) without a table returns 1.
Use a subquery instead:
... WHEN (SELECT count(*) FROM temp) = 0 ..

How can drop table if table exists in oracle?

I am trying to create database by my java application using my generated schema file. In schema I have included drop query also. But I want to do some improvements for DROP QUERY. So I want to check the existence of db objects before running drop query and drop only when if it exists.
I googled for it and found some oracle link, Some link suggest following syntax and some mentioned that ORACLE does not support such syntax.
SYNTAX A:
IF EXISTS DROP TABLE TABLE_NAME
SYNTAX B:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
I also tried following queries:-
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' )
DROP TABLE [BBB]
but it was giving error:-
Error starting at line 2 in command:
DROP TABLE [BBB]
Go
Error report:
SQL Error: ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Error starting at line 1 in command:
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' ) DROP TABLE [BBB]
Error report:
Unknown Command
I refered following links:-
https://community.oracle.com/thread/2421779?tstart=0
Please suggest me if there any other queries to drop table with condition if table exists.
Drop table with no check. If any error exists you'll never know when something went wrong.
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Or you can search in Oracle dictionary.
DECLARE
l_cnt NUMBER;
BEGIN
SELECT count(*)
INTO l_cnt
FROM user_tables
WHERE table_name = 'MY_TABLE';
IF l_cnt = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
END IF;
END;
If you run following code you do not have to check if table exists and in case of errors (table is locked with now wait or any other you will know about it)
begin
for c1 in (select owner,table_name from dba_tables where table_name='MY_TABLE') loop
execute immediate 'drop table '||c1.owner||'.'||c1.table_name||'';
end loop;
end;
Try this : It will drop table 'table_name' if it is present .
declare
a varchar2(700) ;
begin
execute immediate ' SELECT CASE WHEN tab = 1
THEN ''DROP TABLE TABLE_NAME''
ELSE ''select 1 from dual''
END
FROM ( SELECT sum(case when table_name = ''TABLE_NAME'' then 1 else 0 end ) as tab FROM user_tables)' into a;
EXECUTE IMMEDIATE a;
end;

convert t-sql statement to teradata

I am trying to understand the equivalent of this statement
IF OBJECT_ID('Current') IS NOT NULL
DROP TABLE Current;
in Teradata.
Can someone help me out converting this statement to TD14. Thank you!
You can do this in at least newer versions of TD:
select
count (*)
from
dbc.tablesv where tablename = '<your table>'
and databasename = '<your db>'
having count (*) > 0;
.if activitycount = 1 then .GOTO DropTable;
.if activitycount <> 1 then .quit;
.LABEL DropTable
select 'DROP TABLE!';
drop table <your db>.<your table>;
Sadly enough, this won't work with volatile tables. If they are global temporary tables, you can use
select
count (*)
from
dbc.AllTempTablesVX where B_tablename =

create dynamic table within procedure?

I am trying to create dynamic table within procedure but i am getting error please
tell me whats the error
CREATE OR REPLACE PROCEDURE check_sms_bundle_25 (
MON VARCHAR2,
YEAR_P VARCHAR2 DEFAULT TO_CHAR (SYSDATE, 'YY'),
QUARTER VARCHAR2,
TYPE VARCHAR2 DEFAULT 'NEW')
IS
BEGIN
IF UPPER (QUARTER) = 1
THEN
EXECUTE IMMEDIATE 'BEGIN CREATE OR REPLACE TABLE (''SMS_Bundle_25_'''|| UPPER (MON)|| '''_Q'''|| UPPER (QUARTER)|| '||''_''||'''|| UPPER (YEAR_P)|| ''')
AS
SELECT customer_id, otxact
FROM ordertrailer INNER JOIN orderhdr_all ON ohxact = otxact
WHERE sncode = 343 AND ohentdate = ''1-aug-2014'' AND ohstatus = ''IN''
END';
END IF;
END;
The error msg is
ORA-06550: line 2, column 4: PLS-00103: Encountered the symbol
"CREATE" when expecting one of the following:
begin case declare exit for goto if loop mod null pragma raise
return select update while with << close current delete
fetch lock insert open rollback savepoint set sql execute commit
forall merge pipe ORA-06512: at "FI_SDINE.CHECK_SMS_BUNDLE_25", line 9
ORA-06512: at line 1
As well as 'or replace' not being valid as part of the create table syntax, you're enclosing the DDL statement inside another anonymous PL/SQL block, you're trying to put the table name inside parentheses, and you're including quote marks - which are not allowed in an (unquoted) object identifier. So if you pass in argukments AUG, 14, 1 NEW then your dynamic statement is trying to run:
BEGIN CREATE OR REPLACE TABLE ('SMS_Bundle_25_'AUG'_Q'1||'_'||'14')
AS
SELECT customer_id, otxact
FROM ordertrailer INNER JOIN orderhdr_all ON ohxact = otxact
WHERE sncode = 343 AND ohentdate = '1-aug-2014' AND ohstatus = 'IN'
END
The BEGIN/END make is a block and the DDL statement isn't valid in PL/SQL; that's why you're having to use execute immediate in the first place. If you change your construction to:
EXECUTE IMMEDIATE 'CREATE TABLE SMS_Bundle_25_'|| UPPER (MON)
|| '_Q'|| UPPER (QUARTER) ||'_' || UPPER (YEAR_P)|| '
AS
SELECT customer_id, otxact
FROM ordertrailer INNER JOIN orderhdr_all ON ohxact = otxact
WHERE sncode = 343 AND ohentdate = ''1-aug-2014'' AND ohstatus = ''IN''');
you'd then be trying to run:
CREATE TABLE SMS_Bundle_25_AUG_Q1_14
AS
SELECT customer_id, otxact
FROM ordertrailer INNER JOIN orderhdr_all ON ohxact = otxact
WHERE sncode = 343 AND ohentdate = '1-aug-2014' AND ohstatus = 'IN'
which at least looks more viable, assuming the tables you're selecting from exist. No idea why you're passing the year and quarter numbers as strings or applying upper() to them. And presumably you really want the select to be filtered on the same year and month as the table name.
It's useful to display the command you're trying to execute, for example with dbms_output, to see exactly what you've created; and you can then also run that manually to see where it's going wrong more clearly.
But as noted in comments, it's unusual to create objects from a procedure like this. Your schema should usually be static, not modified on the fly. You won't be able to refer to this table from other code unless that is also being built dynamically. You seem to be creating a table as a snapshot of the other tables; a view or a materialised view might be more appropriate. But I'm not really sure why you're doing this so it's not entirely clear what you should be doing instead.

Resources