且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[原创]通过动态Sql语句,一次性彻底删除或者修改SBO的某个字段对应内容的信息

更新时间:2022-09-18 12:33:44

尽管SBO强烈要求我们不要通过Sql语句在数据库维护界面上修改SBO业务信息,事实上,任何一个ERP等管理软件的厂商也都是这样要求的。不过有些时候,我们只有--真的只有这一条路了。
 
不过,要修改SBO的某个主数据的主键信息,还是应该慎重,至少您需要有能力一次性将数据库中的对应的所有数据全部修改过来才行。这里介绍两种通过动态Sql的方式来达到上述的目标。
 
实例一,清除SBO中没有被引用的主物料信息。
 
所谓没有被引用,原则上应该是:1、没有发生业务;2、没有被包含在BOM中。考虑到SBO的所有业务单据发生后都被记录在日志表OINM中,而BOM表对应着OITT和ITT1,那么,对应的修改程序应该为:
 
create procedure fsup_ClearNoRefenceItem
as
begin
 _declare@strTable nvarchar(50), @strSql nvarchar(4000)
 _declaremycursor cursor for
  _selectdistinct a.name from sysobjects a inner join syscolumns b on a.id=b.id and lower(b.name)='itemcode' and  OBJECTPROPERTY(a.id, N'IsUserTable') = 1
 open mycursor
 fetch next from mycursor into @strTable
 while @@fetch_status=0
 begin
  print_char(13)+'正在删除 ' + @strTable
  _select@strSql = '_deletefrom ' + @strTable + ' WHERE ItemCode not in (_selectdistinct ItemCode OINM union all _selectcode from OITT union all _selectdistinct code from ITT1)'
  __exec(@strSql)
  print '已经删除 ' + @strTable
  fetch next from mycursor into @strTable
 end
 close mycursor
 deallocate mycursor
end
go
 
这是一个存储过程,建立在您的SBO数据库中后,通过以下语句来执行删除:__exec fsup_ClearNoRefenceItem
 
当然,为了更好的扩展性,这个存储过程是可以带上参数的,比如带着参数将您不愿意删除的那么信息保留下来。这个功能扩展很容易,在此就不写出实际代码了。
 
实例二,修改SBO中某个主数据的主键信息为另一值。
 
SBO主数据是指非单据信息,比如物料、员工、仓库、财务科目等。在此列举的是仓库主数据的一次性修改。
 
create procedure fsup_ModifyAllWhsCode
 @OldWhs  nvarchar(100),
 @NewWhs  nvarchar(100)
as
begin
 _declare@strTable nvarchar(50), @strSql nvarchar(4000)
 _declaremycursor cursor for
  _selectdistinct a.name from sysobjects a inner join syscolumns b on a.id=b.id and lower(b.name)='WhsCode' and  OBJECTPROPERTY(a.id, N'IsUserTable') = 1
 open mycursor
 fetch next from mycursor into @strTable
 while @@fetch_status=0
 begin
  print_char(13)+'正在修改 ' + @strTable
  _select@strSql = '_update' + @strTable + ' SET WhsCode=N''' + @NewWhs + ''' WHERE WhsCode=N'''+@OldWhs+ ''''
print @strSql
  __exec(@strSql)
  print '已经修改 ' + @strTable
  fetch next from mycursor into @strTable
 end
 close mycursor
 deallocate mycursor
end
go
 
这个过程传过来两个参数,一个是原来的值,一个是新值。通过这个过程可以完成将多数的业务信息中仓库的原值修改为新值,不过,也有例外,比如仓库转储的时候,其的字段不叫WhsCode,比如ItemCode,在OITT和ITT1中成为Code,就无法完成了,这是需要你自己做些善后工作。当然可,如果您要修改SqlCode之类的,就完全没有任何后虞之忧了。
 
改进实例二,成为通用的主数据修改程序。
 
这个存储过程可以扩展得更为强大,比如传过来三个参数,其中一次参数为主键的列名,不妨称之为@keyColumn,另外两个仍然是原值和新值。这个时候过程中唯一要修改的就是将上述的 'WhsCode' 改为 @KeyColumn,就可以了。如下:
create procedure fsup_ModifyAllMasterCode
 @KeyColumn  nvarchar(30),
 @OldKey  nvarchar(100),
 @NewKey  nvarchar(100)
as
begin
 _declare@strTable nvarchar(50), @strSql nvarchar(4000)
 _declaremycursor cursor for
  _selectdistinct a.name from sysobjects a inner join syscolumns b on a.id=b.id and lower(b.name)=@KeyColumn and  OBJECTPROPERTY(a.id, N'IsUserTable') = 1
 open mycursor
 fetch next from mycursor into @strTable
 while @@fetch_status=0
 begin
  print_char(13)+'正在修改 ' + @strTable
  _select@strSql = '_update' + @strTable + ' SET ' + @KeyColumn + ' =N''' + @NewKey + ''' WHERE ' + @KeyColumn + ' = N'''+@OldKey + ''''
print @strSql
  __exec(@strSql)
  print '已经修改 ' + @strTable
  fetch next from mycursor into @strTable
 end
 close mycursor
 deallocate mycursor
end
go
 
以上代码仅针对基于Sql Server数据库的SBO系统,并在对应环境下测试通过。


本文转自foresun  51CTO博客,原文链接:http://blog.51cto.com/foresun/202709,如需转载请自行联系原作者