Store old data in one column and update new data i

2019-08-23 12:43发布

问题:

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.

回答1:

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.



回答2:

Try this update statement:

UPDATE Employee
Set LastName = @NewLastName,
    Alias = LastName
WHERE PeopleId = @Whatever


回答3:

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