在SQL中建立使用者自定義拼音函式

2021-05-28 10:29:23 字數 1630 閱讀 8129

--在sql中建立使用者自定義拼音函式:

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[f_getpy]') and xtype in (n'fn', n'if', n'tf'))

drop function [dbo].[f_getpy]

gocreate function f_getpy(@str nvarchar(400))

returns nvarchar(4000)

asbegin

declare @strlen int,@re nvarchar(4000)

declare @t table(chr nchar(1) collate chinese_prc_ci_as,letter nchar(1))

insert @t select '吖','a' union all select '八','b'

union all select '嚓','c' union all select '咑','d'

union all select '妸','e' union all select '發','f'

union all select '旮','g' union all select '鉿','h'

union all select '丌','j' union all select '咔','k'

union all select '垃','l' union all select '嘸','m'

union all select '拏','n' union all select '噢','o'

union all select '妑','p' union all select '七','q'

union all select '呥','r' union all select '仨','s'

union all select '他','t' union all select '屲','w'

union all select '夕','x' union all select '丫','y'

union all select '帀','z'

select @strlen=len(@str),@re=''

while @strlen>0

begin

select top 1 @re=letter+@re,@strlen=@strlen-1

from @t a where chr<=substring(@str,@strlen,1)

order by chr desc

if @@rowcount=0

select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1

endreturn(@re)

endgo

/*--測試

select dbo.f_getpy('廣州市') as 廣州市,dbo.f_getpy('廣東人') as 廣東人

--以後查詢的時候,就可以呼叫上面的函式來實現漢字模糊查詢

select * from dbo.vw_oillist where dbo.f_getpy(oilname)='93#qy'

*/

建立使用者自定義函式 SQL

建立使用者自定義函式 標量函式 create function dbo.bmrs bmh as int returns int asbegin declare bmrs int select bmrs count 工號 from 銷售人員 where 部門號 bmh return bmrs endg...

建立使用者自定義函式 SQL

建立使用者自定義函式 標量函式 create function dbo.bmrs bmh as int returns int asbegin declare bmrs int select bmrs count 工號 from 銷售人員 where 部門號 bmh return bmrs endg...

SQL 使用者自定義函式

使用者自定義函式是 sql server 的資料庫物件,它不能用於執行一系列改變資料庫狀態的操作,但它可以像系統函式一樣在查詢或儲存過程等的程式段中使用,也可以像儲存過程一樣通過 execute 命令來執行。使用者自定義函式中儲存了乙個 transact sql 例程,可以返回一定的值。在sql s...