显示标签为“scheduled”的博文。显示所有博文
显示标签为“scheduled”的博文。显示所有博文

2012年3月19日星期一

Disconnect/Clear connections

I have a scheduled job where I'm attempting to restore a full backup of our
Production database, over our Test/Dev database.
However, it keeps failing.
When I try to run the Restore Database command manually, I get an error that
states "Exclusive access could not be obtained".
Right now, the only way I know how to clear the connections is to go into
the Detach Database task, and select 'Clear all connections'.
I tried using the 'Exec SQL Disconnect ALL' command, but that statement
won't parse.
Is there another way to clear all database connections? Preferebly in
Trasact-SQL language, that I could schedule to run immediately prior to the
Restore Database command?
Please advise.
Thanks!
YsandreYsandre wrote:
> I have a scheduled job where I'm attempting to restore a full backup of our
> Production database, over our Test/Dev database.
> However, it keeps failing.
> When I try to run the Restore Database command manually, I get an error that
> states "Exclusive access could not be obtained".
> Right now, the only way I know how to clear the connections is to go into
> the Detach Database task, and select 'Clear all connections'.
> I tried using the 'Exec SQL Disconnect ALL' command, but that statement
> won't parse.
> Is there another way to clear all database connections? Preferebly in
> Trasact-SQL language, that I could schedule to run immediately prior to the
> Restore Database command?
> Please advise.
> Thanks!
> Ysandre
Read up on the ALTER DATABASE command
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||I did, but I don't see anything in there regarding clearing connections to
the database.
Is there somewhere else I should look?
All I'm looking for is something along the lines of:
EXEC SQL DISCONNECT ALL
Or why the above doesn't parse.
Thanks!
Ysandre
"Tracy McKibben" wrote:
> Ysandre wrote:
> > I have a scheduled job where I'm attempting to restore a full backup of our
> > Production database, over our Test/Dev database.
> > However, it keeps failing.
> > When I try to run the Restore Database command manually, I get an error that
> > states "Exclusive access could not be obtained".
> > Right now, the only way I know how to clear the connections is to go into
> > the Detach Database task, and select 'Clear all connections'.
> > I tried using the 'Exec SQL Disconnect ALL' command, but that statement
> > won't parse.
> > Is there another way to clear all database connections? Preferebly in
> > Trasact-SQL language, that I could schedule to run immediately prior to the
> > Restore Database command?
> > Please advise.
> > Thanks!
> > Ysandre
> Read up on the ALTER DATABASE command
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>|||In short: When you go to Detach/Clear in the EMC to disconnect all the
users it is going to do a couple things.
First - It searches through the sysprocesses for the DB you want to
clear and finds the spid's
Then - set noexec off set parseonly off
Lastly - kill spid#
Hope this helps.
Cheers,
Ysandre wrote:
> I did, but I don't see anything in there regarding clearing connections to
> the database.
> Is there somewhere else I should look?
> All I'm looking for is something along the lines of:
> EXEC SQL DISCONNECT ALL
> Or why the above doesn't parse.
> Thanks!
> Ysandre
> "Tracy McKibben" wrote:
> > Ysandre wrote:
> > > I have a scheduled job where I'm attempting to restore a full backup of our
> > > Production database, over our Test/Dev database.
> > > However, it keeps failing.
> > > When I try to run the Restore Database command manually, I get an error that
> > > states "Exclusive access could not be obtained".
> > > Right now, the only way I know how to clear the connections is to go into
> > > the Detach Database task, and select 'Clear all connections'.
> > > I tried using the 'Exec SQL Disconnect ALL' command, but that statement
> > > won't parse.
> > > Is there another way to clear all database connections? Preferebly in
> > > Trasact-SQL language, that I could schedule to run immediately prior to the
> > > Restore Database command?
> > > Please advise.
> > > Thanks!
> > > Ysandre
> >
> > Read up on the ALTER DATABASE command
> >
> >
> > --
> > Tracy McKibben
> > MCDBA
> > http://www.realsqlguy.com
> >|||You could try something like this. Replace the DBID with the database
you are backing up:
DECLARE SpidsToKill CURSOR FOR
SELECT spid
FROM master..sysprocesses
WHERE dbid = 9
DECLARE @.Spid int
DECLARE @.SQL varchar(255)
OPEN SpidsToKill
FETCH NEXT FROM SpidsToKill
INTO @.Spid
WHILE ( @.@.FETCH_STATUS = 0 )
BEGIN
SELECT @.SQL = 'KILL ' + CONVERT( varchar(10), @.Spid )
EXEC ( @.SQL )
FETCH NEXT FROM SpidsToKill
INTO @.Spid
END
CLOSE SpidsToKill
DEALLOCATE SpidsToKill|||Ysandre wrote:
> I did, but I don't see anything in there regarding clearing connections to
> the database.
You want to use ALTER DATABASE to put the database in SINGLE_USER mode,
WITH immediate ROLLBACK of transactions...
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||That worked.
THANK YOU!!!
"10001110101" wrote:
> You could try something like this. Replace the DBID with the database
> you are backing up:
> DECLARE SpidsToKill CURSOR FOR
> SELECT spid
> FROM master..sysprocesses
> WHERE dbid = 9
> DECLARE @.Spid int
> DECLARE @.SQL varchar(255)
> OPEN SpidsToKill
> FETCH NEXT FROM SpidsToKill
> INTO @.Spid
> WHILE ( @.@.FETCH_STATUS = 0 )
> BEGIN
> SELECT @.SQL = 'KILL ' + CONVERT( varchar(10), @.Spid )
> EXEC ( @.SQL )
> FETCH NEXT FROM SpidsToKill
> INTO @.Spid
> END
> CLOSE SpidsToKill
> DEALLOCATE SpidsToKill
>|||Ysandre wrote:
> That worked.
> THANK YOU!!!
>
Ugh, it's too painful to watch... Drop that cursor nonsense, you can
accomplish this with ONE STATEMENT. Since you can't read documentation
and piece together the command yourself, here it is:
ALTER DATABASE DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
That will put the database in single-user mode, allowing one connection
only, and will gracefully rollback any outstanding transactions.
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||This is a multi-part message in MIME format.
--=_NextPart_000_141C_01C6AF0A.34C3D1A0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: quoted-printable
The problem with the script above to 'kill' sessions, is that a session =may immediately reconnect.
The BEST solution, is as Tracy advised. Use
ALTER DATABASE MyDatabase
SET SINGLE_USER WTIH ROLLBACK IMMEDIATE
-- Arnie Rowland
Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous
"Tracy McKibben" <tracy@.realsqlguy.com> wrote in message =news:%23EmybC0rGHA.1592@.TK2MSFTNGP02.phx.gbl...
> Ysandre wrote:
>> I did, but I don't see anything in there regarding clearing =connections to >> the database.
> > You want to use ALTER DATABASE to put the database in SINGLE_USER =mode, > WITH immediate ROLLBACK of transactions...
> > > -- > Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
--=_NextPart_000_141C_01C6AF0A.34C3D1A0
Content-Type: text/html;
charset="Utf-8"
Content-Transfer-Encoding: quoted-printable
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

The problem with the script above to ='kill' sessions, is that a session may immediately reconnect.
The BEST solution, is as Tracy advised. =Use
ALTER DATABASE =MyDatabase
SET SINGLE_USER =WTIH ROLLBACK IMMEDIATE
-- Arnie RowlandMost good =judgment comes from experience. Most experience comes from bad judgment. =- Anonymous
"Tracy McKibben" =wrote in message news:%23EmybC0rGHA.1592@.TK2MSFTNGP02.phx.gbl...> =Ysandre wrote:> I did, but I don't see anything in there regarding =clearing connections to > the database.> > You want to =use ALTER DATABASE to put the database in SINGLE_USER mode, > WITH =immediate ROLLBACK of transactions...> > > -- > Tracy McKibben> MCDBA>">http://www.realsqlguy.com</FONT></A></BODY></HTML>
--=_NextPart_000_141C_01C6AF0A.34C3D1A0--

Disconnect/Clear connections

I have a scheduled job where I'm attempting to restore a full backup of our
Production database, over our Test/Dev database.
However, it keeps failing.
When I try to run the Restore Database command manually, I get an error that
states "Exclusive access could not be obtained".
Right now, the only way I know how to clear the connections is to go into
the Detach Database task, and select 'Clear all connections'.
I tried using the 'Exec SQL Disconnect ALL' command, but that statement
won't parse.
Is there another way to clear all database connections? Preferebly in
Trasact-SQL language, that I could schedule to run immediately prior to the
Restore Database command?
Please advise.
Thanks!
YsandreYsandre wrote:
> I have a scheduled job where I'm attempting to restore a full backup of ou
r
> Production database, over our Test/Dev database.
> However, it keeps failing.
> When I try to run the Restore Database command manually, I get an error th
at
> states "Exclusive access could not be obtained".
> Right now, the only way I know how to clear the connections is to go into
> the Detach Database task, and select 'Clear all connections'.
> I tried using the 'Exec SQL Disconnect ALL' command, but that statement
> won't parse.
> Is there another way to clear all database connections? Preferebly in
> Trasact-SQL language, that I could schedule to run immediately prior to th
e
> Restore Database command?
> Please advise.
> Thanks!
> Ysandre
Read up on the ALTER DATABASE command
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||I did, but I don't see anything in there regarding clearing connections to
the database.
Is there somewhere else I should look?
All I'm looking for is something along the lines of:
EXEC SQL DISCONNECT ALL
Or why the above doesn't parse.
Thanks!
Ysandre
"Tracy McKibben" wrote:

> Ysandre wrote:
> Read up on the ALTER DATABASE command
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>|||In short: When you go to Detach/Clear in the EMC to disconnect all the
users it is going to do a couple things.
First - It searches through the sysprocesses for the DB you want to
clear and finds the spid's
Then - set noexec off set parseonly off
Lastly - kill spid#
Hope this helps.
Cheers,
Ysandre wrote:[vbcol=seagreen]
> I did, but I don't see anything in there regarding clearing connections to
> the database.
> Is there somewhere else I should look?
> All I'm looking for is something along the lines of:
> EXEC SQL DISCONNECT ALL
> Or why the above doesn't parse.
> Thanks!
> Ysandre
> "Tracy McKibben" wrote:
>|||You could try something like this. Replace the DBID with the database
you are backing up:
DECLARE SpidsToKill CURSOR FOR
SELECT spid
FROM master..sysprocesses
WHERE dbid = 9
DECLARE @.Spid int
DECLARE @.SQL varchar(255)
OPEN SpidsToKill
FETCH NEXT FROM SpidsToKill
INTO @.Spid
WHILE ( @.@.FETCH_STATUS = 0 )
BEGIN
SELECT @.SQL = 'KILL ' + CONVERT( varchar(10), @.Spid )
EXEC ( @.SQL )
FETCH NEXT FROM SpidsToKill
INTO @.Spid
END
CLOSE SpidsToKill
DEALLOCATE SpidsToKill|||Ysandre wrote:
> I did, but I don't see anything in there regarding clearing connections to
> the database.
You want to use ALTER DATABASE to put the database in SINGLE_USER mode,
WITH immediate ROLLBACK of transactions...
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||That worked.
THANK YOU!!!
"10001110101" wrote:

> You could try something like this. Replace the DBID with the database
> you are backing up:
> DECLARE SpidsToKill CURSOR FOR
> SELECT spid
> FROM master..sysprocesses
> WHERE dbid = 9
> DECLARE @.Spid int
> DECLARE @.SQL varchar(255)
> OPEN SpidsToKill
> FETCH NEXT FROM SpidsToKill
> INTO @.Spid
> WHILE ( @.@.FETCH_STATUS = 0 )
> BEGIN
> SELECT @.SQL = 'KILL ' + CONVERT( varchar(10), @.Spid )
> EXEC ( @.SQL )
> FETCH NEXT FROM SpidsToKill
> INTO @.Spid
> END
> CLOSE SpidsToKill
> DEALLOCATE SpidsToKill
>|||Ysandre wrote:
> That worked.
> THANK YOU!!!
>
Ugh, it's too painful to watch... Drop that cursor nonsense, you can
accomplish this with ONE STATEMENT. Since you can't read documentation
and piece together the command yourself, here it is:
ALTER DATABASE DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
That will put the database in single-user mode, allowing one connection
only, and will gracefully rollback any outstanding transactions.
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||The problem with the script above to 'kill' sessions, is that a session may
immediately reconnect.
The BEST solution, is as Tracy advised. Use
ALTER DATABASE MyDatabase
SET SINGLE_USER WTIH ROLLBACK IMMEDIATE
--
Arnie Rowland
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
"Tracy McKibben" <tracy@.realsqlguy.com> wrote in message news:%23EmybC0rGHA.1592@.TK2MSFTNGP0
2.phx.gbl...
> Ysandre wrote:
>
> You want to use ALTER DATABASE to put the database in SINGLE_USER mode,
> WITH immediate ROLLBACK of transactions...
>
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com

2012年2月24日星期五

Disabled job continues to run

Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
when you disable a scheduled job, it would no longer run. Do you really have
to disable the schedule inside of the job in order to keep the job from
running and not just disable the job itself?
Thanks,
Jon
Disabling the job should be fine. Can you check the history and see who is
invoking it? Could it be that there's another job that runs this job using
sp_start_job?
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> when you disable a scheduled job, it would no longer run. Do you really
have
> to disable the schedule inside of the job in order to keep the job from
> running and not just disable the job itself?
> Thanks,
> Jon
|||Yes, I was thinking that may be the case also but I can find no evidence of
anyone or anything that was invoking the job other than its own schedule.
Once I disabled the schedule it stopped.
"Narayana Vyas Kondreddi" wrote:

> Disabling the job should be fine. Can you check the history and see who is
> invoking it? Could it be that there's another job that runs this job using
> sp_start_job?
> --
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
> news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> have
>
>
|||Hi,
After you have disabled the job, probably you could have checked "enabled"
column in sysjobs against this particular job, to see if its really been
disabled.
But its quite strange though.....
- - - - - - - - -
Thanks
Yogish
"Jon Goughnour" wrote:
[vbcol=seagreen]
> Yes, I was thinking that may be the case also but I can find no evidence of
> anyone or anything that was invoking the job other than its own schedule.
> Once I disabled the schedule it stopped.
> "Narayana Vyas Kondreddi" wrote:
|||Yes, quite strange. I wish I would've thought to check sysjobs but
unfortunately I didn't. I'm going to play around with this a little more on
a test server. Thanks.
"Yogish" wrote:
[vbcol=seagreen]
> Hi,
> After you have disabled the job, probably you could have checked "enabled"
> column in sysjobs against this particular job, to see if its really been
> disabled.
> But its quite strange though.....
> --
> - - - - - - - - -
> Thanks
> Yogish
>
> "Jon Goughnour" wrote:

Disabled job continues to run

Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
when you disable a scheduled job, it would no longer run. Do you really hav
e
to disable the schedule inside of the job in order to keep the job from
running and not just disable the job itself?
Thanks,
JonDisabling the job should be fine. Can you check the history and see who is
invoking it? Could it be that there's another job that runs this job using
sp_start_job?
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> when you disable a scheduled job, it would no longer run. Do you really
have
> to disable the schedule inside of the job in order to keep the job from
> running and not just disable the job itself?
> Thanks,
> Jon|||Yes, I was thinking that may be the case also but I can find no evidence of
anyone or anything that was invoking the job other than its own schedule.
Once I disabled the schedule it stopped.
"Narayana Vyas Kondreddi" wrote:

> Disabling the job should be fine. Can you check the history and see who is
> invoking it? Could it be that there's another job that runs this job using
> sp_start_job?
> --
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
> news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> have
>
>|||Hi,
After you have disabled the job, probably you could have checked "enabled"
column in sysjobs against this particular job, to see if its really been
disabled.
But its quite strange though.....
- - - - - - - - -
Thanks
Yogish
"Jon Goughnour" wrote:
[vbcol=seagreen]
> Yes, I was thinking that may be the case also but I can find no evidence o
f
> anyone or anything that was invoking the job other than its own schedule.
> Once I disabled the schedule it stopped.
> "Narayana Vyas Kondreddi" wrote:
>|||Yes, quite strange. I wish I would've thought to check sysjobs but
unfortunately I didn't. I'm going to play around with this a little more on
a test server. Thanks.
"Yogish" wrote:
[vbcol=seagreen]
> Hi,
> After you have disabled the job, probably you could have checked "enabled"
> column in sysjobs against this particular job, to see if its really been
> disabled.
> But its quite strange though.....
> --
> - - - - - - - - -
> Thanks
> Yogish
>
> "Jon Goughnour" wrote:
>

Disabled job continues to run

Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
when you disable a scheduled job, it would no longer run. Do you really have
to disable the schedule inside of the job in order to keep the job from
running and not just disable the job itself?
Thanks,
JonDisabling the job should be fine. Can you check the history and see who is
invoking it? Could it be that there's another job that runs this job using
sp_start_job?
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> when you disable a scheduled job, it would no longer run. Do you really
have
> to disable the schedule inside of the job in order to keep the job from
> running and not just disable the job itself?
> Thanks,
> Jon|||Yes, I was thinking that may be the case also but I can find no evidence of
anyone or anything that was invoking the job other than its own schedule.
Once I disabled the schedule it stopped.
"Narayana Vyas Kondreddi" wrote:
> Disabling the job should be fine. Can you check the history and see who is
> invoking it? Could it be that there's another job that runs this job using
> sp_start_job?
> --
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
> news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> > Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> > when you disable a scheduled job, it would no longer run. Do you really
> have
> > to disable the schedule inside of the job in order to keep the job from
> > running and not just disable the job itself?
> >
> > Thanks,
> >
> > Jon
>
>|||Hi,
After you have disabled the job, probably you could have checked "enabled"
column in sysjobs against this particular job, to see if its really been
disabled.
But its quite strange though.....
--
- - - - - - - - -
Thanks
Yogish
"Jon Goughnour" wrote:
> Yes, I was thinking that may be the case also but I can find no evidence of
> anyone or anything that was invoking the job other than its own schedule.
> Once I disabled the schedule it stopped.
> "Narayana Vyas Kondreddi" wrote:
> > Disabling the job should be fine. Can you check the history and see who is
> > invoking it? Could it be that there's another job that runs this job using
> > sp_start_job?
> > --
> > Vyas, MVP (SQL Server)
> > SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
> >
> >
> > "Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
> > news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> > > Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> > > when you disable a scheduled job, it would no longer run. Do you really
> > have
> > > to disable the schedule inside of the job in order to keep the job from
> > > running and not just disable the job itself?
> > >
> > > Thanks,
> > >
> > > Jon
> >
> >
> >|||Yes, quite strange. I wish I would've thought to check sysjobs but
unfortunately I didn't. I'm going to play around with this a little more on
a test server. Thanks.
"Yogish" wrote:
> Hi,
> After you have disabled the job, probably you could have checked "enabled"
> column in sysjobs against this particular job, to see if its really been
> disabled.
> But its quite strange though.....
> --
> - - - - - - - - -
> Thanks
> Yogish
>
> "Jon Goughnour" wrote:
> > Yes, I was thinking that may be the case also but I can find no evidence of
> > anyone or anything that was invoking the job other than its own schedule.
> > Once I disabled the schedule it stopped.
> >
> > "Narayana Vyas Kondreddi" wrote:
> >
> > > Disabling the job should be fine. Can you check the history and see who is
> > > invoking it? Could it be that there's another job that runs this job using
> > > sp_start_job?
> > > --
> > > Vyas, MVP (SQL Server)
> > > SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
> > >
> > >
> > > "Jon Goughnour" <JonGoughnour@.discussions.microsoft.com> wrote in message
> > > news:DC301B21-E312-44F4-8B72-EF883EF19913@.microsoft.com...
> > > > Thinks sounds a little crazy, and please tell me I'm wrong, but I thought
> > > > when you disable a scheduled job, it would no longer run. Do you really
> > > have
> > > > to disable the schedule inside of the job in order to keep the job from
> > > > running and not just disable the job itself?
> > > >
> > > > Thanks,
> > > >
> > > > Jon
> > >
> > >
> > >