March 17, 2010 at 9:05 am
@@SPID
- 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
March 17, 2010 at 9:06 am
I didn't see your previous post, but are you looking for @@SPID?
select @@SPID will give you your current connection id, is that what you are looking for?
here's a collection of some of the functions you can use to get more information about your own login:
select @@spid,
user_name() AS [user_name],
suser_name() AS [suser_name],
current_user AS [current_user],
system_user AS [system_user],
session_user AS [session_user],
user AS
Lowell
March 18, 2010 at 1:01 am
Thanks guys. Yes, @@SPID is what I was looking for.
When adding columns to a table via our admin app, I need to show the admin user that the users X, Y and Z are still connected. They need to be disconnected to ensure data integrity in those tables. Normally the admin user would have told the users to exit the system, but I don't want to rely entirely on that (users can be an interesting bunch after all 🙂 .) If the admin user okays it, the admin app will disconnect the users.
I was going to do it via a for loop in the app, merely KILLing all session ids that are not the current user's and that are connected to the target database. However, is there a better way? Over at bytes.com "Hugo Kornelis, SQL Server MVP" posted
ALTER DATABASE <dbname> SET SINGLE_USER
orALTER DATABASE <dbname> SET SINGLE_USER WITH ROLLBACK_IMMEDIATE
but my SSMS gives me a syntax error when I execute WITH ROLLBACK_IMMEDIATE. Is there a better way that ensures that transactions get rolled back? Should I just KILL the other sessions one by one and let their transactions roll back one by one or can I get all the transactions to roll back in parallel?
Thanks
March 18, 2010 at 4:14 am
GDI Lord (3/18/2010)
Users can be an interesting bunch after all
Amen.
I was going to do it via a for loop in the app, merely KILLing all session ids that are not the current user's and that are connected to the target database. However, is there a better way?
The correct syntax is:
-- Kick everyone out, rolling back
-- their transactions if necessary
ALTER DATABASE AdventureWorks
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
--
-- Do admin stuff
--
-- Restore access to the database
ALTER DATABASE AdventureWorks
SET MULTI_USER;
Notice the lack of an underscore between ROLLBACK and IMMEDIATE 🙂
Paul
March 18, 2010 at 4:42 am
Thanks guys.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply