Gespeicherte Prozeduren mit FB_DBStoredProceduresRecordArray

Mit Hilfe des Funktionsbausteins FB_DBStoredProceduresRecordArray können Parameter als INPUT, OUTPUT oder INOUT deklariert werden und den Gespeicherten Prozeduren übergeben werden. So können komplexe SQL-Kommandos am Datenbank Server vorprogrammiert und müssen nur noch vom TwinCAT Database Server angetriggert werden. Im Gegensatz zum FB_DBStoredProceduresRecordReturn Baustein können mehrere Datensätze mit einem Aufruf zurückgeliefert werden.

Download "Beispiel mit Gespeicherter Prozedur" sample9.zip

Gespeicherte Prozeduren mit FB_DBStoredProceduresRecordArray 1:

Verwendeter Datenbanktyp

MS SQL (MS SQL Server 2008)

Kompatible Datenbanktypen

MS SQL, MySQL, Oracle

Verwendete Funktionsbausteine

FB_DBStoredProceduresRecordArray

Einzubindende Bibliotheken

"TcDatabase.lib", "TcSystem.lib", "TcBase.lib", "STANDARD.lib",  "TcUtilities.lib"

Download Dateiliste

FB_DBStoredProceduresRecordArray_Sample.pro, CurrentConfigDataBase.xml, SQLQuery2.sql

Das folgende Beispiel zeigt ihnen den Aufruf einer einfachen Gespeicherten Prozedur mit einem Eingangsparameter und Rückgabedatensatz. Die Prozedur wurde erstellt an einem Microsoft SQL Server 2008.

Code der Gespeicherten Prozedur SP_GetAddressByCustomerID

CREATE PROCEDURE [SP_GetAddressByCustomerID] 
    @Customer_ID bigint
AS
BEGIN
    SELECT tbl_Customer.ID, tbl_Customer.Name, tbl_Customer.Customer, tbl_Products.SerNum, tbl_Products.Product, tbl_Products.Info, tbl_Pos.Timestamp FROM 
        tbl_Pos JOIN tbl_Customer ON tbl_Pos.CustomerNum = tbl_Customer.ID 
        JOIN tbl_Products ON tbl_Pos.ProductNum = tbl_Products.SerNum
    WHERE
        tbl_Pos.CustomerNum = @Customer_ID;
END

Variablendeklaration in der SPS

PROGRAM MAIN
VAR
    R_TRIG1: R_TRIG;
    bREAD : BOOL := FALSE;

    nState: BYTE;

    arrParaList: ARRAY [0..0] OF ST_DBParameter;

    nCustomerID: DINT := 12345;

    FB_DBStoredProceduresRecordArray1: FB_DBStoredProceduresRecordArray;

    nCustomerID: DINT := 12345;
    nRecordStartIndex: UDINT;
    stRecordArr: ARRAY [1..25] OF ST_Record;
    nRecs: UDINT;

    bBusy: BOOL;
    bErr: BOOL;
    nErrid: UDINT;
    stSqlstate: ST_DBSQLError;
END_VAR

Datensatzstruktur in der SPS (ST_Record)

TYPE ST_Record :
STRUCT
    nID : T_ULARGE_INTEGER;
    sCustomer : STRING(50);
    sName : STRING(50);
    nProductNum : DINT;
    sProductName : STRING(50);
    sProductInfo : T_MaxString;
    tTimestamp : DT;
END_STRUCT
END_TYPE

SPS Programm

R_TRIG1(CLK:=bREAD);
IF R_TRIG1.Q AND NOT bBusy THEN
    nState := 1;
END_IFCASE nState OF
    0:
        ;
    1:(*Init of the parameters*)
        arrParaList[0].sParameterName := '@Customer_ID';
        arrParaList[0].eParameterDataType := eDBColumn_Integer;
        arrParaList[0].eParameterType := eDBParameter_Input;
        arrParaList[0].cbParameterValue := SIZEOF(nCustomerID);
        arrParaList[0].pParameterValue := ADR(nCustomerID);

        nState := 2;
    2:(*Start the stored procedure "SP_GetCustomerPosition"*)
        FB_DBStoredProceduresRecordArray1(
            sNetID:= ,
            hDBID:= 1,
            sProcedureName:= 'SP_GetCustomerPositions',
            cbParameterList:= SIZEOF(arrParaList),
            pParameterList:= ADR(arrParaList),
            nStartIndex:= nRecordStartIndex,
            nRecordCount:= 25,
            cbRecordArraySize:= SIZEOF(stRecordArr),
            pDestAddr:= ADR(stRecordArr),
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> bBusy,
            bError=> bErr,
            nErrID=> nErrid,
            sSQLState=> stSqlstate,
            nRecords=> nRecs);
        
        IF NOT bBusy THEN
            FB_DBStoredProceduresRecordReturn1(bExecute:= FALSE);
            nState := 0;
        END_IFEND_CASE

Visualisierung

Gespeicherte Prozeduren mit FB_DBStoredProceduresRecordArray 2: