Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3184

SQL Insertion Question

$
0
0

Given that Max(CODE_ID) = 1006 and CODE_ID is the primary key and that....

 

begin

declare varCodeID integer;

select Max(CODE_ID) + 1 into varCodeID from CODE;

-- Insert Parent Code

insert into CODE(CODE_ID,PARENT_CODE_ID,CODE_NAME)

select varCodeID,1,'Example Parent';

-- snipped insertion of children

end

 

works as expected, why does the following fail saying that 1007 is a duplicate primary key?

 

begin

declare varCodeID integer;

select Max(CODE_ID) + 1 into varCodeID from CODE;

-- Insert Parent Code

insert into CODE(CODE_ID,PARENT_CODE_ID,CODE_NAME)

select varCodeID,1,'Example Parent' from CODE;

-- snipped insertion of children

end

 

Hoping the answer isn't too embarrassing, Paul


Viewing all articles
Browse latest Browse all 3184

Trending Articles