Getting my Current Session ID

  • What command can I run to I get the session ID for my current connection to a database in TSQL? This is related to my previous question here about how to show current and past connections and connection information to a database.

    Thank you

  • @@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

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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_USERorALTER 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

  • 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

  • Thanks guys.

Viewing 6 posts - 1 through 5 (of 5 total)

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