TransWikia.com

postgresql execute dynamic sql command

Stack Overflow Asked by Boothy on November 22, 2021

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.

2 Answers

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;

Answered by XNH on November 22, 2021

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.

Answered by hdiogenes on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP