I'm trying to insert a date when a user decides to deactivate or activate an UserID. I'm trying to use a SP to trigger that but apparantly this is harder than I thought it would be.
I can do a
SELECT GETDATE()
to get the date but is there a way to insert that information from GETDATE and put it into a column I want?
Couple of ways. Firstly, if you're adding a row each time a [de]activation occurs, you can set the column default to GETDATE() and not set the value in the insert. Otherwise,
UPDATE TableName SET [ColumnName] = GETDATE() WHERE UserId = @userId
To insert a new row into a given table (tblTable) :
INSERT INTO tblTable (DateColumn) VALUES (GETDATE())
To update an existing row :
UPDATE tblTable SET DateColumn = GETDATE()
WHERE ID = RequiredUpdateID
Note that when INSERT
ing a new row you will need to observe any constraints which are on the table - most likely the NOT NULL
constraint - so you may need to provide values for other columns eg...
INSERT INTO tblTable (Name, Type, DateColumn) VALUES ('John', 7, GETDATE())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With