[原創]通過動態Sql語句,一次性徹底刪除或者修改SBO的某個字段對應內容的信息

儘管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系統,並在對應環境下測試通過。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章