Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2010
    Posts
    72

    Unanswered: 'Execute immediate' context

    Hi,

    When I run my script I am getting the below error msg. Please help me to fix this error. Thanks in advance.

    Msg 11736, Level 15, State 0:
    line 1: You cannot specify this command in an 'execute immediate' context.


    Here is my script:

    declare tbl_loop cursor for
    select distinct name from sysobjects where type='U'
    go

    declare @sql varchar(250)
    declare @tbl_name char(25)
    open tbl_loop
    fetch tbl_loop into @tbl_name
    while (@@sqlstatus = 0 )
    begin
    select @sql = ' dbcc checktable('+@tbl_name+')'
    print @sql
    execute (@sql)
    fetch tbl_loop into @tbl_name
    end
    close tbl_loop
    deallocate cursor tbl_loop
    go
    END

  2. #2
    Join Date
    May 2009
    Posts
    43
    It's not possible to run the dbcc commands like
    exec dbcc checktable(*****)

  3. #3
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    Code:
    declare tbl_loop cursor for
    select name from sysobjects where type='U'
    go
    declare @tbl_name char(25)
    open tbl_loop
    fetch tbl_loop into @tbl_name
    while (@@sqlstatus = 0 )
    begin
      dbcc checktable(@tbl_name)
      fetch tbl_loop into @tbl_name
    end
    close tbl_loop
    deallocate cursor tbl_loop

Posting Permissions

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