码迷,mamicode.com
首页 > 数据库 > 详细

SQL中对数据每一列的数据处理

时间:2016-09-21 11:26:33      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

不久前做了一个项目,其中需要对操作数据表中的每一列数据,业务需求是,一张日报表,一张基础数据表,日报表每天都会提交数据去更新基础数据表,现在需求是拿出最近一次导入的日报数据去和基础表数据做比较,2张表的数据,对应的列,哪一列不一样,就做出标记或者其他的什么需求的,

先贴上我弄好的存储过程吧:

ALTER PROCEDURE [dbo].[sp_WechatGroup_DayReport_Import_CompareDayAndBasedata]

@Date nvarchar (20)

 AS
    BEGIN
    IF EXISTS(SELECT * FROM sysobjects WHERE name=‘#@table‘)
    DROP TABLE #@table
    ELSE

   Select *,‘‘ AS remark into #@table From WechatGroup_DayReport w where CONVERT(NVARCHAR(10),CreateDate,121) = CONVERT(NVARCHAR(10),CONVERT(DATETIME,@Date),121)

   ALTER TABLE #@table ALTER COLUMN remark NVARCHAR(800)
   UPDATE #@table  SET remark += CASE WHEN B.AnchorCity <> C.AnchorCity THEN ‘AnchorCity, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId
   
   UPDATE #@table  SET remark += CASE WHEN B.Attribute <> C.Attribute THEN ‘群属性 ,‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.city <> C.city THEN ‘城市, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.ClusterCity <> C.ClusterCity THEN ‘ClusterCity, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.CreateUser <> C.CreateUser THEN ‘创建时间, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.Department <> C.Department THEN ‘部门, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.GroupId <> C.GroupId THEN ‘群ID, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.GroupName <> C.GroupName THEN ‘群名, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.HospitalCode <> C.HospitalCode THEN ‘医院代码, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.HospitalName <> C.HospitalName THEN ‘医院名称, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.Province <> C.Province THEN ‘省, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.Region <> C.Region THEN ‘大区, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId

      UPDATE #@table  SET remark += CASE WHEN B.Status <> C.Status THEN ‘进群状态, ‘ ELSE ‘‘ END
   FROM #@table B, [dbo].[WechatGroup_BaseData] C
   WHERE B.GroupId = C.GroupId


    Select  *,ROW_NUMBER() OVER (order by GroupId) AS Raws from #@table
    
    
  END

SQL中对数据每一列的数据处理

标签:

原文地址:http://www.cnblogs.com/jisin/p/5891931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!