view the table definition using the SQLSERVER management studio - sql-server

What does the output of "Script Table As -> CREATE To" mean ? I have a partitioned table, but when I right click on it in the Management Studio and choose "Script Table As -> CREATE To", the output didn't contain anything related to table partition. I was expected to find the "ON partition_scheme_name", but it didn't show up.
USE [FcstDB]
GO
/****** Object: Table [dbo].[StFcst] Script Date: 2014-06-10 15:54:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StFcst](
[MID] [tinyint] NOT NULL,
[INITDATE] [smalldatetime] NOT NULL,
[FH] [tinyint] NOT NULL,
[STID] [char](5) NOT NULL,
[WW03] [smallint] NOT NULL,
[WW06] [smallint] NOT NULL,
[WW12] [smallint] NOT NULL,
[T] [smallint] NOT NULL,
[TMAX12] [smallint] NOT NULL,
[TMIN12] [smallint] NOT NULL,
[PR03] [smallint] NOT NULL,
[PR06] [smallint] NOT NULL,
[PR12] [smallint] NOT NULL,
[PR24] [smallint] NOT NULL,
[RH] [smallint] NOT NULL,
[WD] [smallint] NOT NULL,
[WS] [smallint] NOT NULL,
[CLOUD] [smallint] NOT NULL,
[VV] [int] NOT NULL,
[SLP] [smallint] NOT NULL,
[E_T] [smallint] NOT NULL,
[E_TMAX12] [smallint] NOT NULL,
[E_TMIN12] [smallint] NOT NULL,
[E_PR03] [smallint] NOT NULL,
[E_PR06] [smallint] NOT NULL,
[E_PR12] [smallint] NOT NULL,
[E_PR24] [smallint] NOT NULL,
[E_WD] [smallint] NOT NULL,
[E_WS] [smallint] NOT NULL,
CONSTRAINT [PK_StFcst] PRIMARY KEY CLUSTERED
(
[INITDATE] DESC,
[FH] ASC,
[MID] ASC,
[STID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO

Script Table As -> CREATE To creates script containing Create command of given table structure without any kind of data.
Do you intend to copy the data somewhere else? If yes, consider using SELECT INTO command.
For Create Partition scheme please visit msdn.
Script table documentation here.

Related

SQL Server DELETE query performance

I've got a simple table and I'm evaluating which is the best way to delete some records from it.
The DELETE query is so simple: DELETE FROM [dbo].[Badge] WHERE [Id_Operatore] = #SOMEVALUE
Is there a way to increase performance of this operation?
This is the table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Badge](
[Id_Badge] [int] IDENTITY(1,1) NOT NULL,
[Id_Operatore] [int] NULL,
[Badge0] [smallint] NULL,
[Badge1] [smallint] NULL,
[Badge2] [smallint] NULL,
[Badge3] [smallint] NULL,
[Badge4] [smallint] NULL,
[Badge5] [smallint] NULL,
[Badge6] [smallint] NULL,
[Badge7] [smallint] NULL,
[Badge8] [smallint] NULL,
[Badge9] [smallint] NULL,
[Badge10] [smallint] NULL,
[Badge11] [smallint] NULL,
[Badge12] [smallint] NULL,
[Badge13] [smallint] NULL,
[Badge14] [smallint] NULL,
[Badge15] [smallint] NULL,
[Badge16] [smallint] NULL,
[Badge17] [smallint] NULL,
[Badge18] [smallint] NULL,
[Badge19] [smallint] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Badge] WITH CHECK ADD CONSTRAINT [Badge_FK00] FOREIGN KEY([Id_Operatore])
REFERENCES [dbo].[Operatori] ([ID_Operatore])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Badge] CHECK CONSTRAINT [Badge_FK00]
GO
You should add an index on the foreign key.
The index might be covering, so including all these badges is not a bad idea.
Create a clustered (preferred) or nonclustered index on [Id_Operatore].
"Clustered" keeps the data physical by each other.
This reduces the IO on delete.

Partition Table Update Slow

I have an issue with one of my MSSQL tables. The table has 1.2 Trillion rows about 1TB of data and growing. The table is partitioned into 8000 partitions, only about 800 are used. The others were created for expanded growth.
Inserts and selects are < 1s
My problem is Updates are very slow. To updated 1 record varchar(100) using the exact partition key and Identity Column key, it is 3s.
If I place the update code in a Stored Procedure, goes in less than 1s. If I add option (recompile), about 1s. Is there a way to fix this without adding recomile?
Thank you.
CREATE TABLE [dbo].[_tabletest](
[data_id] [bigint] IDENTITY(10000,1) NOT NULL,
[idx1_id] [smallint] NOT NULL,
[idx2_id] [bigint] NOT NULL,
[idx3_id] [bigint] NOT NULL,
[template_id] [bigint] NOT NULL,
[reference_id] [varchar](200) NOT NULL,
[data] [nvarchar](400) NULL,
[data_1] [tinyint] NULL,
[data_2] [varchar](max) NULL,
[data_3] [real] NULL,
[data_4] [tinyint] NULL,
[data_5] [tinyint] NULL,
[data_6] [bit] NULL,
[data_7] [nvarchar](50) NULL,
[data_8] [datetime] NULL,
[data_9] [nvarchar](50) NULL,
[data_10] [varchar](100) NULL,
[data_11] [varchar](100) NULL,
[data_12] [varchar](100) NULL,
[data_13] [varchar](300) NULL,
[data_14] [varchar](200) NULL,
[data_15] [uniqueidentifier] NULL,
[data_16] [varchar](600) NULL,
[data_17] [varchar](100) NULL,
[data_18] [varchar](100) NULL,
[data_19] [decimal](10, 5) NULL,
[data_20] [decimal](10, 5) NULL,
[data_21] [decimal](10, 5) NULL,
[data_22] [decimal](10, 5) NULL,
[data_23] [decimal](10, 5) NULL,
[data_24] [int] NULL,
[data_25] [int] NULL,
[data_26] [int] NULL,
[data_27] [int] NULL,
[data_28] [decimal](10, 5) NULL,
[data_29] [decimal](10, 5) NULL,
[data_30] [decimal](10, 5) NULL,
[data_31] [datetime] NULL,
[data_32] [decimal](10, 5) NULL,
[data_3] [bit] NULL,
[data_34] [varchar](max) NULL,
[data_35] [smallint] NULL,
[data_36] [bigint] NULL,
[data_37] [int] NULL,
[data_38] [real] NULL,
[data_39] [datetime] NULL,
[data_40] [varchar](2500) NULL,
CONSTRAINT [PK_data_id] PRIMARY KEY CLUSTERED
(
[idx1_id] ASC,
[idx2_id] ASC,
[idx3_id] ASC,
[data_id] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)
)
GO
ALTER TABLE [dbo].[_tabletest] SET (LOCK_ESCALATION = AUTO)
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_1] DEFAULT ((0)) FOR [data_1]
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_2] DEFAULT (getutcdate()) FOR [data_39]
GO
ALTER TABLE [dbo].[_tabletest] ADD DEFAULT (newid()) FOR [data_15]
GO
UPDATE [_tabletest] SET [data_40] = 'test-data'
WHERE [idx1_id] = 1209 AND [idx2_id] = 113795 AND [idx3] = 41195716 AND [data_id] = 1329110156
;

FK conflict on a table that the data definitely exists in

UPDATE MysterySpells SET SpellId = (SELECT Id FROM Spells
WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2
I know the subquery returns 589. Anyone have any clues why I would be getting this error:
The UPDATE statement conflicted with the FOREIGN KEY constraint
"FK_MysterySpells_Spells". The conflict occurred in database "x",
table "dbo.Spells", column 'Id'.
CODE UPDATE
CREATE TABLE [dbo].[Spells](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OldId] [int] NULL,
[Name] [nvarchar](100) NOT NULL,
[School] [nvarchar](50) NULL,
[SubSchool] [nvarchar](50) NULL,
[SchoolID] [int] NOT NULL,
[SubSchoolID] [int] NULL,
[CastingTime] [nvarchar](250) NULL,
[Components] [nvarchar](250) NULL,
[IsCostly] [bit] NOT NULL,
[Cost] [int] NULL,
[RangeDescription] [nvarchar](250) NULL,
[Range] [int] NOT NULL,
[RangeIncrement] [int] NOT NULL,
[RangeGap] [int] NOT NULL,
[Area] [int] NOT NULL,
[AreaIncrement] [int] NOT NULL,
[AreaGap] [int] NOT NULL,
[AreaNote] [nvarchar](250) NULL,
[Description] [nvarchar](max) NULL,
[ShortDescription] [nvarchar](250) NULL,
[Targets] [nvarchar](250) NULL,
[Effect] [nvarchar](250) NULL,
[Duration] [nvarchar](250) NULL,
[SavingThrow] [nvarchar](250) NULL,
[SpellResistence] [nvarchar](250) NULL,
[HasVerbal] [bit] NOT NULL,
[HasSomatic] [bit] NOT NULL,
[HasMaterial] [bit] NOT NULL,
[HasFocus] [bit] NOT NULL,
[HasDivineFocus] [bit] NOT NULL,
[WizardLevel] [int] NULL,
[ClericLevel] [int] NULL,
[AdeptLevel] [int] NULL,
[AlchemistLevel] [int] NULL,
[AntipaladinLevel] [int] NULL,
[BardLevel] [int] NULL,
[BloodragerLevel] [int] NULL,
[DruidLevel] [int] NULL,
[InquisitorLevel] [int] NULL,
[MagusLevel] [int] NULL,
[OracleLevel] [int] NULL,
[PaladinLevel] [int] NULL,
[RangerLevel] [int] NULL,
[ShamanLevel] [int] NULL,
[SorcererLevel] [int] NULL,
[SummonerLevel] [int] NULL,
[WitchLevel] [int] NULL,
[SLALevel] [int] NULL,
[IsDismissible] [bit] NOT NULL,
[IsLanguageDependent] [bit] NOT NULL,
[IsShapeable] [bit] NOT NULL,
[IsAcid] [bit] NOT NULL,
[IsAir] [bit] NOT NULL,
[IsCold] [bit] NOT NULL,
[IsCurse] [bit] NOT NULL,
[IsDarkness] [bit] NOT NULL,
[IsDeath] [bit] NOT NULL,
[IsDisease] [bit] NOT NULL,
[IsEarth] [bit] NOT NULL,
[IsElectricity] [bit] NOT NULL,
[IsEmotion] [bit] NOT NULL,
[IsFear] [bit] NOT NULL,
[IsFire] [bit] NOT NULL,
[IsForce] [bit] NOT NULL,
[IsLight] [bit] NOT NULL,
[IsPain] [bit] NOT NULL,
[IsPoison] [bit] NOT NULL,
[IsShadow] [bit] NOT NULL,
[IsSonic] [bit] NOT NULL,
[IsWater] [bit] NOT NULL,
[IsChaotic] [bit] NOT NULL,
[IsEvil] [bit] NOT NULL,
[IsGood] [bit] NOT NULL,
[IsLawful] [bit] NOT NULL,
[IsMindAffecting] [bit] NOT NULL,
[IsMythic] [bit] NULL,
[MythicDescription] [nvarchar](max) NULL,
[Augmented] [nvarchar](max) NULL,
CONSTRAINT [PK_Spells] 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] TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[MysterySpells](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MysteryId] [int] NOT NULL,
[ClassLevel] [int] NOT NULL,
[SpellId] [int] NOT NULL,
[SpellLevel] [int] NULL,
[Restrictions] [varchar](100) NULL,
CONSTRAINT [PK_MysterySpells] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[MysterySpells] WITH NOCHECK ADD CONSTRAINT [FK_MysterySpells_Mysteries] FOREIGN KEY([MysteryId])
REFERENCES [dbo].[Mysteries] ([Id])
GO
ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Mysteries]
GO
ALTER TABLE [dbo].[MysterySpells] WITH NOCHECK ADD CONSTRAINT [FK_MysterySpells_Spells] FOREIGN KEY([SpellId])
REFERENCES [dbo].[Spells] ([Id])
GO
ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Spells]
GO
If I were you, here is how I debug
If you expect
SELECT Id FROM Spells WHERE Name = 'unseen servant'
to return 589
I will hard code it to 1 first.
UPDATE MysterySpells SET SpellId = 589
WHERE MysteryId = 1 And ClassLevel = 2
If the error still there, I will check if table Spells contains value where Id = 589 and Name = 'unseen servant'.
I believe you will find the root cause somewhere in the process.
Note: It would be better if you show us the table structure and data inside.
The issue is that it does not know what value to grab. There is no mapping defined between spells and mystery spells in your query. The current update is actually trying to update each row in MysterySpells with 589 records.
If you simply wanted to map all of your mystery spells to a single spell you can do:
UPDATE MysterySpells SET SpellId = (SELECT TOP 1 Id FROM Spells
WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2
If there is a pre-existing mapping between these 2 tables and you are expecting to update MysterySpells based on a relationship with spells table:
UPDATE MysterySpells
SET SpellID = t2.ID
FROM MysterySpells t1
JOIN Spells t2 ON t2.relation = t1.relation
If there isn't any existing mapping and you are trying to do this to create this as the mapping you have few options.
1) If no other relationship exists map by hand
2) If their is a relationship that can be calculated, but is not available through an existing join, you can take advantage of the row and partition functions to relate the two. Then use an update similar to the 2nd code block I posted.

SQL Query Using VB Fields Does Not Return Results

I have been trying for several hours to figure out my correct syntax for a MSSQL query using fields. If I use the first line of code, I get no results.
Dim selectStatement As String = "SELECT * from tires.dbo.sales WHERE date BETWEEN '" & Reports.CalStartDate.SelectionStart & "'" & " and " & Reports.CalEndDate.SelectionEnd & "'"""
(I have two text boxes that show the contents of Reports.CalStartDate.SelectionStart and Reports.CalStartDate.SelectionEnd and they show the correct characters, exactly the same as the query that works.
If I use this (hardcoded) line, I get the expected results.
Dim selectStatement As String = "SELECT * from tires.dbo.sales where date between '03/21/2015' and'03/23/2015'"
Here is my mssql create script:
USE [tires]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[sales](
[date] [date] NULL,
[cust_id] [int] NULL,
[tire_id] [int] NULL,
[qty] [int] NULL,
[cost_ea] [decimal](5, 2) NULL,
[retail_ea] [decimal](5, 2) NULL,
[plate] [nchar](10) NULL,
[mileage] [nchar](10) NULL,
[service] [nchar](30) NULL,
[serv_price] [nchar](10) NULL,
[total] [nchar](10) NULL,
[notes] [nvarchar](150) NULL,
[custphone] [nchar](10) NULL,
[service2] [nchar](30) NULL,
[serv2_price] [nchar](10) NULL,
[inv_no] [int] IDENTITY(1,1) NOT NULL,
[tire_id2] [int] NULL,
[qty2] [int] NULL,
[cost_ea2] [decimal](5, 2) NULL,
[retail_ea2] [decimal](5, 2) NULL,
[tire_id3] [int] NULL,
[qty3] [int] NULL,
[cost_ea3] [decimal](5, 2) NULL,
[retail_ea3] [decimal](5, 2) NULL,
[tire1w] [nvarchar](50) NULL,
[tire2w] [nvarchar](50) NULL,
[tire3w] [nvarchar](50) NULL,
[tax] [nchar](10) NULL,
[fet] [nchar](10) NULL,
[labor] [nchar](10) NULL,
CONSTRAINT [PK_sales] PRIMARY KEY CLUSTERED
(
[inv_no] 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

Violation of Primary Key error on Identity column

This is maddening! Code in question has been running for over 5 years.
Here's the scoop....
I am doing an INSERT...SELECT into a table with a primary key that is an identity column. I do not specify the key when I insert - SQL Server generates it as expected.
I am doing the insert in a stored procedure that I call in a loop (for loop in SSIS, actually). The stored procedure will insert rows in batches (configurable). It might insert 1000 rows at a time or it might insert 50,000 - doesn't matter. It will work for a random number of calls (inserting thousands of rows) and then it will fail, out of the blue, with a
Violation of primary key / duplicate
error. If I check the identity seed - it is correct. If I kick off the process again it will work fine, for a while.
The values being inserted are coming from 2 tables that I join together, as if that matters.
The bulk of my code is below:
WHILE #pk <= #max_pk
BEGIN
INSERT INTO tbl_claim_line (fk_batch_control_group, fk_claim, fk_provider, service_from_date, service_to_date, allowed, net_paid, COB, flex_1, flex_2, flex_3, flex_4)
SELECT
#fk_batch_control_group
, c.pk_claim
, p.pk_provider
, i.date_of_service_from
, i.date_of_service_to
, i.allowed_amount
, i.net_paid_amount
, i.cob_amount
, i.claimline_flex_1
, i.claimline_flex_2
, i.claimline_flex_3
, i.claimline_flex_4
FROM
tbl_import i
INNER JOIN
tbl_import__claim c ON i.claim_number = c.claim_number
LEFT JOIN
tbl_import__provider p ON ISNULL(i.provider_type,'') = ISNULL(p.provider_type,'')
AND ISNULL(i.provider_specialty,'') = ISNULL(p.provider_specialty,'')
AND ISNULL(i.provider_zip_code,'') = ISNULL(p.provider_zip_code,'')
WHERE
pk_import = #pk
UPDATE tbl_import
SET fk_claim_line = SCOPE_IDENTITY()
WHERE pk_import = #pk
SET #pk += 1
END
--TABLE DEFINITIONS...
CREATE TABLE [dbo].[tbl_claim_line](
[fk_batch_control_group] [int] NOT NULL,
[fk_claim] [int] NOT NULL,
[fk_provider] [int] NULL,
[service_from_date] [date] NULL,
[service_to_date] [date] NULL,
[allowed] [money] NULL,
[net_paid] [money] NULL,
[COB] [money] NULL,
[flex_1] [varchar](200) NULL,
[flex_2] [varchar](200) NULL,
[flex_3] [varchar](200) NULL,
[flex_4] [varchar](200) NULL,
[pk_claim_line] [int] IDENTITY(1,1) NOT NULL,
[insert_date] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_claim_line] PRIMARY KEY NONCLUSTERED
(
[pk_claim_line] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
FOREIGN KEY([fk_batch_control_group])
REFERENCES [dbo].[tbl_batch_control_group] ([pk_batch_control_group])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_claim]
FOREIGN KEY([fk_claim])
REFERENCES [dbo].[tbl_claim] ([pk_claim])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_claim]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_provider]
FOREIGN KEY([fk_provider])
REFERENCES [dbo].[tbl_provider] ([pk_provider])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_provider]
GO
ALTER TABLE [dbo].[tbl_claim_line] ADD CONSTRAINT [DF_tbl_claim_line__insert_date] DEFAULT (getdate()) FOR [insert_date]
GO
----second table
CREATE TABLE [dbo].[tbl_import](
[fk_claim_line] [int] NULL,
[member_id] [varchar](50) NULL,
[member_card_id] [varchar](50) NULL,
[member_first_name] [varchar](50) NULL,
[member_last_name] [varchar](50) NULL,
[member_dob] [varchar](50) NULL,
[member_gender] [varchar](50) NULL,
[member_subscriber_relationship_code] [varchar](50) NULL,
[member_address_line_1] [varchar](100) NULL,
[member_address_line_2] [varchar](100) NULL,
[member_city] [varchar](50) NULL,
[member_state] [varchar](50) NULL,
[member_zip] [varchar](50) NULL,
[member_phone] [varchar](50) NULL,
[member_email] [varchar](50) NULL,
[subscriber_id] [varchar](50) NULL,
[group_line_of_business] [varchar](50) NULL,
[group_product] [varchar](50) NULL,
[group_employer] [varchar](50) NULL,
[provider_first_name] [varchar](50) NULL,
[provider_last_or_full_name] [varchar](200) NULL,
[provider_type] [varchar](200) NULL,
[provider_specialty] [varchar](400) NULL,
[provider_zip_code] [varchar](50) NULL,
[provider_tax_id] [varchar](50) NULL,
[medical_code_1] [varchar](10) NULL,
[medical_code_1_description] [varchar](500) NULL,
[medical_code_2] [varchar](10) NULL,
[medical_code_2_description] [varchar](500) NULL,
[medical_code_3] [varchar](10) NULL,
[medical_code_3_description] [varchar](500) NULL,
[medical_code_4] [varchar](10) NULL,
[medical_code_4_description] [varchar](500) NULL,
[medical_code_5] [varchar](10) NULL,
[medical_code_5_description] [varchar](500) NULL,
[medical_code_6] [varchar](10) NULL,
[medical_code_6_description] [varchar](500) NULL,
[medical_code_7] [varchar](10) NULL,
[medical_code_7_description] [varchar](500) NULL,
[medical_code_8] [varchar](10) NULL,
[medical_code_8_description] [varchar](500) NULL,
[medical_code_9] [varchar](10) NULL,
[medical_code_9_description] [varchar](500) NULL,
[medical_code_10] [varchar](10) NULL,
[medical_code_10_description] [varchar](500) NULL,
[medical_code_11] [varchar](10) NULL,
[medical_code_11_description] [varchar](500) NULL,
[medical_code_12] [varchar](10) NULL,
[medical_code_12_description] [varchar](500) NULL,
[medical_code_13] [varchar](10) NULL,
[medical_code_13_description] [varchar](500) NULL,
[medical_code_14] [varchar](10) NULL,
[medical_code_14_description] [varchar](500) NULL,
[medical_code_15] [varchar](10) NULL,
[medical_code_15_description] [varchar](500) NULL,
[medical_code_16] [varchar](10) NULL,
[medical_code_16_description] [varchar](500) NULL,
[date_of_service_from] [varchar](50) NULL,
[date_of_service_to] [varchar](50) NULL,
[claim_number] [varchar](50) NULL,
[claim_line_number] [varchar](50) NULL,
[original_claim_number] [varchar](50) NULL,
[allowed_amount] [varchar](50) NULL,
[net_paid_amount] [varchar](50) NULL,
[cob_amount] [varchar](50) NULL,
[date_paid] [varchar](50) NULL,
[member_flex_1] [varchar](200) NULL,
[member_flex_2] [varchar](200) NULL,
[member_flex_3] [varchar](200) NULL,
[member_flex_4] [varchar](200) NULL,
[claim_flex_1] [varchar](200) NULL,
[claim_flex_2] [varchar](200) NULL,
[claim_flex_3] [varchar](200) NULL,
[claim_flex_4] [varchar](200) NULL,
[claimline_flex_1] [varchar](200) NULL,
[claimline_flex_2] [varchar](200) NULL,
[claimline_flex_3] [varchar](200) NULL,
[claimline_flex_4] [varchar](200) NULL,
[pk_import] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_tbl_import] PRIMARY KEY NONCLUSTERED
(
[pk_import] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I ran into this and much like user3170349, it was a seed issue on the column. I'm adding some additional info, however.
First, you can run this to figure out if you have a seed problem:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', NORESEED);
This will give you information which will read something like this:
Checking identity information: current identity value 'XXXX', current column value 'YYYY'.
If YYYY is larger than XXXX, then you have a problem and need to RESEED the table to get things going again. You can do so with the following command:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', RESEED, ZZZZZ);
Where ZZZZ is the reseed value. That value should be at least one higher than YYYY. YMMV, so pick a value that is appropriate for your situation.
"Code in question has been running for over 5 years."
"It might insert 1000 records at a time or it might insert 50,000 "
Is it possible you have finally overflowed the integer type of the primary key?
Did it wrap around and is now starting over? That would cause duplicate primary keys.

Resources