能够在一列中更新新数据但不能将旧数据存储在另一列中

时间:2012-02-03 16:31:26

标签: sql-server-2008 asp-classic

我在4个月前在 Store old data in one column and update new data in another column within same table发布了一个问题。

现在,我正在重新编写代码,这是我的问题。单击网页上的“更新”按钮后,(上一个)LastName未存储在别名列中。实际上, LastName存储在 LastName (对于新LastName)和 Alias (据称对于之前的LastName)列中。我需要将 LastName保存在别名列中。请在下面查看,看看我做错了什么。提前谢谢!

存储过程

ALTER PROCEDURE [dbo].[KeepAliasName]
@PeopleID int,
@LastName varchar(25)

AS BEGIN

UPDATE People
SET Alias = LastName,
LastName = @LastName

WHERE CAST(PeopleID AS Varchar(25)) = @PeopleID

END

调用存储过程的代码

Dim oCmd1
Dim ln
Dim retCount

Set oCmd1 = Server.CreateObject("ADODB.Connection")
Set oCmd1 = Server.CreateObject("ADODB.Command")

''' EXECUTE THE COMMAND TO CREATE THE RECORDSET
With oCmd1
.ActiveConnection = Connect
.Commandtext = "KeepAliasName"
.CommandType = adoCmd1StoredProc
.Parameters.Append .CreateParameter("@PeopleID", adInteger, adParamInput, 25)
.Parameters("@PeopleID") = pID 'some Employee you get from your code
.Parameters.Append .CreateParameter("@LastName", adVarChar, adParamInput, 25)
.Parameters("@LastName") = lastnameVal 'some Employee's name you get from your code
.Execute ln, , adExecuteNoRecords
End With

Set oCmd1 = Nothing

1 个答案:

答案 0 :(得分:0)

除非有一个Doh时刻,例如将新姓氏设置为旧姓氏,y = th唯一的解释是dbms以相反的顺序执行这两个集合!

很容易找到。

ALTER PROCEDURE [dbo].[KeepAliasName]
@PeopleID int,
@LastName varchar(25)


AS BEGIN

UPDATE People
SET Alias = LastName

WHERE CAST(PeopleID AS Varchar(25)) = @PeopleID

UPDATE People
SET LastName = @LastName

WHERE CAST(PeopleID AS Varchar(25)) = @PeopleID

END