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?
This question is related to
sql
tsql
stored-procedures
You could use getdate() in a default as this SO question's accepted answer shows. This way you don't provide the date, you just insert the rest and that date is the default value for the column.
You could also provide it in the values list of your insert and do it manually if you wish.
If you're looking to store the information in a table, you need to use an INSERT or an UPDATE statement. It sounds like you need an UPDATE statement:
UPDATE SomeTable
SET SomeDateField = GETDATE()
WHERE SomeID = @SomeID
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())
UPDATE Table
SET DateColumn=GETDATE()
WHERE UserID=@UserID
If you're inserting into a table, and will always need to put the current date, I would recommend setting GETDATE()
as the default value for that column, and don't allow NULLs
Source: Stackoverflow.com