简体   繁体   中英

POSTGRES ALTER procedure syntax error I can't figure out

Here is the error:

ERROR: [GET_ALL_DATASETS_BY_DATE] syntax error at or near "LANGUAGE"
LINE 3:         LANGUAGE SQL

Here is the code

def PROCEDURE_GET_ALL_DATASETS_BY_DATE(conn):
    print("CREATING [GET_ALL_DATASETS_BY_DATE] PROCEDURE")
    try:
        cursor = conn.cursor()
        cursor.execute(f"""
        ALTER PROCEDURE GET_ALL_DATASETS_BY_DATE(how_many int)
        LANGUAGE SQL
        AS $$
        SELECT U.username, F.File_PATH "Path", Description "Desc", F.Date_Time "Date", F.File_size "Size"
            FROM USERS as U
            INNER JOIN FILES as F
            ON F.UserId = U.User_Id
            ORDER BY F.Date_Time
            limit how_many  
            $$;
        """)
        conn.commit()
    except Exception as e:
        cursor.execute("ROLLBACK")
        print("ERROR: [GET_ALL_DATASETS_BY_DATE] " + str(e))

I feel like I am missing something super simple...Thank you for your time. Any help is appreciated

here is the documentation link to how I've been modelling my procedures.

https://www.postgresql.org/docs/current/sql-createprocedure.html

if you wanna bring the table by using join. You can use the code below.

PostgreSQL is not like MSSQL or TSQL. In MSSQL and TSQL we can use procedure as direct. But In Postgres, if we want to use select processing, we have to specify columns on our query.

Example:

    CREATE OR REPLACE FUNCTION public.myposts(_user_id integer)
 RETURNS TABLE(post_id integer, create_user_id integer, ad_name character varying, topic character varying, content_values text, slug_url character varying)
 LANGUAGE plpgsql
AS $function$
BEGIN
   RETURN QUERY
   select distinct posts.id,posts.create_user_id,users.ad_name,posts.topic,posts.content_values,posts.slug_url from posts 
   inner join users on users.id = posts.create_user_id
   inner join post_receivers on post_receivers.post_id = posts.id
   where post_receivers.user_id = _user_id;
END
$function$
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM