Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 964197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:47:20+00:00 2026-05-16T01:47:20+00:00

I have seen several patterns used to ‘overcome’ the lack of constants in SQL

  • 0

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:

  • Just to hard code it, and possibly just ‘comment’ the status
-- StatusId 87 = Loaded
SELECT ... FROM [Table] WHERE StatusId = 87;
  • Using a lookup table for states, and then joining to this table so that the WHERE clause references the friendly name.

SubQuery:

SELECT ... 
FROM [Table] 
WHERE 
  StatusId = (SELECT StatusId FROM TableStatus WHERE StatusName = 'Loaded');

or joined

SELECT ... 
FROM [Table] t INNER JOIN TableStatus ts On t.StatusId = ts.StatusId 
WHERE ts.StatusName = 'Loaded';
  • A bunch 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();

(IMO this causes a lot of pollution in the database – this might be OK in an Oracle package wrapper)

  • And similar patterns with Table Valued Functions holding the constants with values as rows or columns, which are CROSS APPLIED back to [Table]

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:47:20+00:00Added an answer on May 16, 2026 at 1:47 am

    Hard coded. With SQL performance trumps maintainability.

    The consequences in the execution plan between using a constant that the optimizer can inspect at plan generation time vs. using any form of indirection (UDF, JOIN, sub-query) are often dramatic. SQL ‘compilation’ is an extraordinary process (in the sense that is not ‘ordinary’ like say IL code generation) in as the result is determined not only by the language construct being compiled (ie. the actual text of the query) but also by the data schema (existing indexes) and actual data in those indexes (statistics). When a hard coded value is used, the optimizer can give a better plan because it can actually check the value against the index statistics and get an estimate of the result.

    Another consideration is that a SQL application is not code only, but by a large margin is code and data. ‘Refactoring’ a SQL program is … different. Where in a C# program one can change a constant or enum, recompile and happily run the application, in SQL one cannot do so because the value is likely present in millions of records in the database and changing the constant value implies also changing GBs of data, often online while new operations occur.

    Just because the value is hard-coded in the queries and procedures seen by the server does not necessarily mean the value has to be hard coded in the original project source code. There are various code generation tools that can take care of this. Consider something as trivial as leveraging the sqlcmd scripting variables:

    defines.sql:

    :setvar STATUS_LOADED 87
    

    somesource.sql:

    :r defines.sql
    SELECT ... FROM [Table] WHERE StatusId = $(STATUS_LOADED);
    

    someothersource.sql:

    :r defines.sql
    UPDATE [Table] SET StatusId = $(STATUS_LOADED) WHERE ...;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen several questions regarding SocketException when using Android, but none of them
I have seen several questions with people asking about the same problem but none
I have seen several posts similar to this but none with a strait forward
I have seen several similar questions to this, but none that addresses my specific
This is out of curiosity, but I have seen several (and some of them
I have seen several references to people running Redis on Azure, but no implementation
I have seen several similar questions, but not found the exact answer I'm looking
I have seen several developers perform: string fileStore = Server.MapPath(@~\someDirectory); File.Create(fileStore + someFileName.xxx); I
I have seen several places that Class.getClassLoader() returns the ClassLoader used to load that
I have seen several very similar questions on stackoverflow, but haven't come across anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.