存储过程SQL Server迁移到PostgreSQL

时间:2016-05-12 21:28:07

标签: sql-server postgresql stored-procedures plpgsql

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

SQL Server存储过程:

    $(document).ready(function(){
    var purpleBackgroundCss  = {"background-color": "purple"};
    $(".classname").closest('div').css(purpleBackgroundCss).children('div').css(purpleBackgroundCss);
    });

Postgres功能:

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

当我尝试使用:

在pgadmin中测试时
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;

我收到错误:

  

错误:函数addfullsig(整数,bigint)不存在

我不确定我对postgresql的转换是否正确,特别是在使用bytea for binary(16)而不是bit(16)时。任何帮助或见解将不胜感激。

1 个答案:

答案 0 :(得分:2)

将1和0的序列解析为整数,并猜测它是bigint的类型。

使用位串语法:

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

查看更多here