PostgreSQL syntax error at end of input not sure why - sql

CREATE OR REPLACE FUNCTION customerGetByLargestSpend()
RETURNS TABLE(
customerID INTEGER,
firstName VARCHAR(20),
Surname VARCHAR(40),
totalBookings BIGINT,
totalSpend Numeric
)
ERROR: syntax error at end of input
LINE 8: )
^
SQL state: 42601
Character: 213
Not sure why this is happening.Any help would be very appreciated. thanks

The function is defined to return a row set, but does not return anything.
The parser expects to find something that returns rows, eg a select statement.
For example:
CREATE OR REPLACE FUNCTION customerGetByLargestSpend()
RETURNS TABLE(
customerID INTEGER,
firstName VARCHAR(20),
Surname VARCHAR(40),
totalBookings BIGINT,
totalSpend Numeric
)
AS $$ SELECT .... $$
LANGUAGE SQL

Related

Error in SQL : invalid input syntax for type integer:

I'm trying to use cast function for a query where the customer_id is given in varchar constraint .
I'm trying to use cast to amend it to an int so that I can use it in my query, but its not working and I don't understand it why.
I was expecting that
select cast (customer_id as integer) as cust_id from customer
would eventually give back the customer-id as integer so that I can run it in my query, but its just giving my error message.
Have you tried replacing integer with int
select cast (customer_id as int) as cust_id from customer
or bigint
select cast (customer_id as bigint) as cust_id from customer
also note that in this tutorial int, bigint or integer is not officially supported for CAST()

Can I have two varchar types next to each other?

I am trying to create schemas in SQL but I am running into issues with my variable types. When I have two varchar types I run into errors but if I only have one type as a varchar my code can execute. What am I doing wrong?
My code
CREATE TABLE IF NOT EXISTS Customer(
user_id integer
ccnum integer
expdate timestamptz
name varchar(30)
email varchar(100)
);
Error
Error: near line 8: in prepare, near "email": syntax error (1)
[Execution complete with exit code 1]
You are missing commas.
CREATE TABLE IF NOT EXISTS Customer(
user_id integer,
ccnum integer,
expdate timestamptz,
name varchar(30),
email varchar(100)
);

how to create function in postgres

how to create function in PostgreSQL am using below code
create table sample(id int primary key)
create table sample2(id int primary key)
create view sampleall
As
select id from sample
union
select id from sample2
while creating below function for above tables
Create FUNCTION CheckFunction (ID INT)
RETURNS BIT AS
$$
BEGIN
IF EXISTS (SELECT 1 FROM sampleall WHERE id = #ID)
BEGIN
RETURN 1
END
RETURN 0
END
$$
LANGUAGE plpgsql;
CREATE TABLE sample3 (ID INT NOT NULL CONSTRAINT CHK_ID CHECK (dbo.CheckFunction(ID) = 1))
and getting below error
ERROR: syntax error at or near "BEGIN"
LINE 8: BEGIN
^
********** Error **********
ERROR: syntax error at or near "BEGIN"
SQL state: 42601
Character: 132
please help to solve the issue
As documented in the manual an IF requires a THEN. Additionally, parameters cannot be prefixed with a #. To avoid a name clash between a parameter name and a column name you should use a different name for the parameter. A common approach is to prefix parameters with p_
However your function is needlessly complex and can be simplified substantially if you return a proper boolean rather than a bit:
Create FUNCTION check_function (p_ID INT)
RETURNS boolean AS
$$
select exists (SELECT 1 FROM sampleall WHERE id = p_id);
$$
LANGUAGE sql;
Your check constraint can then be simplified to:
CREATE TABLE sample3 (ID INT NOT NULL CONSTRAINT CHK_ID CHECK (check_Function(ID));

Error type casts Postgresql function

Hello i have this error
Error: psql:/Samsung/VisualCodeStudioWorkspace/1524335164735.pgsql:95: ERROR: function altaplayer(character varying, integer, integer, character varying, character varying) does not exist
LINE 1: select altaPlayer( varchar 'Drazen Petrovic',52,80, varchar ...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
psql end.
I cant solve for more than two days... ive tried everyhing...
CREATE TYPE player AS (
nom varchar(20),
alçada integer,
pes integer,
posicio varchar(20),
equip varchar(20)
);
CREATE TABLE player_list of player(
primary key(nom)
);
CREATE TABLE team (
jugador player
);
INSERT INTO team VALUES(row('Pepelu',190,90,'Base','Barça'));
INSERT INTO player_list VALUES('Asterix',50,30,'Base','Barça');
INSERT INTO player_list VALUES('Pepelui',100,90,'Alero','Barça');
INSERT INTO player_list VALUES('Manolo',10,50,'Base','Barça');
INSERT INTO player_list VALUES('Juan',50,30,'Base','Barça');
INSERT INTO player_list VALUES('Fernando Martin',50,30,'Pivot','Barça');
INSERT INTO player_list VALUES('Audie Norris',50,30,'Pivot','Barça');
create or replace function altaPlayer ( varchar(20),int,int,varchar(20),varchar(20)) as $$
BEGIN
INSERT INTO player_list VALUES($1,$2,$3,$4,$5);
END
$$ LANGUAGE plpgsql;
select altaPlayer('Drazen Petrovic',52,80,'Escolta','Cibona');
The function result type must be specified:
create or replace function altaplayer ( varchar(20),int,int,varchar(20),varchar(20))
returns void -- !!!
as $$
begin
insert into player_list values($1,$2,$3,$4,$5);
end
$$ language plpgsql;
You missed to put the keyword RETURNS.
create or replace function altaPlayer
(varchar(20),int,int,varchar(20),varchar(20))
RETURNS void as $$

Oracle: Invalid Datatype Error on Create Table

I'm trying to simply create an Oracle table and I'm getting an invalid datatype error, but it seems that's there nothing wrong with my create statement (unless I'm completely overlooking something).
CREATE TABLE SCRIPT_LINE
(
ID INTEGER PRIMARY KEY NOT NULL,
EPISODE_ID INTEGER NOT NULL,
LINE_NUMBER INTEGER,
RAW_TEXT VARCHAR(4000),
TIMESTAMP NUMERIC,
SPEAKING_LINE BOOLEAN,
CHARACTER_ID INTEGER NOT NULL,
LOCATION_ID INTEGER NOT NULL,
CHARACTER VARCHAR(500),
LOCATION VARCHAR(500),
SPOKEN_WORD VARCHAR(4000),
WORD_COUNT INTEGER
);
Any idea where the error could be? Thanks in advance.
Oracle does not have a BOOLEAN datatype
As Oracle does not support Boolean, the best alternative would be char(1)
Y/N. alternatively use number (1) 0/1