1

I'm slowly learning more about PostgreSQL, as we are attempting to move to it from MSSQL Server.

In MSSQL I have the following code:

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

I'm trying to work out how to do the sp_executesql part in a PostgreSQL function. I believe that I can do the rest, but most of the examples that I have found show a simple select with maybe a few variables, whereas I need to execute another function, with parameters, where the function name is text in a table.

Many Thanks.

1

2 Answers 2

3

In case you want to execute a function with parameters

EXECUTE 'SELECT Value FROM ' || v_workbookCommandText || '(ApplicationContainerID :=$1)'
INTO v_actualValue
USING v_applicationContainerID;

In case you need select records a function, you can using INOUT refcursor variable

EXECUTE 'SELECT Value FROM ' || v_workbookCommandText || '(ApplicationContainerID :=$1, refcur:= $2)'
INTO v_actualValue
USING v_applicationContainerID, my_cursor;
Sign up to request clarification or add additional context in comments.

Comments

0

I think what you want to do is EXECUTE 'some string', like this:

EXECUTE 'SELECT count(*) FROM mytable WHERE inserted_by = $1 AND inserted <= $2'
   INTO c
   USING checked_user, checked_date;

Another option is to create and use your own PL/PGSQL functions.

1 Comment

Not really ... unless 'some string' can be the name of another function with parameters ... but cheers anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.