将SQL转换为PL / pgSQL

时间:2014-07-16 11:56:13

标签: sql-server postgresql tsql plpgsql

我有很多函数和存储过程从MS SQL服务器导出到PostgreSQL

你能帮我解决这个问题,并为我提供PL / pgSQL中与PostgreSQL相同的功能吗?

USE [METRICS]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[fnPSR_CtrlNouveauProjet]
(
    @InstanceID int,
    @Month_Param nvarchar(15)
)
RETURNS nvarchar(2)
AS
BEGIN
    DECLARE @ReturnValue NVARCHAR(2), @Max_ID_Token int
    DECLARE @Month_Param_Prev nvarchar(15)
    SELECT @Month_Param_Prev = CASE WHEN @Month_Param like '%-08' THEN  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 2, 0), 105),4,10) 
         ELSE  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 1, 0), 105),4,10) end 
    DECLARE @KIS_FileName NVARCHAR(100)
    SELECT 
        @KIS_FileName = KI.KIS_FileName 
    FROM 
        PSR_KIS_INSTANCE KI (NOLOCK)
    WHERE
        KI.ID_Instance = @InstanceID

    SELECT @Max_ID_Token = MAX(KI.ID_Token) FROM DBO.PSR_KIS_INSTANCE KI (NOLOCK) WHERE KI.KIS_FileName = @KIS_FileName and KI.Reporting_Month = @Month_Param_Prev

    IF (EXISTS(SELECT 
        KI.ID_Instance 
    FROM 
        PSR_KIS_INSTANCE KI (NOLOCK) 
    WHERE 
        KI.KIS_FileName  = @KIS_FileName
        and KI.Reporting_Month = @Month_Param_Prev 
        and KI.ID_Token = @Max_ID_Token))

    BEGIN
        SET @ReturnValue = ' '
    END
    ELSE
    BEGIN
        SET @ReturnValue = 'N'
    END

    RETURN @ReturnValue

END

1 个答案:

答案 0 :(得分:1)

我的T-SQL真的很生疏,但是这样的事情应该让你开始:

这是未经测试的,不会编译

create or replace function fnPSR_CtrlNouveauProjet(p_instance_id integer, p_month_param varchar)
  return varchar
as
$body$
declare
   l_return_value VARCHAR(2);
   l_max_id_token integer;
   l_mont_param_prev varchar(15);
   l_kis_filename varchar(100);

   -- I have NO idea what the following is doing.
   SELECT @Month_Param_Prev = 
       CASE WHEN @Month_Param like '%-08' 
         THEN  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 2, 0), 105),4,10) 
         ELSE  SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+@Month_Param) - 1, 0), 105),4,10) 
       end 

    SELECT ki.kis_filename 
      INTO l_kis_filename
    FROM psr_kis_instance ki
    WHERE ki.id_instance = p_instance_id;

    SELECT MAX(ki.id_token) 
      INTO l_max_id_token
    FROM psr_kis_instance ki 
    WHERE ki.kis_filename = l_kis_filename
      AND reporting_month = l_month_param_prev;

    SELECT count(*)
       INTO l_count 
    FROM psr_kis_instance ki
    WHERE ki.kis_filename = l_kis_filename
      and ki.reporting_month = l_month_param_prev
      and ki.id_token = l_max_id_token;

    if l_count = 1 then 
      l_return_value = ' '
    else
      l_return_value = 'N';
    end if;

    RETURN l_return_value;
END;
$body$
language plpgsql;

一些注意事项:

    Postgres中不需要
  • (NOLOCK)。读者从不阻止作家和作家从不阻止读者。
  • 没有NVARCHAR数据类型。只要您使用UTF-8
  • 创建数据库,VARCHAR就一样好
  • Postgres中也不需要dbo前缀。但是,您可以创建具有该名称的架构,以便在需要时更轻松地进行迁移。
  • 习惯了不带引号的名称被折叠成小写的事实。因此,不要使用CamelCaseIdentifier采用习惯来使用下划线分隔单词。使用类似fn_psr_ctrl_nouveau_projet的内容。不带引号的标识符不区分大小写,因此您也可以使用fn_PSR_Ctrl_Nouveau_Projet
  • 我觉得这三个SQL语句可以合并为一个(使效率更高),但是由于你没有解释你在做什么,这很难说。