2017-01-19 3 views
1

Wie kann ich diese SP/Funktion von MySQL zu PostgreSQL konvertieren?PostgreSQL: Wenn Else Anweisung zum Einfügen, Löschen und Aktualisieren in einer Funktion

DELIMITER $$ 
CREATE DEFINER=`user_name`@`%` PROCEDURE `sp_TABLE_name`(
pTransType int, 
pId bigint, 
pName varchar(250), 
pStartFrom datetime, 
pStartTo datetime, 
pSignature longblob) 
BEGIN 

if pTransType = 1 then 

    insert into TABLE_name (Id, Name, startfrom, startto, signature)  
     values(pId, pName, pStartFrom, pStartTo, pSignature); 

end if; 

if pTransType = 2 then 

    update TABLE_name set 
      Name = pName, 
      startfrom = pStartFrom, 
      startto = pStartTo, 
      signature = pSignature 
     where Id = pId; 

end if; 

if pTransType = 3 then 

    delete from TABLE_name where id = pId; 

end if; 

END$$ 
DELIMITER ; 

Ich versuchte dann, wenn Statement aber andere Fehler zeigt .. Gibt es eine andere Möglichkeit, es zu tun?

Antwort

1

ich denke, es würde wie folgt aussehen:

CREATE OR REPLACE FUNCTION sp_TABLE_name(pTransType int, pId bigint, pName varchar(250), 
    pStartFrom timestamp, pStartTo timestamp, pSignature bytea) 
    RETURNS void AS 
$BODY$ 
BEGIN 
    if pTransType = 1 then 

     insert into TABLE_name (Id, Name, startfrom, startto, signature)  
     values(pId, pName, pStartFrom, pStartTo, pSignature); 

    elsif pTransType = 2 then 

     update TABLE_name set 
       Name = pName, 
       startfrom = pStartFrom, 
       startto = pStartTo, 
       signature = pSignature 
      where Id = pId; 

    elsif pTransType = 3 then 

     delete from TABLE_name where id = pId; 

    end if; 
END; 
$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 

Hinweis Ich habe Ihre Datentypen in PostgreSQL Äquivalenten entsprechen müssen (oder meine beste Vermutung, was sie sein würde).

+0

Versuchen Sie dies. Danke vielmals! – fLen

Verwandte Themen