0

I add column to table as:

RedMarkItemsId (IDENTITY(1,1) NOT NULL PRIMARY KEY)
TaskId (UNIQUEIDENTIFIER)
CreationDate (DATETIME)
CreatedBy (UNIQUEIDENTIFIER)

I want to create column like:

  ALTER TABLE RedMarkItems ADD Item1 BIT DEFAULT(0)

But when I try to insert some values:

INSERT INTO RedMarkItems 
VALUES (
    '9B093907-2072-4F59-A55C-0003CE89EF9E', 
    GETDATE(), 
    '6074CAEA-7A8E-4699-9451-16C2EAF394EF')

It throws error:

Column name or number of supplied values does not match table definition.

Why I'm getting this error if I set DEFAULT(0) to that new column?

Table create script:

CREATE TABLE RedMarkItems 
(
    RedMarkItemsId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    TaskId UNIQUEIDENTIFIER,
    CreationDate DATETIME,
    CreatedBy UNIQUEIDENTIFIER
)
2
  • In my insert statement I have TaskId, CreationDate and CreatedBy, I don't need to specify RedMarkItemsId because it's an IDENTITY(1,1), so it's autonumeric field @gtosto Commented Dec 31, 2018 at 19:17
  • Think about it a bit more: that identity could instead be anywhere positionally in the list of columns, so SQL Server can't guess that the 3 values you supplied are supposed to go into the 3 columns you named; you have to tell it. This is a good thing: the language is being consistent, rather than being overly charitable by making an assumption if the identity happens to be the 1st column, where that assumption could lead to unexpected results and would only work in 1 situation. Commented Dec 31, 2018 at 19:40

1 Answer 1

8

This piece of SQL is not valid :

INSERT INTO RedMarkItems 
VALUES (
    '9B093907-2072-4F59-A55C-0003CE89EF9E', 
    GETDATE(), 
    '6074CAEA-7A8E-4699-9451-16C2EAF394EF')

When not providing all columns for insert, you need to explicitly specify the target columns. As you are assigning only 3 columns, you probably want :

INSERT INTO RedMarkItems(
    RedMarkItemsId, 
    TaskId, 
    CreationDate
) VALUES (
    '9B093907-2072-4F59-A55C-0003CE89EF9E', 
    GETDATE(), 
    '6074CAEA-7A8E-4699-9451-16C2EAF394EF'
);
Sign up to request clarification or add additional context in comments.

1 Comment

It seems, RedMarkItemsId should be replaced with TaskId. He wants to omit the RedMarkItemsId as it is an Identity column...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.