1

我是 postgresql 的初学者,我想在 sql 中创建一个过程。我创建了一个将记录插入表中并返回 id 的函数。但是有问题,我不想要任何结果,除了输出参数。

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;
4

1 回答 1

0

SQL 函数不能使用变量,因此您不需要它们。将其更改为:

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;
于 2013-01-30T14:29:16.850 回答