postgresql code for delete operation in table type - database-migration

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;

Related

Can I add a string type parameter to a SQL statement without quotes?

I have a C++Builder SQL Statement with a parameter like
UnicodeString SQLStatement = "INSERT INTO TABLENAME (DATETIME) VALUES (:dateTime)"
Can I add the parameter without quotes?
Usually I'd use
TADOQuery *query = new TADOQuery(NULL);
query->Parameters->CreateParameter("dateTime", ftString, pdInput, 255, DateTimeToStr(Now()));
which will eventually produce the SQL String
INSERT INTO TABLENAME (DATETIME) VALUES ('2022-01-14 14:33:00.000')
but because this is a legacy project (of course, it always is) and I have to maintain different database technologies, I need to be able to inject database specific date time conversion methods, so that the endresult would look like
INSERT INTO TABLENAME (DATETIME) VALUES (to_date('2022-01-14 14:33:00.000', 'dd.mm.yyyy hh24:mi:ss')))
If I try injecting this via my 'usual' method (because I don't think I can inject a second parameter into this one) it'd look like:
TADOQuery *query = new TADOQuery(NULL);
query->Parameters->CreateParameter("dateTime", ftInteger, pdInput, 255, "to_date('" + DateTimeToStr(Now()) + "', 'dd.mm.yyyy hh24:mi:ss')");
but of course the result would look like:
INSERT INTO TABLENAME (DATETIME) VALUES ('to_date('2022-01-14 14:33:00.000', 'dd.mm.yyyy hh24:mi:ss')'))
and therefore be invalid
Or is there another way to do this more cleanly and elegantly? Although I'd settle with 'working'.
I can work around this by preparing two SQL Statements and switch the statement when another database technology is but I just wanted to check if there is another way.
Why are you defining the parameter's DataType as ftInteger when your input value is clearly NOT an integer? You should be defining the DataType as ftDateTime instead, and then assigning Now() as-is to the parameter's Value. Let the database engine decide how it wants to format the date/time value in the final SQL per its own rules.
query->Parameters->CreateParameter("dateTime", ftDateTime, pdInput, 0, Now());

Parse data from table Greenplum

I have a table scheme2.central_id__new_numbers in Greenplum.
I need select data from scheme2.central_id__new_numbers in the form of a many-to-many relationship.
Also I write the code but must have made a wrong turn somewhere (the code doesn't work):
CREATE FUNCTION my_scheme.parse_new_numbers (varchar) RETURNS SETOF varchar as
$BODY$
declare
i int;
BEGIN
FOR i IN 1..10 LOOP
select
central_id,
(select regexp_split_to_table((select new_numbers
from scheme2.central_id__new_numbers limit 1 offset i), '\s+'))
from scheme2.central_id__new_numbers limit 1 offset i
END LOOP;
END;
$BODY$
LANGUAGE plpgsql;
I'd recommend using the UNNEST() function instead, i.e. assuming new_numbers column is of int[] data type,
SELECT central_id
, UNNEST(new_numbers) AS new_numbers
FROM central_id__new_numbers;
If new_columns column is not an array data type then you need to use i.e. string_to_array() or similar before using UNNEST().

Update column value from array element inside before insert trigger

i am trying to do a before insert trigger that updates one of the columns that will be inserted with a value taken from another column on the same record, however the catch is that that value is the first value of the said column after converting it to an array. when i include the function or the array i get a syntax error but the examples i have found for the proper syntax do not have the detail needed to make my code work. Here is my code:
Schema is as follows
create table events (
id int primary key,
performers varchar,
performer_id varchar
);
Function is:
create function events_set_performer() returns trigger
language plpgsql
as $$
declare
performer_ varchar := '0';
BEGIN
set NEW.performer_id = (string_to_array(NEW.performers,':'))[1];
return new;
END;
$$;

referencing new Table as new_table not visible to the trigger function Postgresql

I am new in Postgresql so this question may be dumb for you guys!
I am tring to use the referenced tables in a trigger function, but some how the function doesn't seem to have access on the referenced alias (new_table) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
I am having this error:
"new_table.other_field" is not a known variable
and here is the trigger code:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
The function code should be executed first and then the trigger's, so how could the function access the alias that is referenced later in the trigger defenition??
and how to access the referenced table aliases in the function?
Note: In my example I am tring to use the alias, so when I use NEW inplace of new_table the function is created successfully!
The problem was that i am tring to set data in the NEW table using the alias name, however to do that i should use the original name NEW and not the referenced alias new_table..
The referenced alias new_table could be used to only get data from, like in FROM CLAUSE, joins and WHERE CLAUSE where one doesn't change the data in the referenced table.
Update:
here is an example of what i did to test that:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
notice that the trigger inserts the data into test_table2

Referencing a macro variable created by a prompt SAS EG

I created a prompt in SAS EG that takes a text input and creates the macro variable called 'variableName'.
I am trying to reference this macro variable like so:
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = &variableName ;
This gives me an error that says: "Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER."
I have also tried enclosing &variableName in single and double quotes but when I do that I just don't get any results.
I am able to reference the prompt when I use query builder and filter data based on the prompt, but I am trying to use the prompt's value in calculated expressions, etc. and in queries I write without query builder. How can i reference the variable I created in the prompt??
Edit: code with a value that the macro variable would have
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = 'NAME OF PERSON';
When I run that, I get the results I want.
It needs to resolve to valid SAS code. Assuming &variableName is a string, then it would be something like:
proc sql;
create table MyTable as
select * from Source_Table as a
where a.field = "&variableName." ;
If this isn't working, please show a query that does work with the same value as the macro variable would have. And then we can suggest how to change your code.
Edit: based on your comment you do not have the prompt connected to your query. Right click the query and link the prompt to your query and it will run before the query to provide the value.