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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:54:18+00:00 2026-06-11T22:54:18+00:00

Our SSDT database project includes a table that has a computed column that can

  • 0

Our SSDT database project includes a table that has a computed column that can take one of several forms, depending on customer requirements. I’m trying to figure out how to manage this computed column so that we can still use the Publish function without reverting everyone’s columns back to the default.

What I’m trying to accomplish can be explained in the following invalid T-SQL code:

CREATE TABLE dbo.Customer
(
  Id INTEGER,
  Region INTEGER,
  Name VARCHAR(50),
  AccountNumber AS dbo.FormatAccountNumber(Id, Region)
)

CREATE FUNCTION [dbo].[FormatAccountNumber]
(
  @Id INTEGER,
  @Region INTEGER
)
RETURNS VARCHAR(20)
AS
BEGIN
  IF '$(AccountType)' = 'Regional'
    RETURN CONVERT(VARCHAR, @Region) + '-' + CONVERT(VARCHAR, @Id)

  IF '$(AccountType)' = 'Merged'
    RETURN CONVERT(VARCHAR, @Region * 100000 + @Id)

  IF '$(AccountType)' = 'Flat'
    RETURN CONVERT(VARCHAR, @Id)
END

This, of course, doesn’t work because the $(AccountType) SQLCMD variable can’t be used inside of the function, and wouldn’t be set properly at run-time anyway. I’ve also trying putting the SQLCMD conditional around the entire function:

IF '$(AccountType)' = 'Flat'
  CREATE FUNCTION ...

but this produces the error that “CREATE FUNCTION must be the only statement in the batch.”

Is there any way to do any sort of conditional compilation of schema in the SSDT project? And if not, what options do I have for maintaining this sort of customizable field within the SSDT publishing process?

  • 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-11T22:54:20+00:00Added an answer on June 11, 2026 at 10:54 pm

    You could use a Post-Deploment script to imperatively deploy your object in dynamic SQL:

    IF NOT EXISTS (SELECT * FROM sys.objects 
                WHERE object_id = OBJECT_ID(N'[dbo].[FormatAccountNumber]') 
                AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    EXEC('CREATE FUNCTION [dbo].[FormatAccountNumber] () RETURNS BIT AS BEGIN RETURN 0 END')
    GO 
    
    IF '$(AccountType)' = 'Regional'
    BEGIN
        EXEC('
            ALTER FUNCTION [dbo].[FormatAccountNumber]
            (
              @Id INTEGER,
              @Region INTEGER
            )
            RETURNS VARCHAR(20)
            AS
            BEGIN
                RETURN CONVERT(VARCHAR, @Region) + ''-'' + CONVERT(VARCHAR, @Id)
            END
        ')
    END
    

    Note that by doing this, you won’t be able to make any references to the dbo.FormatAccountNumber function in your SSDT Database Project (unless those objects also included in the Post-Deployment script).

    I also toyed with an alternate solution which involved addition conditionals within the .sqlproj file itself (within the ItemGroup elements), but this did get a bit messy since MsBuild Properties aren’t exactly like-for-like with SQLCMD Variables, but I can post this if you like.

    If you find yourself needing to conditionally deploy objects in your database often, you may like to consider moving to an imperative-based deployment solution (as opposed to the declarative style of SSDT projects). Imperative deployment, often called migrations, gives you greater control of deploy-time behaviour.

    Disclaimer: I am the founder of ReadyRoll, which makes a product for VS that uses imperative deployment.

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

Sidebar

Related Questions

Our application has a table which stores all transactions records of clients in one
Our Customer table has an int Identity column for ID. This was going to
Our Application has a Class that wraps Jersey REST functionality. One Method is public
Our project's template html has a quirks declaration,which is <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML
Our team (QA) is facing the following problem: We have a database that is
Our ASP.NET 3.5 website running on IIS 6 has two teams that are adding
Our support team has diagnosed some specific areas of a computer's configuration that may
Our team has an applet that is working fine in Oracle JRE 7u5. Upon
Our company has a share point document server where the UNC looks something like
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have

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.