sql job fails --- need help

  • Hello

    I have one up and running job since few years but today i am facing one strange error.

    here is error log

    Executed as user: ABC\XYZ. Incorrect syntax near '*'. [SQLSTATE 42000] (Error 102). The step failed.

    In that step there is no where '*'

    here is command in the step

    declare @spid varchar(2)

    declare @execstat nvarchar(10)

    declare kill_cursor cursor

    for

    select spid

    from sysprocesses sp, sysdatabases sdb

    where sp.dbid=sdb.dbid

    and name='database_name'

    open kill_cursor

    Fetch next from kill_cursor into @spid

    while (@@fetch_status =0)

    Begin

    set @execstat ='kill'+' '+@spid

    exec sp_executesql @execstat

    fetch next from kill_cursor into @spid

    End

    Close kill_cursor

    deallocate kill_cursor

    Please help me how to resolve.

    Thanks

  • sp_dboption is a depreciated feature, user alter database instead

    ALTER DATABASE myDB SET SINGLE_USER

  • anthony.green (1/9/2013)


    sp_dboption is a depreciated feature, user alter database instead

    ALTER DATABASE myDB SET SINGLE_USER

    Thanks for reply

    It was my bad

    actual script is as below

    declare @spid varchar(2)

    declare @execstat nvarchar(10)

    declare kill_cursor cursor

    for

    select spid

    from sysprocesses sp, sysdatabases sdb

    where sp.dbid=sdb.dbid

    and name='database_name'

    open kill_cursor

    Fetch next from kill_cursor into @spid

    while (@@fetch_status =0)

    Begin

    set @execstat ='kill'+' '+@spid

    exec sp_executesql @execstat

    fetch next from kill_cursor into @spid

    End

    Close kill_cursor

    deallocate kill_cursor

    Please help me into this

  • What version of SQL are you running?

    What is the purpose of the task at hand, do you just want to boot people out or are you setting the DB into single user mode?

    sysprocesses and sysdatabases are also depreciated

  • anthony.green (1/9/2013)


    What version of SQL are you running?

    What is the purpose of the task at hand, do you just want to boot people out or are you setting the DB into single user mode?

    sysprocesses and sysdatabases are also depreciated

    The purpose of task is , restore database

    so first need to kill all spids for that database and then

    change database into single user mode

    so right now, job is failed to step-1(kill all spids)

  • ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

  • anthony.green (1/9/2013)


    ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

    Thank for Your reply

    but job is failed to step-1 as killed spids

    I need to resolve that.

  • The above will do exactly the same as what you are trying to do, just it is the new way of doing it.

  • anthony.green (1/9/2013)


    The above will do exactly the same as what you are trying to do, just it is the new way of doing it.

    so it means

    ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

    command will kill all spids as well as set database into SINGLE_USER ?

  • Yep that is correct.

    ROLLBACK IMMEDIATE, will force any connections in that databases to stop executing and rollback what they where doing if they where in the middle of a transaction

    SINGLE_USER does just what it says, after rollback, change DB to single user so that only 1 SPID can connect to it

  • anthony.green (1/9/2013)


    Yep that is correct.

    ROLLBACK IMMEDIATE, will force any connections in that databases to stop executing and rollback what they where doing if they where in the middle of a transaction

    SINGLE_USER does just what it says, after rollback, change DB to single user so that only 1 SPID can connect to it

    That's Awesome

    Thanks

    so Now the process will finish in only one step, I don't need to do in step-1 and step-2 for kill all connections and set database to single user mode.

  • Ensure you do what you do in the same job step, no telling what may happen if it moves step and gets a different spid while its in single user mode not allowing you to access the DB as someone else beat you to it.

    So

    ALTER DATABASE myDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

    GO

    RESTORE DATABASE myDB FROM DISK = 'xxxxxxxxxxxxxxxxxxxxxx'

    GO

    In the same job step.

Viewing 12 posts - 1 through 11 (of 11 total)

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