|
楼主 |
发表于 2012-9-20 19:27:28
|
显示全部楼层
- [size=1em]SET ANSI_NULLS ON
- [size=1em]GO
- [size=1em]SET QUOTED_IDENTIFIER ON
- [size=1em]GO
- [size=1em]CREATE TRIGGER [TRIGGER 存货代码自动生成
- [size=1em] ON dbo.Inventory
- [size=1em] FOR INSERT, UPDATE
- [size=1em]AS
- [size=1em]BEGIN
- [size=1em] SET NOCOUNT ON;
- [size=1em]IF not EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME ='f_GetPy' AND Xtype='FN')
- [size=1em]begin
- [size=1em]exec ('create function f_GetPy(@str nvarchar(4000)) returns nvarchar(4000)
- [size=1em]as
- [size=1em]begin
- [size=1em]declare @strlen int,@re nvarchar(4000)
- [size=1em]declare @t table(chr nchar(1) collate Chinese_PRC_CI_AS,letter nchar(1))
- [size=1em]insert into @t(chr,letter)
- [size=1em] select ''吖'',''A'' union all select ''八'',''B'' union all
- [size=1em] select ''嚓'',''C'' union all select ''咑'',''D'' union all
- [size=1em] select ''妸'',''E'' union all select ''发'',''F'' union all
- [size=1em] select ''旮'',''G'' union all select ''铪'',''H'' union all
- [size=1em] select ''丌'',''J'' union all select ''咔'',''K'' union all
- [size=1em] select ''垃'',''L'' union all select ''嘸'',''M'' union all
- [size=1em] select ''拏'',''N'' union all select ''噢'',''O'' union all
- [size=1em] select ''妑'',''P'' union all select ''七'',''Q'' union all
- [size=1em] select ''呥'',''R'' union all select ''仨'',''S'' union all
- [size=1em] select ''他'',''T'' union all select ''屲'',''W'' union all
- [size=1em] select ''夕'',''X'' union all select ''丫'',''Y'' union all
- [size=1em] select ''帀'',''Z''
- [size=1em] select @strlen=len(@str),@re=''''
- [size=1em] while @strlen>0
- [size=1em] begin
- [size=1em] select top 1 @re=letter+@re,@strlen=@strlen-1
- [size=1em] from @t a where chr<=substring(@str,@strlen,1)
- [size=1em] order by chr desc
- [size=1em] if @@rowcount=0
- [size=1em] select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
- [size=1em] end
- [size=1em] return(@re)
- [size=1em]end')
- [size=1em]end
- [size=1em]declare @cInvCode nvarchar(1000) ,@cInvName nvarchar(1000)
- [size=1em]declare In_Prdetail_Cursor cursor for Select cInvCode ,cInvName from INSERTED
- [size=1em]open In_Prdetail_Cursor
- [size=1em]Fetch next from In_Prdetail_Cursor into @cInvCode,@cInvName
- [size=1em]While @@fetch_status=0
- [size=1em]begin
- [size=1em]update dbo.Inventory set cInvAddCode= dbo.f_GetPy(@cInvName) where cInvCode= @cInvCode
- [size=1em]Fetch next from In_Prdetail_Cursor into @cInvCode,@cInvName
- [size=1em]end
- [size=1em]close In_Prdetail_Cursor
- [size=1em]deallocate In_Prdetail_Cursor
- [size=1em]END
- [size=1em]GO
|
|