我慢慢地學習了關於PostgreSQL的更多信息,因爲我們試圖從MSSQL Server遷移到它。postgresql執行動態sql命令
在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的一部分。我相信我可以完成剩下的工作,但是我發現的大多數示例都顯示了一個簡單的選擇,可能有一些變量,而我需要使用參數執行另一個函數,其中函數名稱是表格中的文本。
很多謝謝。
https://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN –