Using the CS0120 superview to execute a SQL transaction

If I use a transaction that has two statements in it, only the first statement executes
In the example below only the UPDATE statement executes and I am not able to retrieve the result


csUpdateW.Browse "BEGIN TRANSACTION; " & _
  "UPDATE OWID SET LASTVALUE = LASTVALUE + 1; " & _
  "SELECT LASTVALUE FROM OWID; " & _
  "COMMIT TRANSACTION;", True
csUpdateW.InternalSet 256

Do While csUpdateW.Fetch
  resultval = csUpdateW.Fields(0).Value
Loop
csUpdateW.Close

I am trying to use the superview instead of an ADO connection for various reasons.

Any suggestions are appreciated, thank you.

Parents
  • 0

    I use a three part solution.

    First - create a table with one field that is an Identity field:

    CREATE TABLE XXSequenceTable
    (
        ID BIGINT IDENTITY
    );

    Next, create a SQL Procedure that will start a transaction, insert a record and rollback that transaction.  Table identity numbers are not reset within a Begin/Rollback pair.  The procedure will return the current Identity value for the table (which has been incremented by one).

    CREATE PROCEDURE dbo.GetSEQUENCE ( @value BIGINT OUTPUT)
    AS
        --Act like we are INSERTing a row to increment the IDENTITY
        BEGIN TRANSACTION;
        INSERT XXSequenceTable WITH (TABLOCKX) DEFAULT VALUES;
        ROLLBACK TRANSACTION;
        --Return the latest IDENTITY value.
        SELECT @value = SCOPE_IDENTITY();
    GO

    DECLARE @value BIGINT;
    EXECUTE dbo.GetSEQUENCE @value OUTPUT;
    SELECT @value AS [@value];
    GO

    Now write some ugly code using CS0120 and pull the value out.  It comes back as the dreaded Byte Array so you get to do some math to return the actual number:

        Dim p0 As Integer
        Dim p1 As Integer
        Dim p2 As Integer
        Dim p3 As Integer

        Dim q As AccpacCOMAPI.AccpacView
        DBLink.OpenView "CS0120", q
        q.Browse "DECLARE @value BIGINT; EXECUTE dbo.GetSEQUENCE @value OUTPUT; SELECT @value AS [@value];", True
        q.InternalSet 256
        q.Fetch
        p0 = CInt(q.Fields(0).Value(0))
        p1 = CInt(q.Fields(0).Value(1))
        p2 = CInt(q.Fields(0).Value(2))
        p3 = CInt(q.Fields(0).Value(3))
        
        LogUNIQ = p0 + (p1 * (2 ^ 8)) + (p2 * (2 ^ 16)) + (p3 * (2 ^ 24))
       

Reply
  • 0

    I use a three part solution.

    First - create a table with one field that is an Identity field:

    CREATE TABLE XXSequenceTable
    (
        ID BIGINT IDENTITY
    );

    Next, create a SQL Procedure that will start a transaction, insert a record and rollback that transaction.  Table identity numbers are not reset within a Begin/Rollback pair.  The procedure will return the current Identity value for the table (which has been incremented by one).

    CREATE PROCEDURE dbo.GetSEQUENCE ( @value BIGINT OUTPUT)
    AS
        --Act like we are INSERTing a row to increment the IDENTITY
        BEGIN TRANSACTION;
        INSERT XXSequenceTable WITH (TABLOCKX) DEFAULT VALUES;
        ROLLBACK TRANSACTION;
        --Return the latest IDENTITY value.
        SELECT @value = SCOPE_IDENTITY();
    GO

    DECLARE @value BIGINT;
    EXECUTE dbo.GetSEQUENCE @value OUTPUT;
    SELECT @value AS [@value];
    GO

    Now write some ugly code using CS0120 and pull the value out.  It comes back as the dreaded Byte Array so you get to do some math to return the actual number:

        Dim p0 As Integer
        Dim p1 As Integer
        Dim p2 As Integer
        Dim p3 As Integer

        Dim q As AccpacCOMAPI.AccpacView
        DBLink.OpenView "CS0120", q
        q.Browse "DECLARE @value BIGINT; EXECUTE dbo.GetSEQUENCE @value OUTPUT; SELECT @value AS [@value];", True
        q.InternalSet 256
        q.Fetch
        p0 = CInt(q.Fields(0).Value(0))
        p1 = CInt(q.Fields(0).Value(1))
        p2 = CInt(q.Fields(0).Value(2))
        p3 = CInt(q.Fields(0).Value(3))
        
        LogUNIQ = p0 + (p1 * (2 ^ 8)) + (p2 * (2 ^ 16)) + (p3 * (2 ^ 24))
       

Children