Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

constant variable Expand / Collapse
Author
Message
Posted Friday, November 4, 2011 10:02 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 10:18 AM
Points: 1,787, Visits: 3,241
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

Post #1200728
Posted Friday, November 4, 2011 10:11 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, October 23, 2014 6:35 AM
Points: 262, Visits: 352
One possible method is to create a simple scalar function to return the value.
Post #1200738
Posted Friday, November 4, 2011 10:12 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:37 PM
Points: 40,210, Visits: 36,619
Put it into a 'Consts' table



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1200739
Posted Friday, November 4, 2011 10:20 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 10:18 AM
Points: 1,787, Visits: 3,241
Yes, we ended up to create a simple scalar function to return the value.

And what is the const table?
Post #1200744
Posted Friday, November 4, 2011 10:34 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:37 PM
Points: 40,210, Visits: 36,619
A table that you create to store constants.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1200760
Posted Friday, November 4, 2011 10:47 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 4:00 PM
Points: 31,181, Visits: 15,627
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1200765
Posted Friday, November 4, 2011 10:55 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 10:18 AM
Points: 1,787, Visits: 3,241
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?
Post #1200770
Posted Friday, November 4, 2011 11:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
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
Post #1200779
Posted Friday, November 4, 2011 12:20 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 4:00 PM
Points: 31,181, Visits: 15,627
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1200826
Posted Friday, November 4, 2011 12:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 1:04 PM
Points: 13,086, Visits: 12,553
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1200834
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse