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

  • SEARCH
  • Home
  • 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 6982269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:15:57+00:00 2026-05-27T18:15:57+00:00

If a constant value is required in multiple stored procedures and functions in a

  • 0

If a “constant” value is required in multiple stored procedures and functions in a database, is there a standard way to define it in one place so it is available everywhere?

For example, suppose I use xp_logevent in CATCH block to write something to the event log when RAISERROR happens, but I want to group the severity into informational, warning, and error based on the RAISERROR severity.

I could set a constant EventSeverity such that:

  • if RAISERROR severity = 0 then xp_logevent is informational.
  • if RAISERROR severity <= EventSeverity than xp_logevent is warning.
  • if RAISERROR severity > EventSeverity than xp_logevent is error.

The cut-off between warning and error severity is unlikely to change, but if it ever does I want to change it in one place only.

I thought of these possibilities:

  • Use a ‘@@variable‘ to store the value.

    • Advantages: Low access overhead. Easy to access in code.
    • Disadvantages: Imposes execution order, variable must be declared and set before other procedures and functions can access it. Changing value means changing code.

      DECLARE @@EventSeverity INT = 9
      ...
      BEGIN CATCH
          IF ERROR_SEVERITY() < @@EventSeverity
              ...
          ELSE
              ...
      END CATCH
      
  • Use a function to return the value.

    • Advantages: Fairly low access overhead. Easy to access in code.
    • Disadvantages: Changing value means changing code.

      CREATE FUNCTION dbo.EventSeverity()
      RETURNS INT
      AS
      BEGIN
          RETURN 9
      END
      ...
      BEGIN CATCH
          IF ERROR_SEVERITY() < dbo.EventSeverity()
              ...
          ELSE
              ...
      END CATCH
      
  • Use a “settings” table to store the value.

    • Advantages: Changing value means changing data.
    • Disadvantages: High access overhead. Difficult to access in code. Difficult to use as a parameter. User could change value.

      CREATE TABLE dbo.Settings
      (
          Name VARCHAR(...),
          Value VARCHAR(...)
      )
      ...
      INSERT INTO dbo.Settings (Name, Value)
      VALUES ('EventSeverity', CAST(9 AS VARCHAR))
      ...
      BEGIN CATCH
          IF ERROR_SEVERITY() < (SELECT CAST(Value AS INT) FROM dbo.Settings WHERE Name = 'EventSeverity')
              ...
          ELSE
              ...
      END CATCH
      
  • Use a “settings” table with a function to simplify access.

    • Advantages: Easy to change the value. Easy to access in code.
    • Disadvantages: High overhead. User could change value.

      CREATE TABLE dbo.Settings
      (
          Name VARCHAR(...),
          Value VARCHAR(...)
      )
      ...
      INSERT INTO dbo.Settings (Name, Value)
      VALUES ('EventSeverity', CAST(9 AS VARCHAR))
      ...
      CREATE FUNCTION dbo.EventSeverity()
      RETURNS INT
      AS
      BEGIN
          DECLARE @result INT
          SET @result = (SELECT CAST(Value AS INT) FROM dbo.Settings WHERE Name = 'EventSeverity')
          IF @result IS NULL
              SET @result = 9
          RETURN @result
      END
      ...
      BEGIN CATCH
          IF ERROR_SEVERITY() < dbo.EventSeverity()
              ...
          ELSE
              ...
      END CATCH
      

Is there a best practice way to do this?

  • 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-27T18:15:57+00:00Added an answer on May 27, 2026 at 6:15 pm

    All else being equal, I would settle for the hard-coded FUNCTION, for performance. For security, this function should be put in a distinct SCHEMA, like MyDatabase.CONF.SettingsFunc instead of the usual DBO; with permissions set to this schema so only the administrators would have permission to change data.

    If you need to centralize various configuration settings for many different uses, then the last approach (FUNCTION+TABLE) would have a larger appeal, providing you create an index for each and every use case. Likewise, this “settings” table should be in a restricted schema, unlike the function, which could remain in the default schema for ease of coding.

    But, if it is mandatory that the default schema is to be used, it becomes interesting to configure an “INSTEAD OF UPDATE” trigger in this “settings” table, so the user wouldn’t change data easily; don’t forget that this latter approach can not be called “security” as the user could still change (or drop!) the trigger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does the compiler say a constant value is required for the first case...the
I want to force subclasses to define a constant value. Like const string SomeConstantEverySubclassMustDefine
In grails, is there a way in the i18n file to reference a constant
I have a constant value that I only plan to use once in my
I want to add a constant value onto an incoming bound integer. In fact
Is it possible to access a constant value (i.e. a public static final variable
If you want to associate some constant value with a class, here are two
In SQL you can selecta constant value: Select Constant Text, Column1, Column2 From TableX
Why do I get the error: Unable to create a constant value of type
I am getting an error message expression must have constant value when initializing an

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.