Parameterized insert statement with error transaction

I had a parameterized insert statement that worked well and I needed to add a select statement and wrap it all in a transaction to get the data for one of the fields to insert. I'm not sure if I hit the constraint using ASP / ADO, or if I have something syntactically wrong. Here's my code:

set oSQLCommand = Server.CreateObject("ADODB.Command")
with oSQLCommand
   .ActiveConnection = conn
   .CommandType = 1
   .CommandText = "set nocount on " &_
               "begin transaction " &_
                  "declare @docid integer " &_
                  "begin " &_
                     "set @docid = (SELECT MAX(id+1) AS docid FROM draft_main) " &_
                     "INSERT INTO draft_details (id, main_id, blah) " &_
                     "VALUES ( ?, @docid, ?)" &_
                  "end " &_
               "commit"
   .Parameters(0).value = c_id
   .Parameters(1).value = "blah blah"
   .execute
end with
set oSQLCommand = nothing

      

When I run this code, I get this error message that is thrown when I try to set the parameter value (0)

    Microsoft OLE DB Provider for SQL Server (0x80004005)
    Syntax error or access violation

Any idea what's wrong?

I have a lot of experience with ASP and SQL, but none of them contain stored procedures. Is this code so painfully close to a stored procedure, should I just move it around and call it good?

Thank.

+1


source to share


1 answer


Try to put; after each individual statement. eg



 .CommandText = "set nocount on; " &_
               "begin transaction; " &_
                  "declare @docid integer; " &_
                  "begin " &_
                     "set @docid = (SELECT MAX(id+1) AS docid FROM draft_main); " &_
                     "INSERT INTO draft_details (id, main_id, blah) " &_
                     "VALUES ( ?, @docid, ?);" &_
                  "end ;" &_
               "commit;"

      

+2


source







All Articles