DB2 UDB V7 (Inserting into Identity Columns)
Â
This tip provided by Experts Exchange.
Â
Question:
I am working with DB2 UDB v7 and need to insert into a tablewith an identity column.
Example:
SourceTable:
Student    CourseGrade
——- Â Â Â ———–
Fred       A
Wilma      B
TargetTable Desired result: Â
(SurrogateKey is defined as
Int Generated Always As Identity
(Start with 1, Increment by 1, No Cache)
SurrogateKey     Student    CourseGrade
———— Â Â Â Â ——- Â Â Â ———–
1               Fred      A
2               Wilma     B
I want to write a statement like:
Insert into TargetTable
Select
  Student,
  CourseGrade
From SourceTable
This produces an error “The number of values assigned is not the same asthe number of specified or
implied columns”
Is there a way to specify that the first column is inserted into an identitycolumn?
Â
Â
Accepted Answer:
To create records using the identity column you shouldspecify only the fields that need populating.
insert into targettable (student,coursegrade) select student,coursegrade fromsourcetable;
or
insert into targettable (student,coursegrade) select * from sourcetable;
If updating with values use
insert into targettable (student,coursegrade) values (‘Fred’,’A’);
Â
Written on 2/06/2001
Â
Â
Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.
Related Posts
- Embarcadero Unveils RAD Solution Pack, the Complete VCL and FMX Component Solution for RAD Studio, Delphi and C++Builder
- Shin Megami Tensei V: Vengeance trailer released
- RIAA files lawsuits against Udio and Suno
- Life in The Digital World: Is It Impossible To Be Private Online?
- Diablo 4 Spiritborn class arrives October 2024























