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 8791481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:50:55+00:00 2026-06-13T22:50:55+00:00

I’d like to use the column’s default value in an stored procedure insert, so

  • 0

I’d like to use the column’s default value in an stored procedure insert, so that I don’t have to repeat the default value in multiple places (it could change… DRY principle).

The T-SQL INSERT operation has a handy ‘default’ keyword that I can use as follows:

Declare @newA varchar(10)
Set @newA = 'Foo2'

-- I can use "default" like so...
Insert into Table_1 (
    A, 
    B) 
Values (
    @newA, 
    default)

However, If I need to do something conditional, I can’t seem to get the case statement to return ‘default’.

-- How do I use 'default' in a case statement? 
INSERT INTO Table_1 (
    A,
    B )
VALUES (
    @newA,
    CASE WHEN (@newA <> 'Foo2') THEN 'bar' ELSE default END)
-- > yeilds "Incorrect syntax near the keyword 'default'."

I could insert the default, and then update as needed like so:

INSERT INTO Table_1 (
    A,
    B )
VALUES (
    @newA,
    default)
UPDATE Table_1 
SET B = CASE WHEN (A <> 'Foo2') THEN 'bar' ELSE B END
WHERE ID = SCOPE_IDENTITY()

But I’d really like somebody to tell me “There’s a better way…”

Here’s a table definition for this example if it helps…

CREATE TABLE dbo.Table_1 (
    ID int NOT NULL IDENTITY (1, 1),
    A varchar(10) NULL,
    B varchar(10) NULL  )  
GO
ALTER TABLE dbo.Table_1 ADD CONSTRAINT DF_Table_1_A DEFAULT 'A-Def' FOR A
GO
ALTER TABLE dbo.Table_1 ADD CONSTRAINT DF_Table_1_B DEFAULT 'B-Def' FOR B
GO
  • 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-06-13T22:50:56+00:00Added an answer on June 13, 2026 at 10:50 pm

    default only works from within a VALUES() block, which does not seem to be an acceptable value in a CASE statement; you could use an if statement to determine what to insert:

    DECLARE @newA varchar(10) = 'Foo2'
    
    IF (@newA <> 'Foo2')
    BEGIN
       INSERT INTO Table_1 (A, B)
       SELECT @newA, 'bar'
    END
    ELSE
    BEGIN
       --If you are using default values, you do not have to specify the column
       INSERT INTO Table_1 (A) 
       SELECT @newA
    END
    

    I think this is better than updating after an insert, so that you only insert correct data into your table. It also keeps the number of INSERTS/UPDATES to 1. You should also be careful when you using @@IDENTITY due to scoping. Consider looking into SCOPE_IDENTITY().

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have two tables with like below codes: Table: Accounts id | username |
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text

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.