sql server - Getting corresponding auto-generated ID's in SQL to match XML passed values -
i new sql, , xml please bear me. have query gets values entered multi-line textbox, passes them xml @data xml parameter declared , writes them history , diary tables respectively. along other parameters declared.
so, problem is, first written diary table id's in table auto-generated, 551 , 552 respectively below matters, information inserted history table. should not happen.
i want information written history table first, "auto-generated" id in history respective matters should passed foreign key diary table , inserted column (dy_h_id). @ moment, column not being populated data added later.
please see code below:
declare @u_id varchar(50) declare @add_u_id varchar(50) declare @subject varchar(50) declare @ipriority int declare @data xml declare @un varchar(500) declare @date varchar(50) declare @sdesc varchar(500) set @u_id = 18185 set @add_u_id = 18185 --'liihvcvbbbr43v55vx4wsk2x' set @subject = 'bulk' set @ipriority = 2 set @data = '<matters> <string>g0000010</string> <string>g0000011</string> </matters>' set @date = '2013/08/23 17:00' set @sdesc = 'diary item added : 2013/08/23 17:00 <br/>user : tommy de longe <br/> bulk pam' select @un = rtrim(ltrim(isnull(u_name,'') + ' ' + isnull(u_surname,''))) users with(nolock) id = @u_id ;with cte_tmp ([matteridx], [matterid]) ( select null, paramvalues.m_id.value('.','varchar(50)') @data.nodes('/matters/string') paramvalues(m_id) ) select matter.m_idx m_idx, [matterid] m_id #tbl_matter cte_tmp inner join matter with(nolock) on cte_tmp.[matterid] = matter.id insert dairy(dy_idx,dy_matterno,dy_userid,dy_date,dy_subject,dy_priority) select #tbl_matter.m_idx, #tbl_matter.m_id, @u_id, @date, @subject, @ipriority #tbl_matter insert history(h_idx, h_mid,h_userid,h_description,h_ha_id,h_hsa_id) select #tbl_matter.m_idx, #tbl_matter.m_id, @add_u_id, replace(replace(replace(@sdesc,'@un', @un),'@date', @date),'@subject', @subject), 7,19 #tbl_matter drop table #tbl_matter
i received code maintain , breaking head. know need switch 2 inserts @ bottom of query around, insert history table first, corresponding auto-generated id's (matter g0000010, matter g0000011) in case insert values diary table. sometime user can enter 20 values textbox, other times 2, must dynamic.
any regards appreciated. in advance.
thanks @bummi. used output inserted id's on history table. , took id , identifier h_mid table variable called @mytablevar. on insert of diary table, joined table variable on identifier. add table variable id column insert list of diary table. works dream! big @bummi. below code:
declare @u_id varchar(50) declare @add_u_id varchar(50) declare @subject varchar(50) declare @ipriority int declare @data xml declare @un varchar(500) declare @date varchar(50) declare @sdesc varchar(500) declare @mytablevar table ( id int not null, h_mid varchar(50) ); set @u_id = 18185 set @add_u_id = 18185 set @subject = 'bulk' set @ipriority = 2 set @data = '<matters> <string>g0000010</string> <string>g0000011</string> </matters>' set @date = '2013/08/23 17:00' set @sdesc = 'diary item added : 2013/08/23 17:00 <br/>user : tommy de longe <br/> bulk pam' select @un = rtrim(ltrim(isnull(u_name,'') + ' ' + isnull(u_surname,''))) users with(nolock) id = @u_id; cte_tmp ([matteridx], [matterid]) --column names temporary table ( select null, paramvalues.m_id.value('.','varchar(50)') @data.nodes('/matters/string') paramvalues(m_id) ) select matter.m_idx m_idx, [matterid] m_id #tbl_matter cte_tmp inner join matter with(nolock) on cte_tmp.[matterid] = matter.id insert history(h_idx, h_mid,h_userid,h_description,h_ha_id,h_hsa_id) output inserted.id, inserted.h_mid @mytablevar(id, h_mid) select #tbl_matter.m_idx, #tbl_matter.m_id, @add_u_id, replace(replace(replace(@sdesc,'@un', @un),'@date', @date),'@subject', @subject), 7,19 #tbl_matter insert dairy(dy_idx,dy_matterno,dy_userid,dy_date,dy_subject,dy_priority,dy_h_id) select #tbl_matter.m_idx, #tbl_matter.m_id, @u_id, @date, @subject, @ipriority, mtv.id #tbl_matter inner join @mytablevar mtv on #tbl_matter.m_id = mtv.h_mid drop table #tbl_matter
Comments
Post a Comment