sql - Updating table with child table values -


i have 2 table: crm_inquiries , ics_subscribers. crm_inquiries has primary key inquiryid , foreign key, subid associate table ics_subscribers fname , lname. ics_subscribers has primary key subid , columns subfirstname , sublastname. need run query retrieve crm_inquiry rows , update fname , lname values subfirstname , sublastname in ics_subscriber table. hope makes sense..

i wrote script accomplish wondering if there better way it.

create table #inq( inquiryid int, subid int )  insert #inq  select distinct inquiryid, subid  crm_inquiries  lname = 'poe' , subid not null   declare @totalcount int select @totalcount = count(*) #inq   print @totalcount  while(@totalcount > 0) begin  declare @subid varchar(250) declare @inquiryid int  select top 1 @subid = subid, @inquiryid = inquiryid #inq  print 'subid = ' + @subid print 'inquiryid= ' + cast(@inquiryid varchar(max))  update crm_inquiries  set fname = (select subfirstname ics_subscribers subid = @subid), lname = (select sublastname ics_subscribers subid = @subid) inquiryid = @inquiryid  delete #inq inquiryid = @inquiryid   select @totalcount = count(*) #inq  print @totalcount end   drop table #inq 

the lname='poe' arbitrary, have more complex query sake of example created that.

does accepted answer question resolve issue?

sql server - inner join when updating


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -