June 30, 2010 at 8:46 pm
Comments posted to this topic are about the item Disable All Triggers
June 30, 2010 at 9:13 pm
I did some online research to better understand how to use sp_MSforeachtable. I found this article to be very helpful:
http://www.databasejournal.com/features/mssql/article.php/3441031/SQL-Server-Undocumented-Stored-Procedures-spMSforeachtable-and-spMSforeachdb.htm
It contains this very simplistic example which clarified things for me:
use pubs
go
create table #rowcount (tablename varchar(128), rowcnt int)
exec sp_MSforeachtable
'insert into #rowcount select ''?'', count(*) from ?'
select top 5 * from #rowcount
order by tablename
drop table #rowcount
tablename rowcnt
----------------- -----------
[dbo].[authors] 23
[dbo].[discounts 3
[dbo].[employee] 43
[dbo].[jobs] 14
[dbo].[pub_info] 8
June 30, 2010 at 11:16 pm
thank you... 🙂
i was looking for such a stuff... this can be used for anything which needs to be applied on all the tables...
viz.
sp_msforeachtable "grant select on ? to user1"
June 30, 2010 at 11:18 pm
good
ziangij (6/30/2010)
thank you... 🙂i was looking for such a stuff... this can be used for anything which needs to be applied on all the tables...
viz.
sp_msforeachtable "grant select on ? to user1"
June 30, 2010 at 11:25 pm
Only one thing to note, this stored proc is undocumented which means it may disappear suddenly after a service pack or in new versions.
July 1, 2010 at 12:13 am
Nice question, thanks! (I had to think a little since I don't use TRIGGERs much.)
July 1, 2010 at 1:46 am
very helpful syntax.......
July 1, 2010 at 1:49 am
You'll find plenty of scripts out there using sp_MSforeachdb.
It's really useful...
July 1, 2010 at 3:24 am
Great question about an undocumented feature!
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
July 1, 2010 at 4:17 am
Given the answer options, I agree that the sp_foreachtable option was the only correct one.
But that does not make this the recommended method to disable all triggers in a database. As can be read in the documentation that is referenced in the explanation, a better way would be
DISABLE TRIGGER ALL ON DATABASE;
EDIT: The above is NOT TRUE!!! Hrovje alerted me to this mistake. This will not disable table- and view-scoped triggers, but only database-scoped DDL triggers. My apologies for the confusion.
July 1, 2010 at 4:33 am
I try not to use undocumented procedures in anything other than a "one time" thing. I got it right, but personally I wouldn't have done it like that 😉
July 1, 2010 at 5:14 am
I actually laughed a little when I saw the answers. I didn't know 'THE' was a valid t-sql command.
DROP THE TABLE <TableName>
CREATE THE TABLE <TableName>
DISABLE ALL TRIGGERS ON THE DATABASE
good question though, sp_msForEachTable is another tool in the arsenal.
thanks
July 1, 2010 at 5:16 am
Given the answer options, I agree that the sp_foreachtable option was the only correct one.
I agree with that completely, but it is not quite precise because triggers created on VIEW objects will remain enabled as the procedure loops only TABLE objects.
But that does not make this the recommended method to disable all triggers in a database. As can be read in the documentation that is referenced in the explanation, a better way would be
DISABLE TRIGGER ALL ON DATABASE;
Not true. The command will ONLY disable all triggers scoped to DATABASE while TABLE and VIEW triggers will remain enabled.
Best regards,
Hrvoje Piasevoli
Hrvoje Piasevoli
July 1, 2010 at 5:26 am
hrvoje.piasevoli (7/1/2010)
But that does not make this the recommended method to disable all triggers in a database. As can be read in the documentation that is referenced in the explanation, a better way would be
DISABLE TRIGGER ALL ON DATABASE;
Not true. The command will ONLY disable all triggers scoped to DATABASE while TABLE and VIEW triggers will remain enabled.
Oops! You are right!
Thanks for the correction! 🙂
July 1, 2010 at 7:39 am
Good question and use of the undocumented sp_MSforeachdb.
Now, I have a remark here, the question stated "...disable all triggers in a database".
As Hrvoje noted above, Database scoped triggers as well as triggers on views would not be disabled.
Viewing 15 posts - 1 through 15 (of 26 total)
You must be logged in to reply to this topic. Login to reply