Store old data in one column and update new data i

2019-08-23 12:17发布

I'm not a professional programmer but an enthusiast who needs help. Here is my challenge:

I have a table full of data for our employees.

Currently, any of our employees can go to the "Update" web page on our web site and type in his new last name over the previous one that is displayed in a textbox and click submit, his last name will be updated in the database. However, my supervisor wants to keep the employees' previous last name in the same table as the new last name. Her idea is when an employee types in his new last name in the textbox, it will trigger the database to store his previous last name in a column called "Alias" and then update his new last name in the "LastName" column. How do I proceed with that?

Here is the structure of the table:

PeopleID (int)  
JobIDNum (int)  
EmployeeIDNum (varchar(25))  
Email (varchar(100))  
Password (varchar(50))  
LastName (varchar(50))  
FirstName (varchar(25))  
Deleted (char(1))  
Alias (varchar(50))  

I appreciate any assistance and/or advice I can get.

3条回答
干净又极端
2楼-- · 2019-08-23 12:51

Try this update statement:

UPDATE Employee
Set LastName = @NewLastName,
    Alias = LastName
WHERE PeopleId = @Whatever
查看更多
做自己的国王
3楼-- · 2019-08-23 12:57
UPDATE TABLENAME
  SET Alias = LastName, LastName = @LastName 
WHERE PeopleID = @PeopleID

Where the @LastName is a parameter with new value, @PeopleID is a id of corresponding people.

查看更多
劳资没心,怎么记你
4楼-- · 2019-08-23 12:58

Suggest a stored procedure that you can call from ASP.

 CREATE PROC UpdateEmp
    @EmpID  int,
    @LastName varchar(50),
    --all the others properties you need to update this person
 AS
 BEGIN

     UPDATE Employee
     SET Alias = LastName,
         LastName = @LastName,
         --all the other properties you want updated.
     WHERE PeopleID = @EmpID;

 END

Call this stored proc in your ASP code like this:

Dim cmd
Dim ln
Dim retCount    
Dim conn
Set conn= Server.CreateObject("ADODB.Connection")       
Set cmd = Server.CreateObject("ADODB.Command")      
conn.Open "some connection string"
With cmd
    .ActiveConnection = conn
    .Commandtext = "UpdateEmp"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter("@EmpID", adInteger, adParamInput, 10)
    .Parameters("@EmpID") = 22 'some Employee you get from your code
    .Parameters.Append .CreateParameter("@LastName", adVarChar, adParamInput, 50)
    .Parameters("@LastName") = "MyLastName" 'some Employee you get from your code
    .Execute ln, , adExecuteNoRecords
End With        
Set cmd = Nothing
查看更多
登录 后发表回答