Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to load data to several tables while passing id numbers, which was generated by SP

$
0
0

Environment: SQL server 2005 and Win 7.
Tools: MS. SQL Management Studio 2008 R2.
Data: Flat file source contains large data but here let say nine records in a csv format. The attributes are [SSN], [First name], and [Last name] listed in order:
('001456789',  'Jim', 'Kramer');
('008610321', 'Samanta', 'Cohen'),
('097754378', 'Ray', 'elvicio' )
('112233350', 'Mike', 'Smith'), 
('123456789', 'Dana', 'Kobo'), 
('123456789', 'Dalia', 'Budd'), 
('987654321', 'George', 'Jackson'), 
('987654321', 'Pamella', 'Slattery') , 
('987654321', 'Jose', 'Slattery'),

Or this data is stored on Table and let call it "Table_imported"

Table#1:

DECLARE @Table_people TABLE 
(
SSN CHAR(9) NOT NULL Primary key
pn_id CHAR(9) NOT NULL Reference @table_people (pn_id)
,first_nm VARCHAR(10)
,last_nm VARCHAR(20)
)

Table#2

DECLARE @Table_SSN TABLE 
(
ssn CHAR(9) NOT NULL 
,pn_ID CHAR(9) NOT NULL
)

Stored procedure which is designed to generate randomly pn_id of 9 char length

PROCEDURE [dbo].[proc_gen_pn_id]( 
@new_pn_id	  CHAR(9) OUTPUT)
AS
BEGIN
	DECLARE
		@new_nbr	CHAR(9),
		@user_cnt	INT,
		@cnt 		INT,
		@len		INT
	SELECT	@cnt = 1
	WHILE( @cnt = 1)
	BEGIN
SELECT	@new_nbr = convert(CHAR(9),convert(int,(RAND()*1000000000)))
		SELECT @len = datalength(ltrim(rtrim(@new_nbr)))
		WHILE (@len < 9)
			BEGIN
	SELECT @new_nbr = '0'+@new_nbr
	SELECT @len = @len +1
		END
)
Problem: Imagine you have 1000s (large scale) of records in a flat file (CSV format). How would you load those data into above tables. The process should be automated to save time and prevent errors. Creativity is welcomed!!

Expected results:

1. Table_ssn should contain the following data

.....
.....
001456789   |  791011616
008610321   |  565059001
097754378   |  698173008
112233350   |  964173440
123456789   |  708173550
123456789   |  259872340
987654321   |  011034567
987654321   |  098210001
987654321   |  167952525

2. Table_people

.....
.....
001456789   |  791011616  | Jim      | Kramer
008610321   |  565059001  | Samanta  | Cohen
097754378   |  698173008  | Ray      | elvicio
112233350   |  964173440  | Mike     | Smith
123456789   |  708173550  | Dana     | Kobo
123456789   |  259872340  | Dalia    | Budd
987654321   |  011034567  | George   | Jackson
987654321   |  098210001  | Pamella  | Slattery
987654321   |  167952525  | Jose     | Slattery

Please advise



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>