как напечатать переменную в функции postgres - PullRequest
1 голос
/ 25 июня 2019

Как напечатать bwtid или использовать для выполнения запроса

create or replace function dashboard.rspCreateAndInsertUpdateBranchWiseTurnOverTable(
        AccountGroupIdCol           NUMERIC(10,0),
        SourceBranchIdCol           NUMERIC(10,0),
        DestinationBranchIdCol      NUMERIC(10,0),
        WayBillTypeIdCol            NUMERIC(1,0),
        TotalLRCol                  NUMERIC(20,0),
        TotalBookingAmountCol       NUMERIC(20,2),
        TotalDeliveryAmountCol      NUMERIC(20,2),
        GrandTotalAmountCol         NUMERIC(20,2),
        TotalQuantityCol            NUMERIC(20,0),
        TotalActualWeightCol        NUMERIC(20,3),
        TotalChargedWeightCol       NUMERIC(20,3),
        BookingMonthCol             NUMERIC(2,0),
        BookingYearCol              NUMERIC(4,0),
        schemaName                  TEXT,   
        TableNameCol                TEXT
    ) returns numeric as
    $$
    DECLARE
        nameid                  TEXT;       -- unique id for tables concat(Year,Month21077, 21089,2,2018,8)
        tablename               TEXT;       -- table name
        indexname               TEXT;       -- index name
        indexname1              TEXT;       -- index name
        sequencename            TEXT;
        createtablescr          TEXT;
        indexscr                TEXT;       -- index on booking date column
        indexscr1               TEXT;       -- index on booking date column
        inserttablescr          TEXT;
        sequencescr             TEXT;
        selecttablescr          TEXT;
         bwtid                  integer;
    begin
        tablename       = schemaName || '.' || TableNameCol;
        indexname       = TableNameCol || '_SourceBranchId_idx';
        indexname1      = TableNameCol || '_DestinationBranchId_idx';
        sequencename    = TableNameCol || '_BranchWiseTurnOverId_seq';

        sequencescr     = 'CREATE SEQUENCE IF NOT EXISTS ' || sequencename || ' `enter code here`INCREMENT 1 START 1 CACHE 1';

        createtablescr  = 'CREATE TABLE IF NOT EXISTS ' || tablename || ' ( 
            BranchWiseTurnOverId    SERIAL PRIMARY KEY,
            AccountGroupId          NUMERIC(10,0) not null,
            SourceBranchId          NUMERIC(10,0) not null,
            DestinationBranchId     NUMERIC(10,0) not null,
            WayBillTypeId           NUMERIC(1,0) not null,
            TotalLR                 NUMERIC(20,0) not null,
            TotalBookingAmount      NUMERIC(20,2) not null,
            TotalDeliveryAmount     NUMERIC(20,2) not null,
            GrandTotalAmount        NUMERIC(20,2) not null,
            TotalQuantity           NUMERIC(20,0) not null,
            TotalActualWeight       NUMERIC(20,3) not null,
            TotalChargedWeight      NUMERIC(20,3) not null,
            BookingMonth            NUMERIC(2,0) not null,
            BookingYear             NUMERIC(4,0) not null
        );';

        indexscr    = 'CREATE INDEX IF NOT EXISTS ' || indexname || ' ON ' || tablename || ' (SourceBranchId);';
        indexscr1   = 'CREATE INDEX IF NOT EXISTS ' || indexname1 || ' ON ' || tablename || ' (DestinationBranchId);';

        selecttablescr = 'select branchwiseturnoverid INTO bwtid from '|| tablename || ' where AccountGroupId='|| AccountGroupIdCol ||' and SourceBranchId='|| SourceBranchIdCol ||' and DestinationBranchId='|| DestinationBranchIdCol ||' and WayBillTypeId='||WayBillTypeIdCol ||';';

        inserttablescr  = 'INSERT INTO ' || tablename || '(AccountGroupId, SourceBranchId, DestinationBranchId, WayBillTypeId, TotalLR, TotalBookingAmount,
                                TotalDeliveryAmount, GrandTotalAmount, TotalQuantity, TotalActualWeight, TotalChargedWeight, BookingMonth, BookingYear)
                        VALUES (' || AccountGroupIdCol || ',' || SourceBranchIdCol || ',' || DestinationBranchIdCol || ',' || WayBillTypeIdCol || ',' || TotalLRCol
                                || ',' || TotalBookingAmountCol || ',' || TotalDeliveryAmountCol || ',' || GrandTotalAmountCol || ',' || TotalQuantityCol || ',' || TotalActualWeightCol
                                || ',' || TotalChargedWeightCol || ',' || BookingMonthCol || ',' || BookingYearCol || ');';


        EXECUTE sequencescr;
        EXECUTE createtablescr;
        EXECUTE indexscr;
        EXECUTE indexscr1;
        EXECUTE inserttablescr;
        EXECUTE selecttablescr; 
        RAISE NOTICE USING MESSAGE = bwtid;


    end;
    $$
    language 'plpgsql';
select dashboard.rspCreateAndInsertUpdateBranchWiseTurnOverTable(270,19168,19168,4,1,1000,1000,1000,5,50,50,6,2019,'dashboard','branchwiseturnover_2019_6');

1 Ответ

0 голосов
/ 25 июня 2019

Чтобы ответить на прямой вопрос «Как напечатать btwid», используйте следующий синтаксис:

RAISE NOTICE '%', bwtid;
...