Stored procedures 将存储过程更改为内联sql

Stored procedures 将存储过程更改为内联sql,stored-procedures,vbscript,asp-classic,Stored Procedures,Vbscript,Asp Classic,我有以下经典的asp工作,调用数据库中的存储过程。我希望将其转换为sql,而不是调用它在sql中传递的存储过程,添加PRAM,然后执行它 我尝试了各种各样的方法,但都没能奏效。 有人能转换这个例子吗 此外,在使用参数传递sql时,似乎很难获得合理的错误消息。是否有办法查看此呼叫的痕迹,以便我们了解问题的原因 Set cmd=server.createobjectADODB.Command cmd.ActiveConnection=theDatabase cmd.CommandType=adCmd

我有以下经典的asp工作,调用数据库中的存储过程。我希望将其转换为sql,而不是调用它在sql中传递的存储过程,添加PRAM,然后执行它

我尝试了各种各样的方法,但都没能奏效。 有人能转换这个例子吗

此外,在使用参数传递sql时,似乎很难获得合理的错误消息。是否有办法查看此呼叫的痕迹,以便我们了解问题的原因

Set cmd=server.createobjectADODB.Command cmd.ActiveConnection=theDatabase cmd.CommandType=adCmdStoredProc cmd.commandText=AddResponse cmd。Parameters@id=id cmd。Parameters@pc=个人计算机 cmd。Parameters@idDate=现在 cmd.Execute,adExecuteNoRecords set cmd=Nothing 存储过程定义

ALTER PROCEDURE[dbo].[AddResponse]@id NVARCHAR12、@pc NVARCHAR12、@idDate datetime 像 开始 不计数; 从EmailResponse中选择* 如果不存在,请从EmailResponse中选择id、projectCode,其中id=@id和projectCode=@pc 开始 在EmailResponse id中插入项目代码,输入日期VALUEs@id,@pc,@idDate 终止 终止 编辑: 以下是我对大家的回答。 哇,这太棒了,因为每个人都像你们一样 花一点时间帮助别人的人

选择*是一个错误

我必须维护和转换一些旧的asp代码,以使用存储过程。 在大多数情况下,存储过程是最常用的方法。 出于各种原因,有时最好在代码中包含sql。 快速测试和开发,无需访问数据库等。 所以我需要知道如何处理这两种情况

cmd.Parameters.Refresh 没有这个调用,我的代码可以正常工作。 真的有必要吗? 阅读它应该做什么对我为什么需要使用它没有多大帮助

理解类型对于所有类型的编程都至关重要。 这正是我想要的,还有更多。 Carl Prothman-数据类型映射 谢谢你

我还想知道如何设置记录集对象,尽管我忘了问。也谢谢你! set rs=server.createObject adodb.recordset rs=-cmd.Execute

我让三个都工作了。 对于这里感兴趣的任何人来说,都可以使用经过测试的代码来展示这三种方法

' Stored proc example
' ------------------------------------------
dim theDatabase, cmd, id, pc
theDatabase    = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"

id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))

if pc<>"" and id<>"" then
    Set cmd = server.createobject("ADODB.Command")

    cmd.ActiveConnection = theDatabase
    cmd.CommandType      = adCmdStoredProc
    cmd.commandText      = "AddResponse"

    cmd.Parameters("@id")     = id
    cmd.Parameters("@pc")     = pc
    cmd.Parameters("@idDate") = now

    cmd.Execute , , adExecuteNoRecords
    set cmd = Nothing
end if


' Inline SQl with ? example
' ------------------------------------------
dim theDatabase, cmd, id, pc
theDatabase    = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"

id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))

if pc<>"" and id<>"" then
    Set cmd = server.createobject("ADODB.Command")

    cmd.ActiveConnection = theDatabase
    cmd.CommandType      = adCmdText
    cmd.CommandText      = _
        "if not exists (SELECT id, projectCode FROM EmailResponse WHERE id = ? and projectCode = ?)" &_
        "begin INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(?, ?, ?) end "

    cmd.Parameters.Append cmd.CreateParameter("@id",     adInteger,     adParamInput,   ,  id)
    cmd.Parameters.Append cmd.CreateParameter("@pc",     adVarchar,     adParamInput, 12,  pc)
    cmd.Parameters.Append cmd.CreateParameter("@id2",    adInteger,     adParamInput,   ,  id)
    cmd.Parameters.Append cmd.CreateParameter("@pc2",    adVarchar,     adParamInput, 12,  pc)        
    cmd.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, -1,  now)

    cmd.Execute , , adExecuteNoRecords
    set cmd = Nothing
end if

' Inline SQl with @ example
' ------------------------------------------
dim theDatabase, cmd, sql, id, pc
theDatabase    = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"

id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))

if pc<>"" and id<>"" then
    Set cmd = server.createobject("ADODB.Command")

    sql = ""
    sql = sql & "SET NOCOUNT ON;" & vbCrLf
    sql = sql & "DECLARE @id NVARCHAR(12)" & vbCrLf
    sql = sql & "DECLARE @pc NVARCHAR(12)" & vbCrLf
    sql = sql & "DECLARE @idDate DATETIME" & vbCrLf
    sql = sql & "SELECT @id = ?, @pc = ?, @idDate = ?" & vbCrLf
    sql = sql & "IF NOT EXISTS (SELECT id, projectCode FROM EmailResponse WHERE id = @id and projectCode = @pc)" & vbCrLf
    sql = sql & "INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(@id, @pc, @idDate);"

    cmd.ActiveConnection = theDatabase
    cmd.CommandType      = adCmdText
    cmd.CommandText      = sql
    cmd.Prepared = true
    cmd.Parameters.Append cmd.CreateParameter("@id",     adInteger,     adParamInput,   ,  id)
    cmd.Parameters.Append cmd.CreateParameter("@pc",     adVarchar,     adParamInput, 12,  pc)
    cmd.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, -1,  now)

    cmd.Execute , , adExecuteNoRecords
    set cmd = Nothing
end if

谢谢大家。

要从存储过程切换到内联SQL,您需要更改

cmd.CommandType=adCmdStoredProc 到

cmd.CommandType=adCmdText 然后需要将查询添加到命令文本属性:

cmd.CommandText=从CustomerID=?
上面的行是从MSDN上的中派生的。

在尝试设置命名参数值之前,使用Parameters集合中的,上面的代码没有问题

Set cmd=server.createobjectADODB.Command 具有 .ActiveConnection=数据库 .CommandType=adCmdStoredProc .commandText=AddResponse '查询提供程序以获取参数详细信息 Call.Parameters.Refresh .Parameters@id=id .Parameters@pc=个人计算机 .Parameters@idDate=现在 调用。执行,AdExecutenRecords 以 set cmd=Nothing 如果不想使用此方法,参数定义必须来自某个地方,因此另一个选项是自己定义它们以反映存储过程的定义

Set cmd=server.createobjectADODB.Command 使用cmd .ActiveConnection=数据库 .CommandType=adCmdStoredProc .commandText=AddResponse '手动定义参数 Call.Parameters.Append。CreateParameter@id,adVarWChar,adParamInput,12 Call.Parameters.Append。CreateParameter@pc,adVarWChar,adParamInput,12 Call.Parameters.Append。CreateParameter@idDate,adDBTimeStamp,adParamInput,8 .Parameters@id=id .Parameters@pc=个人计算机 .Parameters@idDate=现在 调用。执行,AdExecutenRecords 以 set cmd=Nothing 如果你真的走手工路线,那么一个很好的资源就是识别要使用的常量

旁注:您的存储过程中有这一行

从EmailResponse中选择* 它希望返回一个结果集,但您在ADODB.Command Execute方法中指定了AdExecutenRecords,该方法会导致忽略该结果集,如果您确实希望返回该结果集,请将上述内容调整为:

暗淡的 ... 使用cmd ... 设置rs=.Execute 以 。。。用于显示省略代码的位置

需要指出的是,虽然已经删除,但不确定为什么。。。如果在不需要时添加两个额外的参数,那么它会使事情变得过于复杂,您可以在动态SQL中声明这些参数,并将它们分配给使用这些本地声明的变量来运行语句

dimsql sql= sql=sql&SET NOCOUNT ON;&换行字符 sql=sql&DECLARE@id NVARCHAR12&vbCrLf sql=sql&DECLARE@pc NVARCHAR12&vbCrLf sql=sql&DECLARE@idDate-DATETIME&vbCrLf sql=sql&选择@id=?、@pc=?、@idDate=?&换行字符 sql= sql&从EmailResponse中选择*换行字符 sql=sql&如果不存在,请从EmailResponse中选择id、projectCode,其中id=@id和projectCode=@pc&vbCrLf sql=sql&插入电子邮件响应id、项目代码、输入日期VALUEs@id,@pc,@idDate; Set cmd=server.createobjectADODB.Command 使用cmd .ActiveConnection=数据库 .CommandType=adCmdText .CommandText=sql .Prepared=true .Parameters.Append cmd。CreateParameter@id,adVarChar,adParamInput,12,id .Parameters.Append cmd。CreateParameter@pc,adVarChar,adParamInput,12,pc .Parameters.Append cmd。CreateParameter@idDate,adDBTimeStamp,adParamInput,8,现在 设置rsOut=.Execute 以 Set cmd=Nothing 有用的链接 -有关手动定义参数以避免错误的一些信息。
你为什么要切换到动态SQL而不是存储过程?你的代码很好,你只是;没有理由改变。@Austin你进展如何?这些建议对我有帮助吗?在大家的帮助下,我让这三种方法都发挥了作用,并创建了有效的示例。请参阅对我的原始帖子的编辑。tx Everyon这也可以,但是为什么在存储过程工作正常时切换到动态SQL呢?