Drop partition if exists in HANA - sql

I would like to know if there is a way to DROP a partition IF EXISTS in HANA.
I have tried like below
ALTER TABLE TBL_NAME DROP IF EXISTS PARTITION VALUE = '1234'
I am getting incorrect syntax error near IF
What is the best available solution?

HANA does not support the IF EXISTS clause.
You may want to work around this limitation by implementing your own "IF EXISTS", similar to what I presented here:
If EXISTS would exist

Related

SQLite Script IF NOT EXISTS (...) - Alternatives

I'm looking for an alternative to the IF-ELSE-Statement as known in MS-SQL for SQLite.
Try searching on stackoverflow first!
I did. I found something like that:
SQLite 'IF NOT EXISTS' syntax error
SQLite if statement in WHERE clause
and so on...
The problem in that cases is that they perform SELECT/INSERT/UPDATE/DELETE.
I want to alter the schema of an existing database.
Creating a new table is trivial because there is CREATE TABLE IF NOT EXISTS "foo" (....);.
But how about adding columns to an existing table?
I want to write a script like:
IF NOT EXISTS (SELECT * FROM pragma_table_info("<table_name>") WHERE name == "<column_name>")
BEGIN
ALTER TABLE "<table_name>" ADD "<column_name>" TEXT;
END
This sounds like ALTER TABLE ADD COLUMN IF NOT EXISTS in SQLite. But this post is out of the year 2010 and I would expect that something has changed in the last 9 years.
I need to do some statements like that using arbitrary queries and statements.
Is there any way to do that in pure SQL or do I have to handle that in application code?

How do you check a database table existence with query statement

may i know how do you check if a database table already exist in the database already or not with just using the sql query method.
Try this
IF Exist(Select 1 from INFORMATION_SCHEMA. COLUMNS where TABLE_NAME like '%tableName%')

How to check if a projection exists before dropping it in Vertica?

I want to do something like
DROP PROJECTION IF EXISTS myProjection;
Apparently I can use IF EXISTS for a table but not a projection.
I understand if I drop the table with CASCADE, that should drop the corresponding projections, but I apparently have some orphaned projections which I do not have a track of. And sometimes when I rename tables/projections, it fails saying projection already exists.
The Drop Projection page and this stackoverflow page for generic sql do not help much.
Edit: I am using this in Mybatis Migrations. So my entire migration would fail if there is an error in any of the scripts. So, no I can not ignore the error.
If there is no IF EXISTS for a projection -- is there a programatic way (LIKE TSQL/PLSQL) where I could specify a condition to check if the projection exists and take an action whether to drop it or not?
There is no drop projection IF EXISTS .... You can just use drop projection ... and - of course - you will get an error message if the projection you're trying to delete does not exists.
You can list ALL projections for a given schema/table using a SQL like this:
\set schema '''my_schema'''
\set table '''my_table'''
select
projection_name,
sum(row_count) as row_count,
sum(used_bytes) as used_bytes,
sum(wos_row_count) as wos_row_count,
sum(wos_used_bytes) as wos_used_bytes,
sum(ros_row_count) as ros_row_count,
sum(ros_used_bytes) as ros_used_bytes
from
projection_storage
where
anchor_table_schema = :schema and
anchor_table_name = :table
group by 1
order by 1
;
And the following will list all projections associated with tables in a given schema:
\set schema '''my_schema'''
select
projection_name,
anchor_table_name,
sum(row_count) as row_count,
sum(used_bytes) as used_bytes,
sum(wos_row_count) as wos_row_count,
sum(wos_used_bytes) as wos_used_bytes,
sum(ros_row_count) as ros_row_count,
sum(ros_used_bytes) as ros_used_bytes
from
projection_storage
where
anchor_table_schema = :schema
group by 1, 2
order by 1, 2
;
you can query catalog table in vertica, this will give you schema names of all present schemas ,
select v_catalog.columns from v_catalog.columns;
i sometime use this select query , which gives me evey info related to a table
select export_objects('','your_schema.table_name');
This is possible since Vertica 9.2.x.
DROP PROJECTION IF EXISTS myProjection;
Reference: https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/SQLReferenceManual/Statements/DROPPROJECTION.htm

empty sql table only if it exists (not drop)

How can I delete the contents of a table only if it exists? Preferably the sql statement should be standard and not oriented to any db.
Please notice that I do not want to drop a table if it exists, i.e.
DROP TABLE IF EXISTS foo
PS: I have already checked truncate and delete but they don't fit the requirement if the table exists.
As far as i know, there is no standard for "if exists". Some databases support it, others do not, and will give you a syntax exception.
Edit
INFORMATION_SCHEMA does not usually change between different versions and is common in most databases, and to my best knowledge this is the most proper way to check whether a table exists in SQL:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = tableName ))
BEGIN
DELETE FROM tableName
END

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'