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 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2005-07-13 : 19:46:10
|
| Guys, I have the follwing trigger which works fine if I do not include the following clause "SELECT @ROWCOUNT=COUNT(*) FROM USERSIF (@ROWCOUNT <> 0)"What I try to do with this clause is to make sure that trigger inserts in userhist table if only the users table is not empty hence I try to check the rowcount in the if clause.But it seems to error out. Is there any better way doing this check.CREATE TRIGGER [USER_BD] ON [dbo].[USERs] FOR DELETE ASDECLARE @USERID varchar (50), @APP varchar (50) , @LOGON datetime, @ROWCOUNT INTSELECT @ROWCOUNT=COUNT(*) FROM USERSIF (@ROWCOUNT <> 0)BEGINSELECT @USERID = USERID, @APP = APP, @LOGON = LOGONFROM deleted insert into USERHIST ( USERID, APP, LOGON)VALUES( @USERID, @APP, @LOGON)endAny suggestions/input is appreciatedThanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-07-13 : 19:51:24
|
| You have a fundamental problem with your trigger. Please see this for more information:http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspxAll you need is this:IF EXISTS(SELECT * FROM USERS) INSERT INTO USERHIST (USERID,APP,LOGON) SELECT USERID,APP,LOGON FROM deletedAnd yes SELECT * should be used for EXISTS and NOT EXISTS as it's fastest in this case only. Tara |
 |
|
|
|
|
|
|
|