1. 表与表的关联赋值(用于表与表之间有关联字段,数据互传)
双表关联赋值
UPDATE #B SET #B.D=#A.B from #B inner join #A on #B.C=#A.A
多表关联赋值
update a set a.e=c.n from a left join b on a.e=b.j left join c on b.k=c.m
2. 两种删除方式(TRUNCATE TABLE <--> DELETE FROM)
TRUNCATE TABLE #USER
删除极快,无日志所以无法恢复,且只能用于表数据全部删除
DELETE FROM #USER WHERE ...删除会产生日志信息,可用于数据恢复,可附加条件选择性删除
3. 利用系统表数据构建SQL查询语句
查询表的字段以及字段类型
SELECT NAME,SYSTEM_TYPE_ID FROM SYS.COLUMNS WHERE OBJECT_ID=OBJECT_ID(N'费用明细',N'U')
构建SQL查询语句,实现字段的规范
SELECT '(case when isnull(b.'+NAME+','''+(CASE SYSTEM_TYPE_ID WHEN 167 THEN '' WHEN 108 THEN '0' WHEN 42 THEN '1899-12-30' ELSE '' END)+''')='''+(CASE SYSTEM_TYPE_ID WHEN 167 THEN '' WHEN 108 THEN '0' WHEN 42 THEN '1899-12-30' ELSE '' END)+''' then '''+(CASE SYSTEM_TYPE_ID WHEN 167 THEN '-' WHEN 108 THEN '0' WHEN 40 THEN '1899-12-30' WHEN 42 THEN '1899-12-30' ELSE '-' END)+''' else b.'+NAME+' end) AS '+NAME+', ' FROM SYS.COLUMNS
WHERE OBJECT_ID=OBJECT_ID(N'住院费用明细',N'U')