我有很多函数和存储过程从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
答案 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;
一些注意事项:
(NOLOCK)
。读者从不阻止作家和作家从不阻止读者。NVARCHAR
数据类型。只要您使用UTF-8 VARCHAR
就一样好
dbo
前缀。但是,您可以创建具有该名称的架构,以便在需要时更轻松地进行迁移。 CamelCaseIdentifier
采用习惯来使用下划线分隔单词。使用类似fn_psr_ctrl_nouveau_projet
的内容。不带引号的标识符不区分大小写,因此您也可以使用fn_PSR_Ctrl_Nouveau_Projet
。