生成xml並更新表字段

2021-05-24 00:29:05 字數 3681 閱讀 5101

--生成tm_xunique對應的xml

declare @x1 xml

set @x1 = 

cast((select a.name as "@tablename",

'uq_' + a.name + '_id_num' as "uniqueconstraint/uq_name", 

'(' + stuff((

select ',' + name from sys.columns 

where a.object_id = object_id 

and ( name like '%s_cid' escape 's' or name like '%s_cnum' escape 's' )

for xml path('')),1,1,'')+ ')' as "uniqueconstraint/uq_fields"

from sys.objects a

where type = 'u' 

and a.name in (select tm_ctable from xt_tableml)

for xml path('table')) as xml)

update xt_tableml set tm_xunique = tb_xml

from xt_tableml

select p.x.query('.') as tb_xml 

from @x1.nodes('/table') as p(x)

where p.x.value('(./@tablename)[1]','varchar(100)') = tm_ctable

)p--select tm_ctable,tb_xml

--from xt_tableml

--     select p.x.query('.') as tb_xml 

--     from @x1.nodes('/table') as p(x)

--     where p.x.value('(./@tablename)[1]','varchar(100)') = tm_ctable

--)p

--生成tm_xlink對應的xml

if object_id('tempdb..#tmp') is not null

drop table #tmp

;with tb(tbname , fieldname) as 

select b.name,a.name from sys.columns a

inner join sys.objects b on a.object_id = b.object_id

where type = 'u' 

and (a.name like '%s_cdept%' escape 's'  

or a.name like '%t_cperson%' escape 't' 

or a.name like '%s_cware%' escape 's'  

or a.name like '%s_center%' escape 's')  

and b.name in (select tm_ctable from xt_tableml)  

tb1(tbname,fieldid,fieldname) as

select a.tbname,

b.fieldname,

a.fieldname 

from tb a

left join tb b on a.tbname = b.tbname 

and a.fieldname = replace(b.fieldname,'id','') 

and patindex('%id%',b.fieldname)>0

where patindex('%id%',a.fieldname) = 0

select * into #tmp from tb1

declare @x1 xml

set @x1 = 

cast((select tbname "@tablename",

case when charindex('deptid',fieldid) > 0 then fieldid end "dept/deptid",

case when charindex('dept',fieldname) > 0 then fieldname end "dept/deptname",

case when charindex('personid',fieldid) > 0 then fieldid end "person/personid",

case when charindex('person',fieldname) > 0 then fieldname end "person/personname",

case when charindex('wareid',fieldid) > 0 then fieldid end "ware/wareid",

case when charindex('ware',fieldname) > 0 then fieldname end "ware/warename",

case when charindex('enter',fieldname) > 0 then fieldname end "enter/entername"

from #tmp

for xml path('table')) as xml)

--select @x1

update xt_tableml set tm_xlink = tb_xml

from xt_tableml s

select tm_ctable,

cast(replace(replace(replace(

select '' + tb_xml

from xt_tableml

select cast(p.x.query('.') as varchar(max)) as tb_xml 

from @x1.nodes('/table') as p(x)

where p.x.value('(./@tablename)[1]','varchar(100)') = tm_ctable

)pwhere a.tm_ctable = tm_ctable

for xml path(''))

,'',''),'<','<'),'>','>') as xml) as tb_xml 

from xt_tableml a 

where s.tm_ctable = tm_ctable

)p--select tm_ctable,

--cast(replace(replace(replace(

--            select '' + tb_xml

-- from xt_tableml

-- select cast(p.x.query('.') as varchar(max)) as tb_xml 

-- from @x1.nodes('/table') as p(x)

-- where p.x.value('(./@tablename)[1]','varchar(100)') = tm_ctable

-- )p

-- where a.tm_ctable = tm_ctable

--        for xml path(''))

--        ,'',''),'<','<'),'>','>') as xml) 

--from xt_tableml a

mysql5 7 多表字段更新

用乙個表中的字段去更新另外乙個表中的字段,mysql 中有相應的 update 語句來支援,不過這個 update 語法有些特殊。看乙個例子就明白了。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 create...

jpa 動態sql 部分更新表字段

transactional modifying clearautomatically true query value update info p set p.status 1 where p.id 2 nativequery true int updatestatusbyid string sta...

oracle聯合查詢並更新乙個表字段的sql語句

更新某乙個欄位寫起來簡單,也容易理解 update tablea a set a.name select name from tableb b where a.id b.id 更新多個欄位就顯得比較冗長 update tablea a set a.name select name from tabl...