skip to Main Content

I’m more of a Microsoft T-SQL guy – not great, but I’ve worked with it a bit.

In the Visual Studio code, I have this:

public static DataSet GetModels(string currentLogical, string fullTable, string library)
{
    var tableCurMd3LF = new DataTable(currentLogical);
    var tableCurMdlPF = new DataTable(fullTable);
    var sql = $@"
SELECT CMDESC, CMSPRE, CMTOLM
FROM {library}.CURMD3LF
ORDER BY CMSEQ ASC
FOR READ ONLY WITH UR;

SELECT CMDESC, CMSPRE, CMTOLM
FROM {library}.CURMDLPF
ORDER BY CMSEQ ASC
FOR READ ONLY WITH UR;
";
    using (var cmd = new OleDbCommand(sql, new OleDbConnection($"{ConfigurationManager.ConnectionStrings["AS400"]}")))
    {
        cmd.Connection.Open();
        using (var reader = cmd.ExecuteReader())
        {
            tableCurMd3LF.Load(reader);
            if (reader.NextResult())
            {
                tableCurMdlPF.Load(reader);
            }
        }
    }
    var result = new DataSet();
    result.Tables.Add(tableCurMd3LF);
    result.Tables.Add(tableCurMdlPF);
    return result;
}

I’m trying to execute a SQL statement like this:

SELECT CMDESC, CMSPRE, CMTOLM
FROM Test_Data.CURMD3LF
ORDER BY CMSEQ ASC
FOR READ ONLY WITH UR;

But the error I am getting is:

SQL0104: Token ; was not valid. Valid tokens: <END-OF-STATEMENT>.
Cause . . . . . : A syntax error was detected at token ;. Token ; is not a valid token. A partial list of valid tokens is . This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: — Verify the SQL statement in the area of the token ;. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. — If the error token is , correct the SQL statement because it does not end with a valid clause.

Obviously, it does not like the ; at the end of the SQL like T-SQL accepts.

Where it says, "Valid tokens: <END-OF-STATEMENT>", I tried using <END-OF-STATEMENT> in place of my semicolon, but that only created this exception:

SQL0104: Token < was not valid. Valid tokens: FOR USE SKIP WAIT WITH OPTIMIZE.
Cause . . . . . : A syntax error was detected at token <. Token < is not a valid token. A partial list of valid tokens is FOR USE SKIP WAIT WITH OPTIMIZE. This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: — Verify the SQL statement in the area of the token <. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. — If the error token is <END-OF-STATEMENT>, correct the SQL statement because it does not end with a valid clause.

Yes, I completely want to use Skip Wait. That’s what I’m trying to get at.

SELECT CMDESC, CMSPRE, CMTOLM
FROM Test_Data.CURMD3LF
ORDER BY CMSEQ ASC
FOR READ ONLY WITH UR
<END-OF-STATEMENT>
SELECT CMDESC, CMSPRE, CMTOLM
FROM Test_Data.CURMDLPF
ORDER BY CMSEQ ASC
FOR READ ONLY WITH UR

But that just creates the same error.

SQL0104: Token < was not valid. Valid tokens: FOR USE SKIP WAIT WITH OPTIMIZE.
Cause . . . . . : A syntax error was detected at token <. Token < is not a valid token. A partial list of valid tokens is FOR USE SKIP WAIT WITH OPTIMIZE. This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: — Verify the SQL statement in the area of the token <. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. — If the error token is <END-OF-STATEMENT>, correct the SQL statement because it does not end with a valid clause.

How do I send 2 or more SQL statements in to the AS400 from my C# code?

2

Answers


  1. I don’t think it is supported to send multiple SQL statements at once to IBM i. Try breaking it up into multiple exchanges.

    If you are creating procedures or routines using SQL, the ; character is indeed the appropriate end of statement character for the body statements of the routine, but everything needs to be wrapped inside a single create statement in that case, and the create statement must not end with a ; character.

    Login or Signup to reply.
  2. DB2 can only return a single object per request.

    Two or more Select statements without a JOIN is invalid. Separate the requests.


    Yes, I completely want to use Skip Wait. That’s what I’m trying to get at.

    This statement combined with the UR clause makes no sense.
    You want one of the following:

    ‘UR’ return uncommitted data. See Locking controls for DB2

    ‘SKIP LOCKED DATA` ignore uncommitted data.

    or ‘SELECT WAIT’ outstanding commits must occur first or timeout.

    In the IBM world, all contingencies are accounted for and any deviation is an error.

    How do I send 2 or more SQL statements in to the AS400 from my C# code?

    Assuming the single return object requirement is fulfilled, use a transaction:

    Use the BEGIN WORK; statement to start a transaction. Yes all or none.
    Then use COMMIT WORK; to commit the transaction.

    You may need to lock the databases involved to ensure success if updating:

    BEGIN WORK;
       LOCK TABLE stock;
       UPDATE stock SET unit_price = unit_price * 1.10
          WHERE manu_code = 'KAR';
       DELETE FROM stock WHERE description = 'baseball bat';
       INSERT INTO manufact (manu_code, manu_name, lead_time) 
          VALUES ('LYM', 'LYMAN', 14);
    COMMIT WORK;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search