ouroboros12321
Technical User
- Jan 13, 2011
- 6
Hello,
I've recently began learning SQL with SQL Server and it's been a blast so far. Right now, I am having a problem figuring out how to insert certain data into a destination table (not created by me, cannot alter it) that has a PK field which is a VARCHAR type and it doesn't appear to be an auto-id column.
Every time that I run the script below, I get the error:
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'PK_AR4CONTACTS'. Cannot insert duplicate key in object 'dbo.AR4CONTACTS'.
Here is my SQL Server script:
--------------------
use ASM_Analysis_Db
go
drop table #temp
go
drop table #u
go
create table #temp
(
Pat_Number varchar(12),
CTYPE VARCHAR(12),
CNOTES VARCHAR(650),
)
GO
bulk insert #temp
from '\\pcnamed\TS_Call_Output.txt'
with
(
fieldterminator = ',',
rowterminator = '\n'
--ERRORFILE = '\\pcnamed\\ERRORFILE.TXT'
)
go
/***? I created the additional # table below because I thought having an IDENTITY field would allow me to insert it into th e final destination table. Unfortunatly the script does not accept this.
***/?
create table #U
(idField int identity(1,1),
Pat_Number varchar(12),
CTYPE VARCHAR(12),
CNOTES VARCHAR(650)
)
GO
insert into #U
(Pat_Number , CTYPE , CNOTES )
select T.Pat_Number, T.CTYPE, T.CNOTES
from #temp as T
insert into [Ar-Express-Test].dbo.AR4CONTACTS
( REFER_CODE, CTYPE, CNOTES, CADDUSERID, DADDDATE)
select U.Pat_Number, U.CTYPE, U.CNOTES, 'ADMIN', GETDATE()
FROM #U as U INNER JOIN [Ar-Express-Test].DBO.AR4CONTACTS AS C
ON U.Pat_Number = C.REFER_CODE
;
--End of script
--------------------------
I have searched all over and cannot find an answer. How do I insert my data to this final table [Ar-Express-Test].dbo.AR4CONTACTS and handle the primary key issue???
I would appreciate any help from the knowledgeable users from this forum.
Thank you.
I've recently began learning SQL with SQL Server and it's been a blast so far. Right now, I am having a problem figuring out how to insert certain data into a destination table (not created by me, cannot alter it) that has a PK field which is a VARCHAR type and it doesn't appear to be an auto-id column.
Every time that I run the script below, I get the error:
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'PK_AR4CONTACTS'. Cannot insert duplicate key in object 'dbo.AR4CONTACTS'.
Here is my SQL Server script:
--------------------
use ASM_Analysis_Db
go
drop table #temp
go
drop table #u
go
create table #temp
(
Pat_Number varchar(12),
CTYPE VARCHAR(12),
CNOTES VARCHAR(650),
)
GO
bulk insert #temp
from '\\pcnamed\TS_Call_Output.txt'
with
(
fieldterminator = ',',
rowterminator = '\n'
--ERRORFILE = '\\pcnamed\\ERRORFILE.TXT'
)
go
/***? I created the additional # table below because I thought having an IDENTITY field would allow me to insert it into th e final destination table. Unfortunatly the script does not accept this.
***/?
create table #U
(idField int identity(1,1),
Pat_Number varchar(12),
CTYPE VARCHAR(12),
CNOTES VARCHAR(650)
)
GO
insert into #U
(Pat_Number , CTYPE , CNOTES )
select T.Pat_Number, T.CTYPE, T.CNOTES
from #temp as T
insert into [Ar-Express-Test].dbo.AR4CONTACTS
( REFER_CODE, CTYPE, CNOTES, CADDUSERID, DADDDATE)
select U.Pat_Number, U.CTYPE, U.CNOTES, 'ADMIN', GETDATE()
FROM #U as U INNER JOIN [Ar-Express-Test].DBO.AR4CONTACTS AS C
ON U.Pat_Number = C.REFER_CODE
;
--End of script
--------------------------
I have searched all over and cannot find an answer. How do I insert my data to this final table [Ar-Express-Test].dbo.AR4CONTACTS and handle the primary key issue???
I would appreciate any help from the knowledgeable users from this forum.
Thank you.