Default Constraint causing an error - sql

I have the following SQL to create a table, but the "DEFAULT" in the first CONSTRAINT is giving me an error: "A Default constraint can exist only at the column level in a CREATE or ALTER TABLE statement."
I've never used default before so I have done some looking into this with internet research, but nothing has helped me solve the error yet or even really explained it to me.
CREATE TABLE [RuleEngine].[NCCIImportHistory](
[NCCIImportHistoryID] [int] IDENTITY(1,1) NOT NULL,
[StartTime] [datetimeoffset](7) NOT NULL,
[EndTime] [datetimeoffset](7) NOT NULL,
[CreatedOn] [datetimeoffset](7) NOT NULL,
[CreatedBy_UserID] [int] NOT NULL,
CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT (getutcdate()) FOR [CreatedOn],
CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy] FOREIGN KEY([CreatedBy_UserID]) REFERENCES [Security].[User] ([UserID]),
CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED ([NCCIImportHistoryID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]) ON [PRIMARY]

You didn't specify what database you are using, but based on syntax I think it's MS SQL Server. If so you can add the constraint inline as part of the column definition instead, like this:
[CreatedOn] [datetimeoffset](7) NOT NULL CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT (getutcdate()),

Try this
CREATE TABLE [RuleEngine].[NCCIImportHistory]
(
[NCCIImportHistoryID] [INT] IDENTITY(1, 1)
NOT NULL ,
[StartTime] [DATETIMEOFFSET](7) NOT NULL ,
[EndTime] [DATETIMEOFFSET](7) NOT NULL ,
[CreatedOn] [DATETIMEOFFSET](7) NOT NULL CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT ( GETUTCDATE() ),
[CreatedBy_UserID] [INT] NOT NULL ,
CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy] FOREIGN KEY ( [CreatedBy_UserID] ) REFERENCES [Security].[User] ( [UserID] ) ,
CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED
( [NCCIImportHistoryID] ASC )
WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
)
ON [PRIMARY]
or first create table and then constraints like this
CREATE TABLE [RuleEngine].[NCCIImportHistory]
(
[NCCIImportHistoryID] [INT] IDENTITY(1, 1) NOT NULL ,
[StartTime] [DATETIMEOFFSET](7) NOT NULL ,
[EndTime] [DATETIMEOFFSET](7) NOT NULL ,
[CreatedOn] [DATETIMEOFFSET](7) NOT NULL,
[CreatedBy_UserID] [INT] NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT( GETUTCDATE() ) FOR [CreatedOn]
GO
ALTER TABLE [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy] FOREIGN KEY ( [CreatedBy_UserID] ) REFERENCES [Security].[User] ( [UserID] )
GO
ALTER TABLE [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED( [NCCIImportHistoryID] ASC ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
GO

Related

Trigger to update table after insert to insert or update into another table

I am having a little trouble with my after insert trigger it does not update/insert into the table and I am not sure why.
I have the tables employee, position, certification, employeecertification, positioncertification.
What I need to do is after a Position is assigned a Certification then all employees that are defined in that position (Employee:PositionId = Ins.PositionId] need to get the certification added (if it does not already exist in their record) , I might need to update them where they do exist (but for the moment do not believe that will be a requirement).
I have been getting a couple different results (neither is correct)
1: EmployeeId can not insert null into EmployeeId
2: On some other test data - nothing is inserted.
I have the following trigger with the sql for creation of tables that follows:
The Trigger
CREATE TRIGGER [dbo].[trig_InsNew_PositionCertification]
ON [dbo].[PositionCertification]
AFTER INSERT
AS
Insert into EmployeeCertification
(CertificationId, EmployeeId)
Select Ins.CertificationId, Emp.EmployeeId
From inserted Ins
Left Join dbo.Employee Emp On Emp.PositionId = Ins.PositionId
Left Join dbo.Certification Trw On Trw.CertificationId = Ins.CertificationId
WHERE NOT EXISTS
(SELECT EmployeeId, CertificationId
FROM
EmployeeCertification
WHERE EmployeeId = Emp.EmployeeId AND CertificationId = Ins.CertificationId);
GO
ALTER TABLE [dbo].[PositionCertification] ENABLE TRIGGER [trig_InsNew_PositionCertification]
GO
CREATE TABLES:
CREATE TABLE [dbo].[Position](
[PositionId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Position] PRIMARY KEY CLUSTERED
(
[PositionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeName] [nvarchar](512) NOT NULL,
[PositionId] [int] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employee_Position] FOREIGN KEY([PositionId])
REFERENCES [dbo].[Position] ([PositionId])
GO
ALTER TABLE [dbo].[Position] CHECK CONSTRAINT [FK_Employee_Position]
GO
CREATE TABLE [dbo].[Certification](
[CertificationId] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](512) NOT NULL,
CONSTRAINT [PK_Certification] PRIMARY KEY CLUSTERED
(
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[EmployeeCertification](
[EmployeeCertificationId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeId] [int] NOT NULL,
[CertificationId] [int] NOT NULL,
CONSTRAINT [PK_EmployeeCertification] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC,
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[EmployeeCertification] WITH CHECK ADD CONSTRAINT [FK_EmpCertification_Certification] FOREIGN KEY([CertificationId])
REFERENCES [dbo].[Certification] ([CertificationId])
GO
ALTER TABLE [dbo].[EmployeeCertification] CHECK CONSTRAINT [FK_EmpCertification_Certification]
GO
ALTER TABLE [dbo].[EmployeeCertification] WITH CHECK ADD CONSTRAINT [FK_EmpCertification_Employee] FOREIGN KEY([EmployeeId])
REFERENCES [dbo].[Employee] ([EmployeeId])
GO
ALTER TABLE [dbo].[EmployeeCertification] CHECK CONSTRAINT [FK_EmpCertification_Employee]
GO
CREATE TABLE [dbo].[PositionCertification](
[PositionCertificationId] [int] IDENTITY(1,1) NOT NULL,
[PositionId] [int] NOT NULL,
[CertificationId] [int] NOT NULL,
CONSTRAINT [PK_PositionCertification] PRIMARY KEY CLUSTERED
(
[PositionId] ASC,
[CertificationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

How to properly create table with 3 columns as primary key?

I have been working with SQL for 3 month, I would like to know create a table with 3 columns as primary key, Any help would be great thank you.
This code below is a Scrip to Create table generated my SQL Server Management Studio of the table
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[REP]
(
[id] [UNIQUEIDENTIFIER] ROWGUIDCOL NOT NULL,
[id_client] [NCHAR](30) NULL,
[id_representant] [UNIQUEIDENTIFIER] NULL,
[date_debut] [DATE] NULL,
[date_fin] [DATE] NULL,
CONSTRAINT [PK_REP]
PRIMARY KEY CLUSTERED ([id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[REP]
ADD CONSTRAINT [DF_REP_id] DEFAULT (NEWID()) FOR [id]
GO
First all the columns that are part of the primary key have to be not null.
then defining the key just add all columns as comma separated list.
CREATE TABLE [dbo].[REP](
[id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[id_client] [nchar](30) NOT NULL,
[id_representant] [uniqueidentifier] NULL,
[date_debut] [date] NULL,
[date_fin] [date] NULL,
CONSTRAINT [PK_REP] PRIMARY KEY CLUSTERED
( [id] ASC,id_client ASC )
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
In the code above I changed the column id_client to be not null and added it to the definition of the primary key ... ([id] ASC, id_client ASC).
To add more columns to a primary Key contraint you'd add the additional fields after the first field.
[id] ASC, nextfield ASC, nextfield2 asc
You can have a one composite key that comprise of more than one primary key.
CREATE TABLE xyz (
primary key (id,id_client,id_representer)
);

Foreign Keys for 4 tables in a GrandParent, Parent, Child, GrandChild so change the Parent Field cascades to GrandChild

I need to be able to change the Chain_ID in the parent table and have it cascade down to the assemblies table through use of foreign keys.
Currently there are foreign keys between the Stores and Machines which does work as when I change the Chain_ID in the Store table it does change the Chain_ID in the Machines table.
However the foreign key on the Machine and Assemblies tables does not update the Assemblies table Chain_ID.
Any help would be greatly appreciated
Database layout
USE [Test_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Chains]
(
**[Chain_ID]** [INT] IDENTITY(1001,1) NOT NULL,
[Chain_Name] [NVARCHAR](50) NOT NULL,
CONSTRAINT [PK__ChainID]
PRIMARY KEY CLUSTERED ([Chain_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Stores]
(
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Store_Name] [NVARCHAR](50) NULL,
CONSTRAINT [PK__ChainID_StoreID]
PRIMARY KEY CLUSTERED ([Chain_ID] ASC, [Store_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[Machines]
(
[Machine_ID] [INT] IDENTITY(10001,1) NOT NULL,
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Status] [NVARCHAR](10) NULL,
[Comments] [NVARCHAR](200) NULL,
CONSTRAINT [PK__MachineID]
PRIMARY KEY CLUSTERED ([Machine_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Machines] WITH CHECK
ADD CONSTRAINT [FK__Machines_Stores]
FOREIGN KEY ([Chain_ID], [Store_ID])
REFERENCES [dbo].[Stores] ([Chain_ID], [Store_ID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[Machines] CHECK CONSTRAINT [FK__Machines_Stores]
GO
CREATE TABLE [dbo].[Assemblies]
(
[Serial_Number] [NVARCHAR](11) NOT NULL,
[Machine_ID] [INT] NOT NULL,
**[Chain_ID]** [INT] NOT NULL,
**[Store_ID]** [INT] NOT NULL,
[Equipment_Type] [NVARCHAR](42) NULL,
[Status] [NVARCHAR](10) NULL,
CONSTRAINT [PK__Serial_Number]
PRIMARY KEY CLUSTERED ([Serial_Number] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Assemblies] WITH CHECK
ADD CONSTRAINT [FK__Assemblies_Machines]
FOREIGN KEY([Machine_ID])
REFERENCES [dbo].[Test_DB] ([Machine_ID])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[Assemblies] CHECK CONSTRAINT [FK__Assemblies_Machines]
GO

Foreign key from one column to another column contained in multiple column primary key

I try to create a foreign key between one column to two-column primary key and SQL Server refused that. Why ?
I have a table to store my wording in different languages and a country table.
ref.SystemLabels
--------------
--> [Id] int
| [IdLanguage] int
| Label nvarchar(200)
| Keywords nvarchar(200)
|
| ref.Countries
| --------------
| [Id]
--> IdSystemLabel
IsoCode
In my mind, it's very logic but I don't understand why SQL Server doesn't understand or accept my logic ^^.
If anyone can help me about this.
NOTE I think in another way, I will create an index on the IdSystemLabel despite of a foreign key.
EDIT: As you ask me, please see my SQL code for tables
Table SystemLanguages
CREATE TABLE [ref].[SystemLanguages](
[Id] [int] NOT NULL,
[IdSystemLabel] [int] NOT NULL,
[IsoCode] [nchar](2) NOT NULL,
[Enabled] [bit] NOT NULL,
CONSTRAINT [PK_SystemLanguages] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ref].[SystemLanguages] ADD CONSTRAINT [DF_SystemLanguages_Enabled] DEFAULT ((0)) FOR [Enabled]
GO
Table SystemLabels
CREATE TABLE [ref].[SystemLabels](
[Id] [int] NOT NULL,
[IdLanguage] [int] NOT NULL,
[Label] [nvarchar](max) NOT NULL,
[Keywords] [nvarchar](200) NULL,
[CreatedAt] [datetime] NOT NULL,
[UpdatedAt] [datetime] NULL,
[Group] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_SystemLabels] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[IdLanguage] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [ref].[SystemLabels] ADD CONSTRAINT [DF_SystemLabels_CreatedAt] DEFAULT (getdate()) FOR [CreatedAt]
GO
NOTE This table can't have foreign key to SystemLanguages due to circular dependency
Table LocalizationLevel0
CREATE TABLE [ref].[LocalizationLevel0](
[Id] [int] NOT NULL,
[IdSystemLabel] [int] NOT NULL,
[IsoCode] [nchar](2) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
[UpdatedAt] [datetime] NULL,
[Enabled] [bit] NULL,
CONSTRAINT [PK_LocalizationLevel0] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ref].[LocalizationLevel0] ADD CONSTRAINT [DF_LocalizationLevel0_CreatedAt] DEFAULT (getdate()) FOR [CreatedAt]
GO
ALTER TABLE [ref].[LocalizationLevel0] ADD CONSTRAINT [DF_LocalizationLevel0_Enabled] DEFAULT ((0)) FOR [Enabled]
GO
I'm trying to explain my logic :
One localizationlevel0 item can have a translation in multiple system languages :
Example for FRANCE country
1 4 'FR' '2015-04-17 00:00:00:000' NULL 1
-- System Languages
1 1 'FR' 1
2 2 'EN' 1
3 3 'DE' 1
-- Corresponding wordings
1 1 'Français' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'
1 2 'French' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'
1 2 'Französisch' NULL '2015-04-17 00:00:00:000' NULL 'SystemLanguages'
...
4 1 'France' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'
4 2 'France' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'
4 3 'Frankreich' NULL '2015-04-17 00:00:00:000' NULL 'LocalizationLevel0'
You have to always reference the whole primary key.
So, if the first table has a composite primary key (Id, IdLanguage), the second table has to have also a composite foreign key (SystemLabels.Id, SystemLabels.Language) and not just (SystemLabels.Id).
I would check out the question IX in this article:
https://www.simple-talk.com/sql/t-sql-programming/questions-about-primary-and-foreign-keys-you-were-too-shy-to-ask/

Problems understanding intermittent inconsistencies when loading data with SSIS package

The problem
During the passed few months the below described procedure has worked without any problems a vast majority of the times it has run (on 2008 r2). We have, however, three instances of incorrectly connected data. The question is, what is causing this and how do I remedy it?
DATA_PreImp
sourceid col01 col02 col03 col04 col...
100001 John Smith
100002 Calvin Klein
100003 Peter Parker
100004 Moe Greene
Usually the rendered result is that the attribute is connected to the Items_Main correctly but sometimes (less than 1%) the order is scrambled so that the value of col01 is not connected to the same Items_Main as the value of the rest of the columns.
Any insights as to what is causing this would be most appreciated.
The data moving procedure
We have an SSIS package that transfers data from a flat table called DATA_PreImp to a structure consisting of three related tables (attribute based).
Items_Main should contains one row for each row in DATA_PreImp
Items_Featurevalues contains one row for each column value of a row in DATA_PreImp
Items_MainRel contains the connection between Items_Main and Items_FeatureValues
The first step in the SSIS package inserts the data from DATA_PreImp to Items_Main and inserts the generated identifier into the TARGET_ID column in the empty DATA_PreImpMappingTMP table.
insert into items_main(creationdate, status)
output inserted.itemid into DATA_PreImpMappingTMP(TARGET_ID)
select getdate(), '0' from data_preimp
order by sourceid asc;
The second step in the SSIS package fill the Items_MainRel table with TARGET_ID (Itemid originally) and an identifier for the feature (in this case a 5).
insert into items_mainrel(itemid, featureid)
output inserted.itemrelid into DATA_PreImpMapping2TMP(INDREL_ID)
select TARGET_ID, 5 from DATA_PreImpMappingTMP
order by TARGET_ID asc;
The third step is to fill the SOURCE_ID column in the DATA_PreImpMapping2TMP table with the SOURCE_ID from DATA_PreImp.
with cte as (select sourceid, row_number() over (order by sourceid asc) as row from data_preimp)
update m set m.SOURCE_ID = s.sourceid, m.FEAT_ID = 5
from DATA_PreImpMapping2TMP as m
join cte as s on s.row = m.ROW;
The last step is to fill the Items_FeatureValues table with data from DATA_PreImpMapping2TMP and DATA_PreImp.
insert into items_featurevalues(itemrelid, languageid, fnvarchar)
select DATA_PreImpMapping2TMP.INDREL_ID, 0, data_preimp.col01
from DATA_PreImpMapping2TMP
join data_preimp on (DATA_PreImpMapping2TMP.SOURCE_ID = data_preimp.sourceid)
where FEAT_ID = 5
Data table structure
Here is what is needed to create the scenario:
CREATE TABLE [dbo].[DATA_PreImp](
[sourceid] [bigint] IDENTITY(1,1) NOT NULL,
[col01] [nvarchar](500) NULL,
[col02] [nvarchar](500) NULL,
[col03] [nvarchar](500) NULL,
[col04] [nvarchar](500) NULL,
[col05] [nvarchar](500) NULL,
[col06] [nvarchar](500) NULL,
[col07] [nvarchar](500) NULL,
[col08] [nvarchar](500) NULL,
[col09] [nvarchar](500) NULL,
[col10] [nvarchar](500) NULL,
CONSTRAINT [PK_DATA_PreImp] PRIMARY KEY CLUSTERED
(
[sourceid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[DATA_PreImpMappingTMP](
[ROW] [int] IDENTITY(1,1) NOT NULL,
[TARGET_ID] [int] NULL,
PRIMARY KEY CLUSTERED
(
[ROW] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Items_Main](
[Itemid] [int] IDENTITY(1,1) NOT NULL,
[creationDate] [smalldatetime] NOT NULL,
[status] [int] NOT NULL,
[purchdate] [smalldatetime] NULL,
[logindate] [smalldatetime] NULL,
CONSTRAINT [PK_Items_Main] PRIMARY KEY CLUSTERED
(
[Itemid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[DATA_PreImpMapping2TMP](
[ROW] [int] IDENTITY(1,1) NOT NULL,
[SOURCE_ID] [int] NULL,
[INDREL_ID] [int] NULL,
[FEAT_ID] [int] NULL,
PRIMARY KEY CLUSTERED
(
[ROW] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Items_Features](
[featureId] [int] IDENTITY(1,1) NOT NULL,
[featureRef] [varchar](15) NOT NULL,
[featureName] [varchar](50) NOT NULL,
[creationDate] [smalldatetime] NOT NULL,
[status] [int] NOT NULL,
[fieldType] [varchar](50) NOT NULL,
[featureType] [int] NOT NULL,
[featureDesc] [varchar](500) NULL,
CONSTRAINT [PK_Items_Features] PRIMARY KEY CLUSTERED
(
[featureId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
CREATE TABLE [dbo].[Items_MainRel](
[ItemRelId] [int] IDENTITY(1,1) NOT NULL,
[Itemid] [int] NOT NULL,
[featureId] [int] NOT NULL,
CONSTRAINT [PK_Items_MainRel] PRIMARY KEY CLUSTERED
(
[ItemRelId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Items_FeatureValues](
[valueId] [int] IDENTITY(1,1) NOT NULL,
[ItemRelId] [int] NOT NULL,
[languageId] [int] NOT NULL,
[FnVarChar] [nvarchar](250) NULL,
[FInt] [int] NULL,
[FImage] [int] NULL,
[FNText] [ntext] NULL,
[FSmallDateTime] [smalldatetime] NULL,
CONSTRAINT [PK_Items_FeatureValues] PRIMARY KEY CLUSTERED
(
[valueId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Items_MainRel] WITH CHECK ADD CONSTRAINT [FK_Items_MainRel_Items_Features] FOREIGN KEY([featureId])
REFERENCES [dbo].[Items_Features] ([featureId])
GO
ALTER TABLE [dbo].[Items_MainRel] CHECK CONSTRAINT [FK_Items_MainRel_Items_Features]
GO
ALTER TABLE [dbo].[Items_MainRel] WITH CHECK ADD CONSTRAINT [FK_Items_MainRel_Items_Main] FOREIGN KEY([Itemid])
REFERENCES [dbo].[Items_Main] ([Itemid])
GO
ALTER TABLE [dbo].[Items_MainRel] CHECK CONSTRAINT [FK_Items_MainRel_Items_Main]
GO
ALTER TABLE [dbo].[Items_FeatureValues] WITH CHECK ADD CONSTRAINT [FK_Items_FeatureValues_Items_MainRel] FOREIGN KEY([ItemRelId])
REFERENCES [dbo].[Items_MainRel] ([ItemRelId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Items_FeatureValues] CHECK CONSTRAINT [FK_Items_FeatureValues_Items_MainRel]
GO
INSERT INTO DATA_PreImp (col01,col02,col03,col04)
VALUES('John', 'Smith', '1964', 'NewYork'),
('Calvin', 'Klein', '1960', 'Washington D. C.'),
('Peter', 'Parker', '1974', 'Losangles'),
('Moe', 'Greene', '1928', 'Lasvegas')
INSERT INTO Items_Features (featureRef, featureName, creationDate, [status], fieldType, featureType, featureDesc)
VALUES ('firstname','First_Name', GETDATE(), 0, 'FnVarChar', 3, 'FirstName'),
('lastname','Last_Name', GETDATE(), 0, 'FnVarChar', 3, 'LastName'),
('Birth','Birth', GETDATE(), 0, 'FnVarChar', 3, 'Birth'),
('City','City', GETDATE(), 0, 'FnVarChar', 3, 'City')
The problem was the computed column CUST_CD. After a lot of researching, it seems that the BULK INSERT does not like complex computed types (see Using SQL Server spatial types in SSIS data load). The solution is to removed the computed column and just make it a varchar(20) NULL. Then I created a new Execute SQL Task that updates any NULL rows with the computed value.