参数化的Insert语句,事务抛出错误

时间:2008-12-04 19:31:11

标签: sql asp-classic

我有一个运行良好的参数化插入语句,我需要添加一个select语句并将其全部包装在一个事务中,以获取插入字段之一的数据。我不确定我是否已经达到ASP / ADO的限制,或者我是否只是语法错误。这是我的代码:

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

当我运行该代码时,我收到此错误消息,当它尝试设置Parameter(0)的值时触发

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

知道什么是错的吗?

我有很多ASP和SQL的经验,但没有存储过程的经验。这段代码是如此痛苦地接近存储过程我应该将其移动并称之为好吗?

感谢。

1 个答案:

答案 0 :(得分:2)

尝试放;在每个单独的声明后。 例如

 .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;"