1

I am a beginner for postgresql and I would like to create a procedure in sql. I created a function that inserts a record into a table and it returns the id. But there is something wrong, I don't want any result except for the output parameter.

CREATE OR REPLACE FUNCTION public."InsertVideo"
(
  OUT  out_scope_id       integer,
  IN   in_youtubeidvideo  varchar[],
  IN   in_title           varchar,
  IN   in_rating          double precision,
  IN   in_viewcount       integer         
)
RETURNS integer AS
$$

DECLARE
id INTEGER;

INSERT INTO Video 
(                 
  YoutubeIdVideo,       
  Title,                
  Rating,               
  ViewCount,                      
  DataAdded,
  ConvertedFlag,
  SchedulingFlag
)

VALUES 
(
  in_youtubeidvideo,                 
    in_title,            
    in_rating,           
    in_viewcount,
  now(),
  false,
  false
);

SELECT id := CURRVAL(pg_get_serial_sequence('public.video','IDVideo'));
RETURN id;

$$
LANGUAGE 'sql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER;
0

1 Answer 1

0

A SQL function can't use variables and you don't need them for this. Changed it to:

CREATE OR REPLACE FUNCTION public."InsertVideo" (
    IN in_youtubeidvideo varchar[],
    IN in_title varchar,
    IN in_rating double precision,
    IN in_viewcount integer
) RETURNS integer AS $$

INSERT INTO Video (
    YoutubeIdVideo,
    Title,
    Rating,
    ViewCount,
    DataAdded,
    ConvertedFlag,
    SchedulingFlag
) VALUES (
    in_youtubeidvideo,
    in_title,
    in_rating,
    in_viewcount,
    now(),
    false,
    false
);

SELECT CURRVAL(pg_get_serial_sequence('public.video','IDVideo'));

$$ LANGUAGE 'sql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.