Results 1 to 7 of 7

Thread: Global Variable

  1. #1
    Join Date
    Mar 2004
    Posts
    19

    Unanswered: Global Variable

    Dear All!

    I want to know that how can i declare a global variable in database, assign some value to it, then using it in multiple triggers and procedure then deallocating that.

    Please provide a smal example.

    Regards,
    Shabber.

  2. #2
    Join Date
    Jul 2002
    Posts
    229
    Create a User-Defined Function that returns the value?

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    And deallocate it?

    What the heck are you doing?

    UDF is the only way to simulate a global variable, but if you then wipe out your UDF it will break your sprocs.
    If it's not practically useful, then it's practically useless.

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

  4. #4
    Join Date
    Jul 2002
    Posts
    229
    I'd assume Shabber has no big need for deallocating. It's probably just that if there would have been such a thing as a global variable, then it would have been a good habit to deallocate once it wasn't needed anymore.

  5. #5
    Join Date
    Mar 2004
    Posts
    19
    Thanks for replying.

    But how function will provide the functionality of Global Variables. A bit confusing.

    Regards,
    Shabber.

  6. #6
    Join Date
    Jul 2002
    Posts
    229
    You call the function, which will return the value you're after.

  7. #7
    Join Date
    Dec 2002
    Posts
    1,245
    Quote Originally Posted by shabber
    Thanks for replying.

    But how function will provide the functionality of Global Variables. A bit confusing.

    Regards,
    Shabber.
    Code:
    CREATE TABLE MyParameter (
       SiteID int IDENTITY (1,1) NOT NULL,
       SiteName varchar(255) NOT NULL
    )
    Code:
    CREATE FUNCTION udfMyFunction 
    	(@p1 int)
    RETURNS varchar(255)
    AS
    BEGIN
    	DECLARE @sTemp varchar(255)
    
    	SELECT @sTemp = SiteName FROM MyParameter WHERE SiteID = @p1
    	RETURN @sTemp
    END
    Example Data and Usage:
    Code:
    insert into MyParameter(SiteName) Values ('Foo')
    insert into MyParameter(SiteName) Values ('Bar')
    
    SELECT dbo.udfMyFunction (2)
    I don't know that this example is all that useful, but maybe it will give you some ideas.

    Regards,

    hmscott
    Have you hugged your backup today?

Posting Permissions

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