Question: What are some other strategies on avoiding magic numbers or hard-coded values in your SQL scripts or stored procedures?
Consider a stored
Imagine
table dbo.Status
(
Id int PK
,Description varchar
)
values
1, Received
2, Acknowledged
3, Under Review
etc
So, just
declare @StatusReceived int = 1
declare @StatusAcknowledged int = 2
declare @StatusUnderReview = 3
etc
As others mention, this assumes that IDENTITY is not set.
I too used to JOIN on lookup tables, but this keeps the SELECT shorter and easier to read.
This approach lends itself to automation, so I generate an entire table in a separate query, then copy over the elements I require (not all of them).