constant variable

  • We have a default OrgnizationID we need to use in our database. It's a constant integer variable.

    In quite a few stored procedures we use it.

    But sometimes the default OrganizaitonID changes, instead of changing in all the stored procedure about the constant variable, is there another way to do this?

    can we setup a globle variable in sql server?

    Thanks

  • One possible method is to create a simple scalar function to return the value.

  • Put it into a 'Consts' table

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Yes, we ended up to create a simple scalar function to return the value.

    And what is the const table?

  • A table that you create to store constants.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I've done what I think Gail suggests and used a table as a global variable store, or a definition table. You store values in there that are needed in the application, and are likely "constant", but could potentially change (rarely). Almost like a configuration values table.

  • Thanks, so in this approach of storing constant in the table, how can we use it in sproc,

    To select the value from the table? Or use a function?

  • Build a table with constants in it.

    Move it to a read-only filegroup, so that it avoids locks/blocks/et al, in terms of contention.

    Select from it when you need to use a value.

    If you ever need to change it, change the file to read-write, update it, then change it back to read-only.

    - 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

  • Use Gus' advice. You could use a function, but a simple select works just as well. Depends on how you want to encapsulate this for developers.

  • One big advantage of a const table (I have always called mine control) is that you can very easily get the values out of this table (assuming there are not very many columns and ONLY 1 row). just add a cross join to your table and you are all set.

    declare @Code int = 10

    select * from myTable

    where myTable.Code = @Code

    becomes

    select * from MyTable

    cross join control c

    where MyTable.Code = c.Code

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 10 posts - 1 through 9 (of 9 total)

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