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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:04:20+00:00 2026-05-11T11:04:20+00:00

I am trying to add a column (MSSQL 2005) to a table (Employee) with

  • 0

I am trying to add a column (MSSQL 2005) to a table (Employee) with a default constraint of a primary key of another table (Department). Then I am going to make this column a FK to that table. Essentially this will assign new employees to a base department based off the department name if no DepartmentID is provided.
This does not work:

DECLARE     @ErrorVar       INT DECLARE     @DepartmentID       INT  SELECT      @DepartmentID = DepartmentID FROM        Department WHERE       RealName = 'RocketScience'  ALTER TABLE     [Employee] ADD             [DepartmentID] INT NULL CONSTRAINT      [DepartmentIDOfAssociate] DEFAULT (@DepartmentIDAssociate) SELECT @ErrorVar = @@Error IF (@ErrorVar <> 0) BEGIN     GOTO FATAL_EXIT END 

The Production, Test, and Development databases have grown out of synch and the DepartmentID for the DepartmentName = ‘RocketScience’ may or may not be the same so I don’t want to just say DEFAULT (somenumber). I keep getting “Variables are not allowed in the ALTER TABLE statement” no matter which way I attack the problem.
What is the correct way to do this? I have tried nesting the select statement as well which gets “Subqueries are not allowed in this context. Only scalar expressions are allowed.”

In Addition, what would be really great I could populate the column values in one statement instead of doing the

{ALTER null}
{Update values}
{ALTER not null}

steps. I read something about the WITH VALUES command but could not get it to work. Thanks!!!

  • 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. 2026-05-11T11:04:21+00:00Added an answer on May 11, 2026 at 11:04 am

    You could wrap the code to find your department ID into a stored function and use that in your DEFAULT constraint statement:

    CREATE FUNCTION dbo.GetDepartment() RETURNS INT AS BEGIN   DECLARE         @DepartmentID           INT    SELECT          @DepartmentID = DepartmentID   FROM            Department   WHERE           RealName = 'RocketScience'    RETURN @DepartmentID END 

    And then:

    ALTER TABLE     [Employee] ADD                     [DepartmentID] INT NULL CONSTRAINT      [DepartmentIDOfAssociate] DEFAULT (dbo.GetDepartment()) 

    Does that help?

    Marc

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

Sidebar

Related Questions

I'm trying to set an unique constraint on the column LoginName of table User.
I am trying to add a column to my existing users table but it
I'm trying to add a composite primary key to a class and having a
I am trying to add a spatial index to a table column named Location
I have a column named 'parent_id' that I am trying to add a default
I am trying to add a column into mysql table using this query but
I am trying to add a column after another column while also specifying a
Hi every One I am trying to add column in Mysql. My Sql Query
I am trying to add a new column into an SQLiteDatabase but every time
I am trying to add bootstrap drop down button in a column of jqGrid

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.