Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Posts
    19

    Unanswered: Scope of Global Variable

    Hi!

    I want to know the scope of a Global Variable in case of multi users.

    Means i have declared a global variable in a function. And a new value is assigned to this global variable into this function, each time it is called.

    So if, 3 users call this function at same time, then will the get different gloabl variables or same?

    Regards,
    Shabber.

  2. #2
    Join Date
    Nov 2005
    Location
    San Francisco, CA
    Posts
    506
    "i have declared a global variable in a function"???

    It would be better if you paste your function here if possible...then it would be easy for us to reply your query.
    Last edited by rudra; 08-22-06 at 02:31.
    Success is the ability to go from one failure to another with no loss of enthusiasm.
    - Sir Winston Churchill
    Joydeep

  3. #3
    Join Date
    Mar 2004
    Posts
    19
    Below is the procedure , please see:

    CREATE PROCEDURE p1
    AS
    BEGIN
    DECLARE @@Global_Var INT

    INSERT INTO TABLE1 ([NAME]) VALUES('shabber')

    SELECT @@Global_Var = @@IDENTITY
    PRINT('val')
    PRINT(@@Global_Var)
    END

  4. #4
    Join Date
    Nov 2005
    Location
    San Francisco, CA
    Posts
    506
    Global variables were those system-supplied, predeclared variables that
    were distinguished from local variables by having two at symbols (@@)
    preceding their names.These functions are global only in the sense that any
    connection can access their values.
    However, in many cases the value returned by these functions is specific to the connection. For example, @@ERROR represents the last error
    number generated for a specific connection, not the last error
    number in the entire system. @@ROWCOUNT represents the number of rows selected or affected by the last statement for the
    current connection.And same in the case of @@IDENTITY.It returns the last identity value created by the current connection.
    I hope you have got your answer.
    Success is the ability to go from one failure to another with no loss of enthusiasm.
    - Sir Winston Churchill
    Joydeep

  5. #5
    Join Date
    Mar 2004
    Posts
    19
    Thanks Rudra!

    I got my answer.

    Best Regards,
    Shabber Abbas.

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    You cannot declare your own global variables.

    You can simulate global constants using User-defined functions.

    Local variables will work for the example you gave:
    Code:
    CREATE PROCEDURE p1
    AS 
    BEGIN
    DECLARE @Local_Var INT
    
    INSERT INTO TABLE1 ([NAME]) VALUES('shabber')
    
    SELECT @Local_Var = @@IDENTITY 
    PRINT('val')
    PRINT(@Local_Var)
    END
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  7. #7
    Join Date
    Nov 2005
    Location
    San Francisco, CA
    Posts
    506
    Quote Originally Posted by shabber
    Below is the procedure , please see:

    CREATE PROCEDURE p1
    AS
    BEGIN
    DECLARE @@Global_Var INT

    INSERT INTO TABLE1 ([NAME]) VALUES('shabber')

    SELECT @@Global_Var = @@IDENTITY
    PRINT('val')
    PRINT(@@Global_Var)
    END
    oops !!, I forgot to mention that, here @@Global_Var is a local one and it's name would be '@Global_Var'.The first @ would be treated as At and the second @will be treated as the variable name i.e.''@Global_Var'.
    Success is the ability to go from one failure to another with no loss of enthusiasm.
    - Sir Winston Churchill
    Joydeep

  8. #8
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    DECLARE @@blindman VARCHAR(50)
    SET @@blindman = 'was here'
    SELECT 'Blindman ' + @@blindman
    -PatP

  9. #9
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    That is still a local variable.
    Code:
    DECLARE @@@@@@@@@@blindman VARCHAR(50)
    SET @@@@@@@@@@blindman = 'was here'
    SELECT 'Blindman ' + @@@@@@@@@@blindman
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  10. #10
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Quote Originally Posted by blindman
    That is still a local variable.
    Surprisingly enough, the local / global ness of that variable actually depends on what build of SQL Server you're running. I don't remember the exact details, but at one point in time you could actually declare new globals, which is what lead them to decide to accept the syntax.

    -PatP

  11. #11
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    What version? And why would the deprecate the functionality?
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  12. #12
    Join Date
    Mar 2004
    Posts
    19
    Dear All,

    Thanks for ur help.

    any way i got wat i wanted to do.

    Regards,
    Shabber.

Posting Permissions

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