Import Task / Identity column throwing error - sql

Software: SSMS 2008 R2, Visual Studio 2010 Ultimate (for creating objects)
This is very strange and I would love for someone to be able to explain it to me. I have two versions of the same table. The production version has two fields, and the Test version has the same two fields PLUS a RecordID field (identity field).
Here's the table definition in Test:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity not null,
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)
Here's where the error gets thrown... I try to refresh the Test version with the production data (Field1 & field2). Since the production version doesn't have the identity column, I don't map it. The identity column should auto-increment. My research shows that if you don't explicitly state seed and increment, the default is (1,1). The import task wizard fails (even with Enable Identity Insert checked!). The error:
"Cannot insert NULL value into field RecordID."
Here is the REALLY strange part. Initially, I thought the error was that the seed and increment were missing, so I modified the table definition as follows:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity(1,1) not null,
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)
Then I tried to refresh the table again, and the EXACT SAME SSIS package succeeded and copied both columns and auto-incremented the RecordID field.
My question: What the heck!? Why does explicitly stating seed/increment allow the import wizard to insert values and auto-increment if the default when you do not explicitly state them is still 1,1?

Just remove the NOT NULL in smallint identity of RecordId column. Because the identity is already not null:
CREATE TABLE [dbo].[Table]
(
[Field1] varchar(50) NOT NULL,
[Field2] varchar(50) NOT NULL,
[RecordId] smallint identity(1,1),
constraint [CIDX_Table] unique clustered ([RecordId]) with (data_compression=page),
)

Related

Create an auto incrementing alpha numeric primary key in SQL Server Management Studio

I have a Student table in SQL Server database which is as follows:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I want the Id property to be alpha-numeric and auto-increment itself for a new entry. I want Id to be S<number> and then S<number+1> and so on.
I tried to solve this problem as a two-step process:
(i) I first tried to make the Id an auto-incrementing property by doing this:
Then I pressed "Update":
And then I updated again and it led me to this table:
CREATE TABLE [dbo].[Student] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
I do not think Id is an auto-incrementing value yet. How can I make it both auto-incrementing and alpha-numeric from the following interface:
It seems that you don't really want a fully auto-incrementing alphanumeric column A001,A002...B001, you just want a regular integer column with a prefix of S. For this you can use a simple computed column
ALTER TABLE Student
ADD MyId AS CONCAT('S', Id);

SQL Server: Using binary_checksum column on a table best practice

Looking at table definitions on an SQL server database have noticed that the (1) binary_checksum column sometimes includes the primary key my_table_id and sometimes does not. What is the best practice?
(2) Also what about the update_by and update_timestamp should they be included or not?
CREATE TABLE [dbo].[my_table] (
[my_table_id] SMALLINT NOT NULL PRIMARY KEY,
[a] SMALLINT NOT NULL,
[b] CHAR(25) NOT NULL,
[update_timestamp] DATETIME NOT NULL DEFAULT getdate(),
[update_by] CHAR(8) NOT NULL,
[my_checksum_col] AS (binary_checksum([a], [g], [update_by], [update_timestamp]))
)
VS
CREATE TABLE [dbo].[my_table] (
[my_table_id] SMALLINT NOT NULL PRIMARY KEY,
[a] SMALLINT NOT NULL,
[b] CHAR(25) NOT NULL,
[update_timestamp] DATETIME NOT NULL DEFAULT getdate(),
[update_by] CHAR(8) NOT NULL,
[my_checksum_col] AS (binary_checksum([my_table_id],[a], [g], [update_by], [update_timestamp]))
)
This may be a matter of opinion, but it depends on how the checksum is going to be used. If the primary key is auto-generated (such as an identity or newid() column), then including it in the checksum is not very interesting. At least, you can't use the checksum to find duplicates.
If the primary key is a data key provided externally, then it is functioning as both data and as a primary key. In that case, including it in the checksum makes more sense.

Cannot insert the value NULL into column 'Id' although Id is set

I have a strange problem.
I want to insert an item to a table from database. I use Entity Framework.
Although the Id is set, I keep getting the following error:
Cannot insert the value NULL into column 'Id', table 'project_atp.dbo.ShoppingCarts'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
The table definition:
CREATE TABLE [dbo].[ShoppingCarts] (
[Id] INT NOT NULL,
[Guid] UNIQUEIDENTIFIER NULL,
[Name] NVARCHAR (255) NULL,
[Code] NVARCHAR (255) NULL,
[SupplierNo] NVARCHAR (255) NULL,
[SupplierName] NVARCHAR (255) NULL,
[Price] NVARCHAR (50) NULL,
[Quantity] INT NULL,
CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Can you please advise what could be wrong here! Thanks!
By default Entity Framework assumes that an integer primary key is database generated. As the result Entity Framework would not include Primary Key field in the actual INSERT statement.
I would try to either play along and ALTER the table to auto-generate the ID (which judging by your comment you did)
or set StoreGeneratedPattern property of OnlineCarStore.Models.ShoppingCarts Id column to 'None'
or use annotation: [DatabaseGenerated(DatabaseGeneratedOption.None)].

Making one of my columns default the DateCreated to current time

I have the following SQL definition:
CREATE TABLE [dbo].[James] (
[JamesID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (255) NOT NULL,
[DateCreated] DATETIME NULL,
CONSTRAINT [PK_dbo.James] PRIMARY KEY CLUSTERED ([JamesID] ASC)
);
How might I make it so new entries have the DateCreated filled out automatically when I create new entries.
What about existing data that has not had that column filled out?
If you are starting from scratch and assuming this is SQL Server:
CREATE TABLE [dbo].[James] (
[JamesID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (255) NOT NULL,
[DateCreated] DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT [PK_dbo.James] PRIMARY KEY CLUSTERED ([JamesID] ASC)
);
If you want to update the table you can use this:
ALTER TABLE dbo.James
ADD CONSTRAINT DF_namehere DEFAULT CURRENT_TIMESTAMP FOR DateCreated;
However, any current NULL values will remain NULL with the ALTER TABLE solution. How you want to address this depends if you want to backfill information.

IDENTITY NOT NULL at Table Creation

Can anyone please tell me whether the instruction IDENTITY NOT NULL at a table creation is redundant or not? I mean, judging by the message
DEFAULT or NULL are not allowed as explicit identity values.
I would say that any column declared as IDENTITY is implicitly also declared as NOT NULL, but I would like to make sure. Can anyone please confirm?
Thank you very much.
SQL Server adds NOT NULL constraint to identity columns automatically eventhough he did not speficy it when creating a table
Consider the following table script
create table test(id int identity(1,1), name varchar(1000))
Now Generate the script of the table from Management Studio. It generates the script as
CREATE TABLE [dbo].[test](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](1000) NULL
) ON [PRIMARY]
Eventhough NOT NULL constraint is not specified in the table script by default it is added. The identity column will never be NULL. So NOT NULL constraint is added default
SQL Server (2008, and probably earlier versions as well) will not allow you to create an identity column on a NULL column. Try it:
CREATE TABLE Foo1
(
FooId int identity not null
,Data varchar(20) not null
)
works, where
CREATE TABLE Foo2
(
FooId int identity null
,Data varchar(20) not null
)
generates error message Could not create IDENTITY attribute on nullable column 'FooId', table 'Foo2'.