Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Location
    Bangalore, India
    Posts
    250

    Unanswered: Stored proc with dynSQL issue

    Hello All:

    I am facing a prob in one of my stored procedures. The proc executes some dynamic sql and it never completes.

    Initially i thought it was a performance issue with the "INSERT INTO ..SELECT..." query which cause this delay, but the query completes in 2-3 seconds if executed from Sql Management studio.

    I ran SQL profiler trace for this stored proc and it is shows "Stmt starting EXEC (@sql)" statement and nothing beyond that. At this time, i noticed that no other SPIDs are blocking this proc and the server CPU/memory usage does not show any significant variation..

    The pseudo-code is given below.

    Any thoughts on why this is happening?

    stored proc
    ...
    Declare @sql NVARCHAR(MAX) =''
    @id int
    ....
    Loop1
    ....
    Loop2
    ....
    Loop3
    .....
    SET @sql = 'insert into #results select distinct x,y,z from view v join tablename t on t.x=v.x where t.id= '+ convert(nvarchar(10),@id)
    print @sql
    EXEC (@sql)
    End Loop3
    .....
    End Loop2
    ....
    End Loop1

    Thanks,
    Baburaj
    Cheers....

    baburajv

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Loop1
    ....
    Loop2
    ....
    Loop3
    First SQL is not a procedural language. It is set based, and if you are looping and not doing set based batches you are crippling your beautiful RDBMS and making it into a one of the old magnetic tape machines.

    Barring that, have you looked at the execution plan and the table level I/O?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •