How to execute this Postgres SQL - postgresql

This should be a simple SQL Server query, i am new to PostGres, i am not sure what is the error, if there is a documentation, please point, i will follow up on that too
declare
Vin VARCHAR(10) := 'WBY7Z4C5&J';
begin
SELECT false as isresultempty, v1.*, '' AS "message"
FROM pd.vehicle v1
WHERE v1.maskedvin = cast(Vin AS CHARACTER(10))
AND v1.modelyyyy = 2018
AND vinChangeCd <> 'D'
AND v1.circularnumber <> 'INTM';
end
I keep getting this error
SQL Error [42601]: ERROR: syntax error at or near "false"
Position: 61
Where: invalid type name "false as isresultempty"
update after changing this sql
SQL Error [42601]: ERROR: syntax error at or near "VARCHAR"
Position: 16

Related

How to implement dynamic sql function in postgresql?

I am using PostgreSQL 11 version.
I want to implement a function that takes the layer name (table), column name, and id as parameters.
create or replace function test(layer_name anyelement, field_name anyelement, object_id text)
returns setof anyelement
language plpgsql
as $function$
begin
return query execute format('
select
*
from
%s
where
%s = cast($1 as int4)'
, pg_typeof(layer_name), pg_typeof(field_name))
using object_id;
end;
$function$
;
This is the code I've implemented and when I call the function I get an error.
What am I doing wrong?
Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
### The error occurred while setting parameters
### SQL: select * from test(?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY}
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
### The error occurred while setting parameters
### SQL: select * from test(?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "="
Where: PL/pgSQL function test(anyelement,anyelement,text) line 3 at RETURN QUERY
You have to change your function slightly
Instead of field_name anyelement use field_name text in parameter.
and in place of pg_typeof(field_name) use only field_name:
So your function definition will be:
create or replace function test(layer_name anyelement, field_name text, object_id text)
returns setof anyelement
language plpgsql
as $function$
begin
return query execute format('
select
*
from
%s
where
%s = cast($1 as int4)'
, pg_typeof(layer_name), field_name)
using object_id;
end;
$function$
;
Most important part is calling of the function:
select * from test(null::table_name,'field_name','2');
Please note that your field_name always should be integer type and object_id should be number only because you are casting it to integer.
DEMO

IF condition with Postgres; ERROR: syntax error at or near "END" Position: 799

I'm trying to write a sql query to pick some columns based on predetermined conditions. I removed the ELSEIF THEN following block from the code pasted below for which I get the following error:
ERROR: syntax error at or near "END" Position: 799
Could you please help?
Do $$
BEGIN
IF {{f_add_cols_check}} THEN
SELECT colA,colB
FROM"XXX"
END IF;
END;
$$

getting error r: unterminated dollar-quoted string at or near "$total$ declare total integer;

https://www.db-fiddle.com/f/3dpbCYK7FeCMFcWRedBz17/2
I am trying to run my function using postgresql database.but I am getting this error
Schema Error: error: unterminated dollar-quoted string at or near "$total$ declare total integer;"
Schema Error: error: syntax error at or near "SELECT"
Schema Error: error: syntax error at or near "RETURN"
Schema Error: error: unterminated dollar-quoted string at or near "$total$ LANGUAGE plpgsql;"
Query Error: error: function totalrecords() does not exist
can I check my function online ?
CREATE TABLE test (
id INT
);
INSERT INTO test (id) VALUES (1);
INSERT INTO test (id) VALUES (2);
CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
total integer;
BEGIN
SELECT count(*) into total FROM test;
RETURN total;
END;
$total$ LANGUAGE plpgsql;

Postgres: invalid type name colname%TYPE

I am trying to create a function in postgres 9.4 using psql as follows:
CREATE OR REPLACE FUNCTION function_name()
RETURNS VOID AS $$
DECLARE
et_ag "table_name"."ismask"%TYPE;
BEGIN
SET SEARCH_PATH = overlay;
SELECT ismask INTO et_ag FROM OVERLAY.table_name WHERE component_name = 'et_ag';
SELECT case when et_ag != 0 then 'et_ag is not 0' else 'et_ag is 0' end;
END;
$$ LANGUAGE plpgsql;
On running this procedure, I am getting the following error:
ERROR: invalid type name ""table_name"."ismask"%TYPE"
LINE 10: et_ag "table_name"."ismask"%TYPE;
^
********** Error **********
ERROR: invalid type name ""table_name"."ismask"%TYPE"
SQL state: 42601
Character: 167
I referred this post, but am not able to resolve the error. I will greatly appreciate and let me know what wrong I am doing while creating this function.

DB2 trigger on update

I have the table ORDERS with a lot of columns: (ORDERS_ID, MEMBER_ID, STATUS, ANY MORE....)
when in STATUS I will have the change in the new status 'M' I would save ORDERS_ID, MEMBER_ID, STATUS in another table XORDERSAUDIT.
I wrote this :
CREATE or replace TRIGGER Order_Status_Update_Trigger
AFTER UPDATE OF STATUS ON ORDERS
REFERENCING NEW AS N
FOR EACH ROW
WHEN (N.STATUS = 'M')
BEGIN ATOMIC
INSERT INTO XORDERSAUDIT
(ORDER_ID, USERS_ID, STATUS) VALUES (N.ORDERS_ID, N.MEMBER_ID, N.STATUS);
END;
and I have this error:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601,
SQLERRMC=END-OF-STATEMENT;MEMBER_ID, N.STATUS);,
DRIVER=4.19.66 SQLState: 42601 ErrorCode: -104 Error occurred in:
I don't understand the error, I try to correct but encountered the same error, please help me.