I need a command to delete a table if that exists in NETEZZA, Something like that:
drop table if exists xxx;
I have searched and tried many but it didn't work. Can you help me here?
In netezza you can use this syntax:
drop table table_name if exists;
There is nothing built-in, but you can create a stored proc which uses the catalog views to check if the table exists before attempting to drop it:
create or replace procedure maybe_drop(varchar(128))
returns boolean
language nzplsql
as
begin_proc
declare
oname alias for $1;
o record;
begin
select otype into o
from (
select 'TABLE' otype from _v_table where tablename = upper(oname)
union all
select 'VIEW' otype from _v_view where viewname = upper(oname)
) x;
if found then
execute immediate 'DROP '||o.otype||' '||oname;
end if;
end;
end_proc;
Related
I already created stored procedure in BigQuery for data querying.
The main point is send some parameter to get data in WHERE condition, then use list of data in WHERE of other table. It shown error as "Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN."
THIS IS MY QUERY
CALL mydatabase.stored_procedures.GetDateOutCS();
SELECT *, GetDateOutCS(ContainerHeaderID)
FROM mydatabase.instance.PRD_ContainerHeader
THIS IS MY STORED PROCEDURE
CREATE OR REPLACE PROCEDURE mydatabase.stored_procedures.GetDateOutCS()
BEGIN
CREATE TEMP FUNCTION GetDateOutCS (ContainerHeader_ID INT64)
AS
(
( SELECT GIDateFROM mydatabase.instance.RMM_ReceivedMaterialHU
WHERE HU IN
(SELECT CAST(HU AS INT64) FROM mydatabase.instance.PRD_ContainerLine
WHERE ContainerHeaderID=ContainerHeader_ID) LIMIT 1)
);
END;
I've tried to changed subquery to join table, but it's not works for me.
CREATE OR REPLACE PROCEDURE mydatabase.stored_procedures.GetDateOutCS_2()
BEGIN
CREATE TEMP FUNCTION GetDateOutCS (ContainerHID INT64)
AS
(
array( SELECT CAST(GIDate AS DATETIME)
FROM mydatabase.instance.RMM_ReceivedMaterialHU as h
INNER JOIN mydatabase.instance.PRD_ContainerLine as cl ON h.HU = CAST(cl.HU AS INT64)
WHERE cl.ContainerHeaderID=ContainerHIDAND cl.HU IS NOT NULL LIMIT 1)
);
END;
My expect solutions
How to fix this stored procedure.
This is limitation of BigQuery?
I have a code for deleting table type in MSSQL.Have to convert into PGSQL which is giving error.
Looking forward for a PGSQL code which deletes from a Table Type User defined:
below MSSQL CODE:
declare #Entity TRef_StructureTree readonly //input parameter from procedure
DELETE Tef_StructureTree
FROM Tef_StructureTree
inner join (select * from #Entity) as source
on Tef_StructureTree.ChildCodeBDR=source.ChildCodeBDR AND
Tef_StructureTree.ChildScheme=source.ChildScheme;
Below is definition of USer defined Table type:
CREATE TYPE [dbo].[Tef_StructureTree] AS TABLE(
[ChildCodeBDR] [numeric](10, 0) NOT NULL,
[ChildScheme] [nvarchar](100) NOT NULL,
)
below PGSQL CODE:
CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity Tef_StructureTree[])
as
begin
DELETE FROM v_entity
where v_entity."ChildCodeBDR" in(select "source"."ChildCodeBDR" from unnest(v_entity) as "source" )
and v_entity."ChildScheme" in (select "source"."ChildScheme" from unnest(v_entity) as "source" );
end;
ERROR: relation "v_entity" does not exist
Please Help by providing the equivalent!!
Your parameter is called v_entitydata not v_entity, so you either need to change your parameter name or the reference to it. You probably want to delete from the table Tef_StructureTree not from the passed array.
Your function's structure is also not valid for Postgres as the function body needs to be provided as a string, e.g. using dollar quoting.
You can simplify the condition, by only using a single sub-query as well.
So putting all that together, the function should look like this:
CREATE OR REPLACE FUNCTION UpdateStrucTree(v_entity "Tef_StructureTree"[])
as
$$
DELETE FROM "Tef_StructureTree"
where ("ChildCodeBDR", "ChildScheme") in (select "ChildCodeBDR", "ChildScheme"
from unnest(v_entity) )
$$
language sql;
The following statement retrieve the value of sub tag msg_id from MISC column if the sub stag contain value like %PACS%.
SELECT REGEXP_SUBSTR(MISC, '(^|\s|;)msg_id = (.*?)\s*(;|$)',1,1,NULL,2) AS TRANS_REF FROM MISC_HEADER
WHERE MISC LIKE '%PACS%';
I notice the query return record with null value (without msg_id) as well. Any idea if can exclude those null records from the syntax of REGEXP_SUBSTR, without adding any where clause.
Sample data of MISC:
channel=atm ; phone=0123 ; msg_id=PACS00812 ; ustrd=U123
channel=pos; phone=9922; ustrd=U156
The second record without msg_id, so it need to be excluded.
This method does not use REGEXP so may not be suitable for you.
However, it does satisfy your requirement.
This takes your embedded list of msg_id, breaks it out to a row for each component for an ID (I've assumed you do have something uniquely identifies each record).
It then only returns the original row where one of the rows for the ID has 'PACS' in it.
WITH thedata
AS (SELECT 1 AS theid
, 'channel=atm ; phone=0123 ; msg_id=PACS00812 ; ustrd=U123'
AS msg_id
FROM DUAL
UNION ALL
SELECT 2, 'channel=pos; phone=9922; ustrd=U156' FROM DUAL)
, mylist
AS (SELECT theid, COLUMN_VALUE AS msg_component
FROM thedata
, XMLTABLE(('"' || REPLACE(msg_id, ';', '","') || '"')))
SELECT *
FROM thedata td
WHERE EXISTS
(SELECT 1
FROM mylist m
WHERE m.theid = td.theid
AND m.msg_component LIKE '%PACS%')
Thedata sub-query is simply to generate a couple of records and pretend to be your table. You could remove that and substitute your actual table name.
There are other ways to break up an embedded list including ones that use REGEXP, I just find the XMLTABLE method 'cleaner'.
At opencart2.0, I want to create a ocmod extension, In install.sql, I need to modify the database field. When I modify the database field, I need to decide if the field exists. Ive tried multiple variations of this, but none of them seem to work. Any ideas? Thanks in advance.
this is my install.sql,but is error
DROP PROCEDURE IF EXISTS add_col_need_credit;
DELIMITER $$ CREATE PROCEDURE add_col_need_credit() BEGIN IF NOT EXISTS(SELECT column_name FROM information_schema.columns WHERE table_name='oc_customer_group_description' AND column_name='need_credit' ) THEN ALTER TABLE `oc_customer_group_description` ADD `need_credit` numeric(10,4) NOT NULL default 0; END IF;END$$ DELIMITER ;
CALL add_col_need_credit();
this is not error,you can in mysql console use it
DROP PROCEDURE IF EXISTS add_col_need_credit;
DELIMITER $$ CREATE PROCEDURE add_col_need_credit() BEGIN IF NOT EXISTS(SELECT column_name FROM information_schema.columns WHERE table_name='oc_customer_group_description' AND column_name='need_credit' ) THEN ALTER TABLE `oc_customer_group_description` ADD `need_credit` numeric(10,4) NOT NULL default 0; END IF;END$$ DELIMITER ;
CALL add_col_need_credit();
DROP PROCEDURE IF EXISTS add_col_need_credit;
DELIMITER $$ CREATE PROCEDURE add_col_need_credit() BEGIN IF NOT EXISTS(SELECT column_name FROM information_schema.columns WHERE table_name='oc_customer_group_description' AND column_name='need_credit' ) THEN ALTER TABLE `oc_customer_group_description` ADD `need_credit` numeric(10,4) NOT NULL default 0; END IF;END$$ DELIMITER ;
CALL add_col_need_credit();
I'm using sqlite3 in my c++ program and am trying to run an SQL string in the
sqlite3_get_table function.
Here's my sql string.
SELECT name FROM sqlite_master WHERE type='table' AND name=test_table;
I keep getting the error "no such column "test_table"" .
All I am trying to do is confirm the existence of a table in my database. That's all.
Any clues as to what's wrong with my string.
In SQLite double quotes ('"') is the identifier-escape character, so assuming this is your SQL (raw SQL, nothing to do with C++):
SELECT
name
FROM
sqlite_master
WHERE
type = 'table'
AND
name = "test_table;"
Is equivalent to:
...
name = test_table
...which obviously isn't what you want.
You should use single-quoted strings in SQL, and the statement-terminating semicolon should go at the very end:
SELECT
name
FROM
sqlite_master
WHERE
type = 'table'
AND
name = 'test_table';