database - Entering 40000 rows in sql server through loop -
i have make database system purely on sql server. it's diagnostic lab. should contain @ least 40,000 distinct patient records. have table named "patient" contains auto-generated id, name, dob, age , phone number. our teacher provided dummy stored procedure contained 2 temporary tables has 200 names each , in end makes cartesian product supposed give 40,000 distinct rows. have used same dummy stored procedure , modified according our table. rows inserted 1260 every time. each time run query not give more 1260 records. have added part of temporary name tables , stored procedure.
declare @tfirstnames table( firstname varchar(50) not null ) declare @tlastnames table ( lastname varchar(50) not null ) declare @tnames table ( id int identity not null, name varchar(50) not null) insert @tfirstnames (firstname) select 'julianne' union select 'sharyl' union select 'yoshie' union select 'germaine' union select 'ja' union select 'kandis' select 'hannelore' union select 'laquanda' union select 'clayton' union select 'ollie' union select 'rosa' union select 'deloras' union select 'april' union select 'garrett' union select 'mariette' union select 'carline' union insert @tlastnames (lastname) select 'brown' union select 'chrichton' union select 'bush' union select 'clinton' union select 'blair' union select 'wayne' union select 'hanks' union select 'cruise' union select 'campbell' union select 'turow' union select 'tracey' union select 'arnold' union select 'derick' union select 'nathanael' union select 'buddy' insert @tnames select firstname + ' ' + lastname @tfirstnames, @tlastnames declare @iindex integer declare @ipatienttotalrecords integer declare @vcname varchar(50) declare @iage integer --set @iindex = 1 select @ipatienttotalrecords = max(id), @iindex = min(id) @tnames while @iindex <= @ipatienttotalrecords begin select @vcname = name @tnames id = @iindex set @iage = cast( rand() * 70 integer ) + 10 insert patient values (@vcname, @iage, case cast( rand() * 3 integer) when 0 'male' when 1 'female' else 'female' end, cast( rand() * 8888889 integer ) + 1111111, dateadd ( year, -@iage, getdate())) set @iindex = @iindex + 1 end
possible miss type union -
select 'julianne' union select 'sharyl' union select 'yoshie' union select 'germaine' union select 'ja' union select 'kandis' --<-- missing union select 'hannelore' union select 'laquanda' union select 'clayton' union select 'ollie' union select 'rosa' union select 'deloras' union select 'april' union select 'garrett' union select 'mariette' union select 'carline'
try 1 (without while , additional variables):
declare @tfirstnames table (firstname varchar(50) not null) insert @tfirstnames (firstname) values ('julianne'), ('sharyl'), ('yoshie'), ('germaine'), ('ja'), ('kandis'), ('hannelore'), ('laquanda'), ('clayton'), ('ollie'), ('rosa'), ('deloras'), ('april'), ('garrett'), ('mariette'), ('carline') declare @tlastnames table (lastname varchar(50) not null) insert @tlastnames (lastname) values ('brown'), ('chrichton'), ('bush'), ('clinton'), ('blair'), ('wayne'), ('hanks'), ('cruise'), ('campbell'), ('turow'), ('tracey'), ('arnold'), ('derick'), ('nathanael'), ('buddy') insert dbo.patient (...) select -- possible problem: string or binary data truncated d.flname -- <-- firstname + lastname i.e. 50 + 1 + 50 = 101 chars , d.age , gender = case abs(cast((binary_checksum(newid(), newid())) int)) % 3 when 0 'male' else 'female' end , (abs(cast((binary_checksum(newid(), newid())) int)) % 8888889) + 1111111 , birthday = convert(varchar(10), dateadd( year, -d.age, getdate()), 112) ( select flname = f.firstname + ' ' + l.lastname , age = (abs(cast((binary_checksum(f.firstname, newid())) int)) % 70) + 10 @tfirstnames f cross join @tlastnames l ) d
Comments
Post a Comment