set @sql=case @input when 'A' then '[啊]'when 'B' then '[不]'......

解决方案 »

  1.   

    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]
    GO--创建取拼音函数
    create function f_GetPy(@Str nvarchar(400))
    returns nvarchar(4000)
    as
    begin
    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
    end
    return(@re)
    end
    go--测试
    select dbo.f_GetPy('东莞市') as 东莞市,dbo.f_GetPy('ab中c国人') as 中国人
      

  2.   

    comerliang(天地良心) ( 可是客户指定必须要这样做啊。他们以前用的软件就是这样。所以现在也要求我们这样做。skyboy0720(虫族+神族+人族)晕菜。这样做肯定死人了。
      

  3.   

    --查询的时候,调用这个函数就行了select * from 表 where dbo.f_GetPy(姓名) like 'Z%'  --查询姓张的
      

  4.   

    zjcxc(邹建) 输入Z可以查处来姓张的,和姓郑的。就跟输入Z可以查出邹建一样
      

  5.   

    --如果只是按姓查询,那就把这个函数改一改,提高查询效率: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]
    GO--创建取拼音函数
    create function f_GetPy(@Str nvarchar(2))
    returns nvarchar(1)
    as
    begin
    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=''
    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
    return(@re)
    end
    go
      

  6.   


    declare @sql varchar(8000)
    set @sql=' select * from tablename '
    if @变量='L'
    set @sql=@sql + ' where 姓名字段 like '刘%'
    else
    begin
    if @变量='Z'
    set @sql=@sql + ' where 姓名字段 like '张%' or 姓名字段 like '郑%'
    end
    exec(@sql)                     ----低手留言
      

  7.   

    另外一个建议,如果查询是频繁的,那楼主在表中增加一个字段: 姓名拼音再写个触发器,自动维护这个字段,在表中记录发生变化时,自动更新姓名的拼音,这样查询时就可以直接查询这个字段,效率会高很多:--示例触发器
    create trigger tr_getpy on 表
    for insert,update
    as
      if update(姓名)
         update a set 姓名拼音=dbo.f_GetPy(a.姓名)
         from 表 a,inserted b
         where a.主键=b.主键
    go--以后查询
    select * from 表 where 姓名拼音='Z'
      

  8.   

    再次晕菜。这样的SQL语句我也会写啊。但是这样写下来会死人的。百家姓那么多。我的意思是。有没有SQL的字符集API。我直接使用SQL API去做字符集转换。或者有没有调用微软拼音或者智能ABC的API来实现这个功能。例如:select * from [user] order by 姓名 DESC---Result---
    username



    ..
    ..
    .. 既然SQL支持这样的排序,那么她的内部机制里面绝对有一个中文拼音字符编码表。我怎么才能调用到这个编码表???或者音节表??