Search code examples
postgresqlstored-functions

How to implement stored procedure/function in postgresql version 12?


How I can implement it? I am confused in syntax also?

CREATE OR REPLACE Function dailyyyy_volation_routine(_ispeed_count integer,_rlvd_count integer,_stopline_count integer,_wrongdir_count integer
                                                     ,_wronglane_count integer,_zebracross_count integer,_total_count integer)
RETURNS void AS $$
BEGIN
INSERT INTO temp_daily_stats(ispeed_count,rlvd_count,stopline_count,wrongdir_count,wronglane_count,zebracross_count,total_count)
with t1 as (SELECT SUM(ispeed_count) as ispeed_count from temp_hourly_stats),
t3 as (SELECT SUM(rlvd_count) as rlvd_count from temp_hourly_stats),
t4 as (SELECT SUM(stop_line_count) as stopline_count from temp_hourly_stats),
t5 as (SELECT SUM(wrong_dir_count) as wrong_dir_count from temp_hourly_stats),
t6 as (SELECT SUM(wrong_lane_count) as wrong_lane_count from temp_hourly_stats),
t7 as (SELECT SUM(zebra_cross_count) as zebra_cross_count from temp_hourly_stats),
t8 as (SELECT @tc := SUM(total_count) as total_day_count from temp_hourly_stats),
SELECT DISTINCT t1.ispeed_count,t3.rlvd_count,t4.stopline_count,t5.wrong_dir_count,t6.wrong_lane_count,t7.zebra_cross_count,t8.total_day_count
FROM t1,t3,t4,t5,t6,t7,t8 limit 1;
END
$$ 
LANGUAGE SQL;


I want to put data from one table to another using postgresql function?*


Solution

  • Here is an attempt to translate your function to correct code:

    CREATE OR REPLACE Function dailyyyy_volation_routine(
       _ispeed_count integer,
       _rlvd_count integer,
       _stopline_count integer,
       _wrongdir_count integer,
       _wronglane_count integer,
       _zebracross_count integer,
    _total_count integer) RETURNS void AS
    $$INSERT INTO temp_daily_stats(
       ispeed_count,
       rlvd_count,
       stopline_count,
       wrongdir_count,
       wronglane_count,
       zebracross_count,
       total_count
    )
    SELECT SUM(ispeed_count),
           SUM(rlvd_count),
           SUM(stop_line_count),
           SUM(wrong_dir_count),
           SUM(wrong_lane_count),
           SUM(zebra_cross_count),
           SUM(total_count)
    FROM temp_hourly_stats$$ 
    LANGUAGE SQL;