Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Racho
Starting Member
13 Posts |
Posted - 2009-09-04 : 03:58:40
|
Posted this message a few days ago here but got no answer quote: New to SQL Server so would appreciate some help.I want a column to default to a unique integer UserIDI've looked at SUSER_NAME()which returns 'ServerName\LoginName'But apart from being more verbose has the disadvantage that (in our organisation we use a firstname+lastname logins, and therefore) when our females marry their logins often change.So I then looked at doing a lookup on the returned SUSER_NAME() value on our STAFF table and getting the integer UserId that way. This is fine for 1 or 100 or 1000 records, but I guess there is an overhead to doing this if the system is asked to write to a 'CreatedBy' or a 'ModifiedBy' (or both) column a few million times in a batch update or create process?So I then looked at SUSER_(ID)which does appear to be returning a unique integer, but it is not clear from the documentation and reference material I have wether in msSQL2008 this number remains unique against that particular login (and does a new number get created if on change of name at marriage the login changes)Can an SQL guru come to my aid on this and tell me if SUSER_ID() will do the trick or alternatively what is the best way to achieve my goal of using a unique numeric integer Id for each member of staff?ThanksSQL2008 and Visual Studio 2008 (VB Preferred)
Pity, but sometimes Forum's are great and sometimes not so here's an even simpler question... I have a 'CreatedDateTime' column which is defined as...[CDT] [datetime2](3) NOT NULL DEFAULT GetUTCDate(),amd a 'ModifiedDateTime' defined as...[MDT] [datetime2](3) NOT NULL DEFAULT GetUTCDate(),and all works just fine at record creationnow how do I get this 'modifiedDateTime' column to refresh with the current GetUTCDate at update time?Obviously I can write code to handle this post creation of the table, but isn't there a way to define this value to be calculated at the create table stage? I tried the obvious of:[MDT] [datetime2](3) AS (GetUTCDate()),But this errors on me Once again I ask for a 'best practice' solution, and as such a column must be one of the most common requirements in a production database I do hope that someone on this forum will come up with an answer for me. thanks.SQL2008 and Visual Studio 2008 (VB Preferred) |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-04 : 04:12:23
|
You should use a trigger to refresh the modified date on update if you don't like to code it in your application. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Racho
Starting Member
13 Posts |
Posted - 2009-09-04 : 08:12:58
|
Thanks Webfred for your quick reply I've now started writing the following trigger code for my 1st table called Staff where I have 2 colums:MDT ModifiedDateTime andMBI ModifiedById...ALTER TRIGGER [dbo].[trStaffModified] ON [dbo].[Staff] FOR UPDATEAS DECLARE @userId intSELECT @userId = id FROM Staff WHERE Staff.Login = SUSER_NAME()IF NOT @userId IS NULLBEGINUPDATE Staff SET Staff.MDT = GetUTCDate(), Staff.MBI = @userIdFROM Staff INNER JOIN Inserted ON Staff.Id = Inserted.IdEND and in testing this is now working fine.Probably have several refinements to make but in terms of best practice I wonder if I could have your comments on how to proceed.Each new table that I will create will have these same 2 fields for tracking/audit purposesDo I create a similiar trigger for each new table that I create?Or can I have a single trigger at the database level that takes the table name as a parameter?Cheers,SQL2008 and Visual Studio 2008 (VB Preferred) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-04 : 08:59:21
|
IMHO there is no chance to have a trigger at database level.On database level I only know DDL trigger and they cannot do this job. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Racho
Starting Member
13 Posts |
Posted - 2009-09-07 : 03:13:45
|
| Thanks WebfredYour answers have helped me to get started on the pathSQL2008 and Visual Studio 2008 (VB Preferred) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-09-07 : 03:27:34
|
welcome  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|