I was wondering if anyone could help me with my SQL code below. I am trying to insert all of the records from my BC_completions table into my Geodata table. If I exclude the ID column on the insert, I get a 'Msg 515... Cannot insert the value NULL into column 'ID'. But if I include it as below I get a 'Msg ...Violation of PRIMARY KEY constraint'.
What makes it more confusing is that if I manually typed in the value I get from @PK the database accepts it, so any help here would be great.
Thanks
DECLARE @PK AS INT
DECLARE @COUNT AS INT
DECLARE @RECORDCOUNT AS INT
SET @PK =
(SELECT TOP 1 ID FROM PRCORE.DBO.GEODATA
ORDER BY ID DESC)
SET @RECORDCOUNT =
(SELECT COUNT(*) FROM BC_COMPLETIONS)
SET @COUNT = 0
WHILE @COUNT < @RECORDCOUNT
BEGIN
SET @PK = @PK+1
SET @COUNT = @COUNT+1
INSERT INTO PRCORE.DBO.GEODATA
(ID,RecordType,ReferenceName,LocationDescription,Description,ORN,StartDate,ChgText)
SELECT @PK,189,REFVAL,ADDRESS,DSCRPN,ORN,RECPTD,AGTNAME
FROM BC_COMPLETIONS B
where @PK not in (select ID from prcore.dbo.geodata)
END