Store old data in one column and update new data in another column within same table

白昼怎懂夜的黑 提交于 2020-01-07 02:51:06

问题


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


来源:https://stackoverflow.com/questions/7336786/store-old-data-in-one-column-and-update-new-data-in-another-column-within-same-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!