Environment: SQL server 2005 and Win 7
Tools: MS. SQL Management Studio 2008 R2
Problem: There are several DB objects in the database: @Table_source, @Table_people, @Table_ssn, and stored procedure called proc_ID_generator designed to generate id. How would you insert multiple records while capturing generated ID from the stored procedure
for each row. Each row with generated id is inserted to @Table_people and @Table_ssn. I know how to do so record by record but having thousands of records it will take a lot of time and I wonder how to automate this process
Code:
DECLARE @Table_source TABLE ( SSN CHAR(9) ,S NCHAR(2) ,C NCHAR(2) ,L NCHAR(2) ,first_nm VARCHAR(10) ,last_nm VARCHAR(20) ,ImportDate DATE ) INSERT @Table_source VALUES ( '123456789' ,'A' ,'V' ,'B' ,'Dana' ,'Kobo' ,'1/1/2005' ) INSERT @Table_source VALUES ( '123456789' ,'N' ,'V' ,'B' ,'Dalia' ,'Budd' ,'1/1/2007' ) INSERT @Table_source VALUES ( '987654321' ,'A' ,'A' ,'B' ,'George' ,'Jackson' ,'1/1/2009' ) INSERT @Table_source VALUES ( '987654321' ,'F' ,'V' ,'B' ,'Jose' ,'Slattery' ,'1/1/2005' ) INSERT @Table_source VALUES ( '112233350' ,'Z' ,'Z' ,'Z' ,'Mike' ,'Smith' ,'1/1/2006' ) INSERT @Table_source VALUES ( '097754378' ,'A' ,'V' ,'B' ,'Ray' ,'elvicio' ,'1/1/2008' ) INSERT @Table_source VALUES ( '008610321' ,'A' ,'A' ,'B' ,'Samanta' ,'Cohen' ,'1/1/2008' ) -- The goal is to transefer some data from @Table_source into @Table_people and @Table_ssn DECLARE @Table_people TABLE ( id CHAR(9 SSN CHAR(9) ,S NCHAR(2) ,C NCHAR(2) ,L NCHAR(2) ,first_nm VARCHAR(10) ,last_nm VARCHAR(20) ) INSERT @Table_people VALUES ( '987654321' ,'A' ,'A' ,'Z' ,'Pamella' ,'Slattery' ) INSERT @Table_people VALUES ( '001456789' ,'N' ,'Z' ,'Z' ,'Jim' ,'Kramer' ) DECLARE @Table_ssn TABLE ( SSN CHAR(9) ID CHAR(9) ) ----- not feasible if 1000s records needed to insert
--how can i write an insert query that loops through all records in the @table_source declare @new_pn_id char(9) EXEC proc_ID_generator @new_id OUTPUT INSERT INTO @Table_ssn ([id] ,[first_nm] ,[last_nm] ) Values(@new_id,'Mike','','Smith') INSERT INTO @Table_people values (@new_id,'100000000','')