经典 ASP - 无法从 SQL 服务器获得预期的响应 INSERT INTO 与 OUTPUT INTO 一起工作
Classic ASP - Can't get expected response from SQL Server INSERT INTO work with OUTPUT INTO
我正在尝试在 SQL 服务器数据库中注册一个新用户,来自 Classic ASP
我很欣赏 Classic ASP 是过时的,但我熟悉它并且它可以完成我需要的工作(通常)。
这是代码...
<%
Set rs = CreateObject("ADODB.Recordset")
Set con = Server.CreateObject("ADODB.Connection")
con.ConnectionString="Provider=SQLOLEDB;" & _
"Server=Server\SQLEXPRESS;" & _
"Uid=IUSR;" & _
"Pwd=Pwd;" & _
"Database=DB"
con.Open
username="username"
password="password"
tsql="CREATE TABLE #newUserTable(UserID int); " & vbCrLf & _
"DECLARE @responseMessage NVARCHAR(50); " & vbCrLf & _
"DECLARE @userID INT; " & vbCrLf & _
"IF EXISTS (SELECT TOP 1 UserID FROM [dbo].[UserLogins] WHERE LoginName='" & username & "') " & vbCrLf & _
"BEGIN " & vbCrLf & _
"SET @responseMessage='User already exists' " & vbCrLf & _
"END " & vbCrLf & _
"ELSE " & vbCrLf & _
"BEGIN " & vbCrLf & _
"DECLARE @salt UNIQUEIDENTIFIER=NEWID(); " & vbCrLf & _
"BEGIN TRY " & vbCrLf & _
"INSERT INTO dbo.UserLogins (LoginName,PasswordHash,Salt) " & vbCrLf & _
"OUTPUT INSERTED.UserID INTO #newUserTable " & vbCrLf & _
"VALUES ('" & username & "',HASHBYTES('SHA2_512', '" & password & "'+CAST(@salt AS NVARCHAR(36))),@salt); " & vbCrLf & _
"SET @responseMessage=" & vbCrLf & _
"CAST((SELECT userID FROM #newUserTable) AS NVARCHAR(50)); " & vbCrLf & _
"END TRY " & vbCrLf & _
"BEGIN CATCH " & vbCrLf & _
"SET @responseMessage=ERROR_MESSAGE() " & vbCrLf & _
"END CATCH " & vbCrLf & _
"END " & vbCrLf & _
"SELECT @responseMessage AS N'responseMessage'; " & vbCrLf & _
"DROP TABLE #newUserTable"
'response.write tsql & "<hr />"
rs.open tsql, con
response.write rs("responseMessage")
rs.close
%>
当我尝试添加现有用户时,我得到了预期的 User already exists
响应,但是当我尝试添加新用户时,我得到 Item cannot be found in the collection corresponding to the requested name or ordinal
这表明 rresponseMessage
不存在。如果我将 tsql 输出到浏览器(将 vbCrLf 替换为“
”)并在 SQL 服务器中将其 运行 输出,我将 userID
返回为 responseMessage
正如我所料。
谁能告诉我为什么会这样,我该如何纠正它?
将您的最后一个命令更改为 SELECT @responseMessage AS N'responseMessage'
(而不是 Drop)。
您也可以尝试使用“SET NOCOUNT ON
”来避免计数消息扰乱您的结果。
此外,您不需要将 vbCrLf
添加到 TSQL
您的代码应如下所示:
tsql="SET NOCOUNT ON; " & _
"CREATE TABLE #newUserTable(UserID int); " & _
"DECLARE @responseMessage NVARCHAR(50); " & _
"DECLARE @userID INT; " & _
"IF EXISTS (SELECT TOP 1 UserID FROM [dbo].[UserLogins] WHERE LoginName='" & username & "') " & _
"BEGIN " & _
"SET @responseMessage='User already exists' " & _
"END " & _
"ELSE " & _
"BEGIN " & _
"DECLARE @salt UNIQUEIDENTIFIER=NEWID(); " & _
"BEGIN TRY " & _
"INSERT INTO dbo.UserLogins (LoginName,PasswordHash,Salt) " & _
"OUTPUT INSERTED.UserID INTO #newUserTable " & _
"VALUES ('" & username & "',HASHBYTES('SHA2_512', '" & password & "'+CAST(@salt AS NVARCHAR(36))),@salt); " & _
"SET @responseMessage=" & _
"CAST((SELECT userID FROM #newUserTable) AS NVARCHAR(50)); " & _
"END TRY " & _
"BEGIN CATCH " & _
"SET @responseMessage=ERROR_MESSAGE() " & _
"END CATCH " & _
"END " & _
"DROP TABLE #newUserTable" & _
"SELECT @responseMessage AS N'responseMessage'; "
我正在尝试在 SQL 服务器数据库中注册一个新用户,来自 Classic ASP
我很欣赏 Classic ASP 是过时的,但我熟悉它并且它可以完成我需要的工作(通常)。
这是代码...
<%
Set rs = CreateObject("ADODB.Recordset")
Set con = Server.CreateObject("ADODB.Connection")
con.ConnectionString="Provider=SQLOLEDB;" & _
"Server=Server\SQLEXPRESS;" & _
"Uid=IUSR;" & _
"Pwd=Pwd;" & _
"Database=DB"
con.Open
username="username"
password="password"
tsql="CREATE TABLE #newUserTable(UserID int); " & vbCrLf & _
"DECLARE @responseMessage NVARCHAR(50); " & vbCrLf & _
"DECLARE @userID INT; " & vbCrLf & _
"IF EXISTS (SELECT TOP 1 UserID FROM [dbo].[UserLogins] WHERE LoginName='" & username & "') " & vbCrLf & _
"BEGIN " & vbCrLf & _
"SET @responseMessage='User already exists' " & vbCrLf & _
"END " & vbCrLf & _
"ELSE " & vbCrLf & _
"BEGIN " & vbCrLf & _
"DECLARE @salt UNIQUEIDENTIFIER=NEWID(); " & vbCrLf & _
"BEGIN TRY " & vbCrLf & _
"INSERT INTO dbo.UserLogins (LoginName,PasswordHash,Salt) " & vbCrLf & _
"OUTPUT INSERTED.UserID INTO #newUserTable " & vbCrLf & _
"VALUES ('" & username & "',HASHBYTES('SHA2_512', '" & password & "'+CAST(@salt AS NVARCHAR(36))),@salt); " & vbCrLf & _
"SET @responseMessage=" & vbCrLf & _
"CAST((SELECT userID FROM #newUserTable) AS NVARCHAR(50)); " & vbCrLf & _
"END TRY " & vbCrLf & _
"BEGIN CATCH " & vbCrLf & _
"SET @responseMessage=ERROR_MESSAGE() " & vbCrLf & _
"END CATCH " & vbCrLf & _
"END " & vbCrLf & _
"SELECT @responseMessage AS N'responseMessage'; " & vbCrLf & _
"DROP TABLE #newUserTable"
'response.write tsql & "<hr />"
rs.open tsql, con
response.write rs("responseMessage")
rs.close
%>
当我尝试添加现有用户时,我得到了预期的 User already exists
响应,但是当我尝试添加新用户时,我得到 Item cannot be found in the collection corresponding to the requested name or ordinal
这表明 rresponseMessage
不存在。如果我将 tsql 输出到浏览器(将 vbCrLf 替换为“
”)并在 SQL 服务器中将其 运行 输出,我将 userID
返回为 responseMessage
正如我所料。
谁能告诉我为什么会这样,我该如何纠正它?
将您的最后一个命令更改为 SELECT @responseMessage AS N'responseMessage'
(而不是 Drop)。
您也可以尝试使用“SET NOCOUNT ON
”来避免计数消息扰乱您的结果。
此外,您不需要将 vbCrLf
添加到 TSQL
您的代码应如下所示:
tsql="SET NOCOUNT ON; " & _
"CREATE TABLE #newUserTable(UserID int); " & _
"DECLARE @responseMessage NVARCHAR(50); " & _
"DECLARE @userID INT; " & _
"IF EXISTS (SELECT TOP 1 UserID FROM [dbo].[UserLogins] WHERE LoginName='" & username & "') " & _
"BEGIN " & _
"SET @responseMessage='User already exists' " & _
"END " & _
"ELSE " & _
"BEGIN " & _
"DECLARE @salt UNIQUEIDENTIFIER=NEWID(); " & _
"BEGIN TRY " & _
"INSERT INTO dbo.UserLogins (LoginName,PasswordHash,Salt) " & _
"OUTPUT INSERTED.UserID INTO #newUserTable " & _
"VALUES ('" & username & "',HASHBYTES('SHA2_512', '" & password & "'+CAST(@salt AS NVARCHAR(36))),@salt); " & _
"SET @responseMessage=" & _
"CAST((SELECT userID FROM #newUserTable) AS NVARCHAR(50)); " & _
"END TRY " & _
"BEGIN CATCH " & _
"SET @responseMessage=ERROR_MESSAGE() " & _
"END CATCH " & _
"END " & _
"DROP TABLE #newUserTable" & _
"SELECT @responseMessage AS N'responseMessage'; "