What is wrong with this query ? It is taking forever.....

  • USE GLPDEMO

    GO

    select t.name as TriggerName, ta.name as TableName, o.parent_obj

    into GLPDemo.dbo.Temp_TablesAndTriggers

    from sysobjects o inner join sys.triggers t

    on t.object_id = o.id inner join syscomments c

    on c.id = t.object_id inner join sys.tables ta

    on ta.object_id = o.parent_obj

    where xtype = 'tr' and c.text like '%Audit%'

    DECLARE @DBTrigger as varchar(100), @DBTable as varchar(100), @exestr as varchar(100)

    DECLARE TCursor CURSOR for

    SELECT TriggerName, TableName from Temp_TablesAndTriggers

    OPEN TCursor

    FETCH NEXT FROM TCursor

    INTO @DBTrigger, @DBTable

    WHILE @@FETCH_STATUS = 0

    select @exestr = ' DISABLE TRIGGER GLPDemo.dbo.' + @DBTrigger + ' ON GLPDemo.dbo.' + @DBTable

    EXECUTE sp_executesql @exestr;

    FETCH NEXT FROM TCursor

    INTO @DBTrigger, @DBTable

    CLOSE TCursor

    DEALLOCATE TCursor

    --DROP TABLE #Temp_TablesAndTriggers

  • There are some minor details that could be cleaned up, like declaring the cursor as "local fast_forward", which would speed it up a bit.

    The main thing I'd look at is how many triggers is it having to disable?

    Maybe replace the exec command with a print command, run the thing, and see what comes up?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • You need to add a begin and end to your while statement. This may or may not resolve the issue, but it should be addressed.

  • Hi,

    Yeah..Begin and end..Also, It didn't like database name with the disable trigger..I had 39 triggers to disable..

    Solved...

    Thanks Guys!!!

  • Yeah, without the BEGIN..END it's an infinite loop!

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply