2016-05-12 138 views
0

我正在将存储过程从SQL Server迁移到PostgreSQL。我已经将存储过程转换为Postgres函数。存储过程SQL Server迁移到PostgreSQL

SQL Server存储过程:

CREATE PROCEDURE AddFullSig @CandID int, @FullSig binary(16) 
AS 
    if not exists (select pub_id 
        from local_lib 
        where cand_id = @CandID and Full_sig = @FullSig) 
     insert into local_lib 
     values(@CandID, 1, @FullSig) 
    else 
     update local_lib 
     set dup_count = dup_count + 1 
     where cand_id = @CandID 
      and Full_sig = @FullSig 

    select pub_id 
    from local_lib 
    where cand_id = @CandID and Full_sig = @FullSig 

    RETURN 

Postgres的功能:

create type addfullsig_return_type as(
    pub_id int 
); 

create or replace function addfullsig(p_candid int, p_fullsig bytea) 
returns addfullsig_return_type as $$ 
begin 

if not exists(select pub_id from local_lib where cand_id = p_candid and full_sig = p_fullsig) then 
    insert into local_lib values(default, p_candid, 1, p_fullsig); 
else 
    update local_lib set dup_count = dup_count + 1 where cand_id = p_candid and full_sig = p_fullsig; 
end if; 

select pub_id from local_lib where 
cand_id = p_candid and full_sig = p_fullsig; 
end; 
$$ language plpgsql; 

当我尝试在pgAdmin的测试此使用:

select * from addfullsig(3,1010111011111011); 

我得到的错误:

ERROR: function addfullsig(integer, bigint) does not exist

我不确定我的转换为postgresql是否正确,特别是在使用bytea代替二进制(16)而不是位(16)时。任何帮助或见解将不胜感激。

+1

你的第二个参数是一个'bytea'不是'long'值。有关如何传递“blob”文字的详细信息,请参见手册:http://www.postgresql.org/docs/current/static/datatype-binary.html –

+0

而且,使用'插入冲突“而不是if语句。 –

回答

1

1和0的序列被解析为一个整数文字并猜测它是bigint类型。

使用位串语法:

select * from addfullsig(3,B'1010111011111011'); 

查看更多here