Hi
I have seen several patterns used to 'overcome' the lack of constants in SQL Server, but none of them seem to satisfy both performance and readability / maintainability concerns.
In the below example, assuming that we have an integral 'status' classification on our table, the options seem to be:
1) Just hard code it, and possibly just 'comment' the status
-- StatusId 87 = Loaded
SELECT ... FROM [Table] WHERE StatusId = 87
2) Using a lookup table for states, and then joining to this table so that the WHERE clause references the friendly name.
SELECT ... FROM [Table] WHERE StatusId = (SELECT StatusId from TableStatus WHERE
StatusName = 'Loaded')
OR
SELECT ... FROM [Table] t INNER JOIN TableStatus ts On t.StatusId = ts.StatusId WHERE ts.StatusName = 'Loaded'
3) And most recently have seen a 'fleet' of scalar UDF's defined which return constants, viz
CREATE Function LoadedStatus()
RETURNS INT
AS
BEGIN
RETURN 87
END
and then
SELECT ... FROM [Table] WHERE StatusId = LoadedStatus()
How have other SO users have solved this common issue?
Edit : Bounty - Does anyone have a best practice method for maintaining $(variables) in DBProj DDL / Schema scripts as per Remus answer and comment?