我正在慢慢了解PostgreSQL,因为我们正试图从MSSQL Server迁移到它。
在MSSQL中,我有以下代码:
DECLARE ServiceabilityParameters
CURSOR FORWARD_ONLY READ_ONLY STATIC LOCAL FOR
SELECT WorkbookParameterType.ID,
WorkbookParameterType.Name,
WorkbookParameter.DefaultValue,
WorkbookParameter.CommandText
FROM WorkbookParameter
JOIN WorkbookParameterType ON WorkbookParameterType.ID = WorkbookParameter.WorkbookParameterTypeID
JOIN WorkbookParameterDirectionType ON WorkbookParameterDirectionType.ID = WorkbookParameter.WorkbookParameterDirectionTypeID
AND WorkbookParameterDirectionType.Writable = 1
WHERE WorkbookParameter.WorkbookID = @WorkbookID
OPEN ServiceabilityParameters
FETCH NEXT FROM ServiceabilityParameters INTO @WorkbookParameterTypeID, @WorkbookParameterTypeName, @WorkbookDefaultValue, @WorkbookCommandText
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @ActualValue NVARCHAR(256) = NULL
IF @WorkbookCommandText IS NOT NULL
BEGIN
EXEC sp_executesql @statement = @WorkbookCommandText,
@params = N'@ApplicationContainerID INT, @Value NVARCHAR(256) OUTPUT',
@ApplicationContainerID = @ApplicationContainerID,
@Value = @ActualValue OUTPUT
END
IF @ActualValue IS NULL AND @WorkbookDefaultValue IS NOT NULL
BEGIN
SET @ActualValue = @WorkbookDefaultValue
END
INSERT @InputParameters (
ID, Name, Value
) VALUES (
@WorkbookParameterTypeID, @WorkbookParameterTypeName, @ActualValue
)
FETCH NEXT FROM ServiceabilityParameters INTO @WorkbookParameterTypeID, @WorkbookParameterTypeName, @WorkbookDefaultValue, @WorkbookCommandText
END
CLOSE ServiceabilityParameters
DEALLOCATE ServiceabilityParameters
我试图找出如何在PostgreSQL函数中执行sp_executesql部分。我相信我可以完成剩下的工作,但是我发现的大多数示例都显示了一个简单的选择,可能有一些变量,而我需要执行另一个带参数的函数,其中函数名是表中的文本。 / p>
非常感谢。
答案 0 :(得分:2)
如果要使用参数执行功能
EXECUTE 'SELECT Value FROM ' || v_workbookCommandText || '(ApplicationContainerID :=$1)'
INTO v_actualValue
USING v_applicationContainerID;
如果需要选择记录功能,可以使用INOUT refcursor变量
EXECUTE 'SELECT Value FROM ' || v_workbookCommandText || '(ApplicationContainerID :=$1, refcur:= $2)'
INTO v_actualValue
USING v_applicationContainerID, my_cursor;
答案 1 :(得分:0)
我认为您要做的是EXECUTE 'some string'
,例如this:
EXECUTE 'SELECT count(*) FROM mytable WHERE inserted_by = $1 AND inserted <= $2'
INTO c
USING checked_user, checked_date;
另一个选择是创建和使用您自己的PL/PGSQL functions。