yunnet23 发表于 2006-10-18 12:38:04

比较两条记录中那些字段不相等

<p>比较两条记录中那些字段不相等,即某个字段的值是否有改变!</p><p>例子如下:</p><p>select&nbsp; nGoodsID, sGoodsDesc, sGoodsName, sSpec , sBrand , nPack ,&nbsp; sUnit ,nCaseUnits ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nCategoryID ,sHome ,sNationID , sNation ,sGoodsLevelID ,sGoodsLevel ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sGoodTypeID ,sGoodType ,sTradeModeID ,sTradeMode ,sDepositTypeID , sDepositType ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nMinLineUnit ,nMinLineL ,nShelfLife ,nMinDueDay ,sNote ,nLength ,nWidth ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nHeight ,nWeight ,nCaseLength ,nCaseWidth ,nCaseHeight ,nCaseWeight ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dBeginUseDate ,dEndUseDate ,sMemo1 ,sMemo2 ,sMemo3 ,sMemo4 ,sMemo5 ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nLineSize ,nSafeStockDay ,nOPLM ,sOrderModeID ,sOrderMode , nSalePrice ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nVipPrice,nSaleTaxRate , sMainBarcode ,nTag ,sChangeUser ,dChangeDate ,dLastUpdateTime<br/>&nbsp; into #Goods001<br/>&nbsp; from tGoods<br/>&nbsp;where nGoodsID = 211</p><p>select&nbsp; nGoodsID, sGoodsDesc, sGoodsName, sSpec , sBrand , nPack ,&nbsp; sUnit ,nCaseUnits ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nCategoryID ,sHome ,sNationID , sNation ,sGoodsLevelID ,sGoodsLevel ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sGoodTypeID ,sGoodType ,sTradeModeID ,sTradeMode ,sDepositTypeID , sDepositType ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nMinLineUnit ,nMinLineL ,nShelfLife ,nMinDueDay ,sNote ,nLength ,nWidth ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nHeight ,nWeight ,nCaseLength ,nCaseWidth ,nCaseHeight ,nCaseWeight ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dBeginUseDate ,dEndUseDate ,sMemo1 ,sMemo2 ,sMemo3 ,sMemo4 ,sMemo5 ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nLineSize ,nSafeStockDay ,nOPLM ,sOrderModeID ,sOrderMode , nSalePrice ,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nVipPrice,nSaleTaxRate , sMainBarcode ,nTag ,sChangeUser ,dChangeDate ,dLastUpdateTime<br/>&nbsp; into #Goods002<br/>&nbsp; from tGoods<br/>&nbsp;where nGoodsID = 211</p><p>select id=identity(4), name ,dd = convert(varchar(255), ''),cc=convert(varchar(255), '') <br/>&nbsp;&nbsp;&nbsp; into #Goods003 from tempdb..syscolumns where id = object_id('#Goods001')<br/>&nbsp;<br/>&nbsp;declare @Num1 int<br/>&nbsp;declare @i int<br/>&nbsp;declare @Name varchar(20)<br/>&nbsp;declare @GoodsID varchar(8)</p><p>&nbsp;select @GoodsID ='211'<br/>&nbsp;select @i = 1<br/>&nbsp;select @Num1 = max(id) from #Goods003</p><p>while @i &lt; @Num1+ 1<br/>begin<br/>&nbsp;&nbsp; select @Name = name from #Goods003 where id = @i<br/>&nbsp;&nbsp; exec ( 'update #Goods003 set dd = convert(varchar(255), (select ' + @Name+ ' from #Goods001 where nGoodsID = convert(numeric(8,0) , ' + @GoodsID + ' ) ) )where name = <a href="mailto:'''+@Name+''''">'''+@Name+''''</a>&nbsp; )<br/>&nbsp;&nbsp; exec ( 'update #Goods003 set cc = convert(varchar(255), (select ' + @Name+ ' from #Goods002 where nGoodsID = convert(numeric(8,0) , ' + @GoodsID + ' ) ) )where name = <a href="mailto:'''+@Name+''''">'''+@Name+''''</a>&nbsp; )<br/>&nbsp;&nbsp; select @i = @i + 1&nbsp; <br/>end</p><p>select * from #Goods003 where dd&lt;&gt;cc</p><p>drop table #Goods001<br/>drop table #Goods002<br/>drop table #Goods003</p>

yunnet23 发表于 2006-10-18 12:42:02

<p>将结果插入一个新表:</p><p>create table tAlterLog(<br/>nID int identity not null,/*ID,序号,没有实际意义*/<br/>dTradeDate datetime not null,/*时间*/<br/>sTableName varchar(30) not null,/*表名*/<br/>sFieldName varchar(30) not null,/*栏位名*/<br/>sOldValue varchar(255) null,/*原值,如果新增资料,此栏位为空*/<br/>sNewValue varchar(255) not null,/*新值*/<br/>sModuleID varchar(20) null,/*产生这条记录的模块编号*/<br/>sVersion varchar(10) null,/*模块对应的版本号*/<br/>sEditer varchar(20) null,/*产生这条记录的操作人*/<br/>constraint PK_ALTERLOG primary key clustered (nID)<br/>on "default"</p><p>)<br/>go</p>
页: [1]
查看完整版本: 比较两条记录中那些字段不相等

免责声明:
本站所发布的一切破解补丁、注册机和注册信息及软件的解密分析文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:Admin@SybaseBbs.com