1、
declare @s varchar(30)
set @s='3yu' --下句有错误啊,怎么解决啊??
--set @s='4' --此时下句话不出错误
if isnumeric(@s)=1 
select tfield from #t 
where (isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) or ((isnumeric(tfield)=0 and isnumeric(@s)=0) and tfield >@s)
else
select tfield from #t 
where  tfield >@s

解决方案 »

  1.   

    1、
    declare @s varchar(30)
    set @s='3yu' --下句有错误啊,怎么解决啊??
    --set @s='4' --此时下句话不出错误
    if isnumeric(@s)=1 
    select tfield from #t 
    where (isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) or ((isnumeric(tfield)=0 or isnumeric(@s)=0) and tfield >@s)
    else
    select tfield from #t 
    where  tfield >@s2、用函数,等......
      

  2.   

    declare @s varchar(30)
    set @s='3yu' --下句有错误啊,怎么解决啊??
    --set @s='5' --此时下句话不出错误
    select tfield from #t where (case when isnumeric(tfield)=1 and isnumeric(@s)=1 then tfield >cast(@s as bigint) else  tfield >@s end )
    drop table #t
      

  3.   

    2、参照精华贴:
    http://expert.csdn.net/Expert/topic/1375/1375432.xml?temp=.65823
      

  4.   

    declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
    insert @a values(1,0,'中国')
    insert @a values(2,0,'美国')
    insert @a values(3,0,'加拿大')
    insert @a values(4,1,'北京')
    insert @a values(5,1,'上海')
    insert @a values(6,1,'江苏')
    insert @a values(7,6,'苏州')
    insert @a values(8,7,'常熟')
    insert @a values(9,6,'南京')
    insert @a values(10,6,'无锡')
    insert @a values(11,2,'纽约')
    insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200))
    while exists(select 1 from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1))
    begin
      insert @tmp1 select top 1 * from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1) order by TC_Id
      while exists(select 1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1))
        insert @tmp1 select a.* from  @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1)
    end
    select * from @tmp1
      

  5.   

    --第一个排序的问题,用下面的方法解决:create table #t (tfield varchar(30))
    insert into #t
    select '003'
    union all
    select '63'
    union all
    select 'a'
    union all
    select '514'
    union all
    select '2at'declare @s varchar(30)
    set @s='1yu'
    --set @s='5'
    select tfield
    from #t
    where
    case when isnumeric(@s)=1 and isnumeric(tfield)=1 then cast(tfield as bigint) else 0 end
    >
    case when isnumeric(@s)=1 and isnumeric(tfield)=1 then cast(@s as bigint) else 0 end
    or
    case when not(isnumeric(@s)=1 and isnumeric(tfield)=1) then tfield else '' end
    >
    case when not(isnumeric(@s)=1 and isnumeric(tfield)=1) then @s else '' enddrop table #t
      

  6.   

    --第二个排序的问题需要用一个自定义函数来解决create table a(TC_Id int,TC_PID int,TC_Name varchar(200))
    insert a values(1,0,'中国')
    insert a values(2,0,'美国')
    insert a values(3,0,'加拿大')
    insert a values(4,1,'北京')
    insert a values(5,1,'上海')
    insert a values(6,1,'江苏')
    insert a values(7,6,'苏州')
    insert a values(8,7,'常熟')
    insert a values(9,6,'南京')
    insert a values(10,6,'无锡')
    insert a values(11,2,'纽约')
    insert a values(12,2,'旧金山')
    go--创建辅助排序的自定义函数
    create function f_getmergid(@TC_Id int)
    returns varchar(1000)
    as
    begin
    declare @re varchar(1000),@TC_PID int
    set @re=cast(@TC_Id as varchar)
    select @TC_PID=TC_PID from a where TC_Id=@TC_Id
    while @@rowcount>0
    select @re=cast(@TC_PID as varchar)+','+@re
    ,@TC_PID=TC_PID from a where TC_Id=@TC_PID
    return(@re)
    end
    go--调用此函数进行排序
    select * from a order by dbo.f_getmergid(TC_Id)--删除数据测试环境
    drop table a
    drop function f_getmergid
      

  7.   

    1、to Rewiah(乘长风) :你的结果是正确的
    2、to myflok(阿棋):你的不行,
    提示:'>' 附近有语法错误。3、to pengdali(大力 V3.0) :你的有些许错误,
    你的结果是:
      1 0 中国
    4 1 北京
    5 1 上海
    6 1 江苏
    9 6 南京
    10 6 无锡
    7 6 苏州
    8 7 常熟
    2 0 美国
    11 2 纽约
    12 2 旧金山
    3 0 加拿大而我要的结果是:(就是还要按照TC_Id排序啊)
    7 6 苏州
    8 7 常熟

    9 6 南京
    10 6 无锡
    的前边啊4、to zjcxc(邹建) :你的和大力的一样啊,也有些许错误。再有啊,我的第一个排序
    select tfield from #t where (isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) or ((isnumeric(tfield)=0 or isnumeric(@s)=0) and tfield >@s)
    为什么有错误啊,请指出,再次感谢!!
      

  8.   

    declare @a table (TC_Id int,TC_PID int,TC_Name varchar(200))
    insert @a values(1,0,'中国')
    insert @a values(2,0,'美国')
    insert @a values(3,0,'加拿大')
    insert @a values(4,1,'北京')
    insert @a values(5,1,'上海')
    insert @a values(6,1,'江苏')
    insert @a values(7,6,'苏州')
    insert @a values(8,7,'常熟')
    insert @a values(9,6,'南京')
    insert @a values(10,6,'无锡')
    insert @a values(11,2,'纽约')
    insert @a values(12,2,'旧金山')declare @tmp1 table (TC_Id int,TC_PID int,TC_Name varchar(200))
    while exists(select 1 from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1))
    begin
      insert @tmp1 select top 1 * from @a where TC_PID=0 and tc_id not in (select tc_id from @tmp1) order by TC_Id
      while exists(select 1 from @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1))
        insert @tmp1 select a.* from  @a a,@tmp1 b where a.tc_pid=b.tc_ID and a.tc_ID not in (select tc_ID from @tmp1) order by a.TC_Id
    end
    select * from @tmp1
      

  9.   


    to pengdali(大力 V3.0) :
    十分感谢你的解答。
    可还不对啊,:-)
    现在的结果是:
    1 0 中国
    4 1 北京
    5 1 上海
    6 1 江苏
    7 6 苏州
    9 6 南京
    10 6 无锡
    8 7 常熟
    2 0 美国
    11 2 纽约
    12 2 旧金山
    3 0 加拿大而我要的结果是:
    1 0 中国
    4 1 北京
    5 1 上海
    6 1 江苏
    7 6 苏州
    8 7 常熟
    9 6 南京
    10 6 无锡
    2 0 美国
    11 2 纽约
    12 2 旧金山
    3 0 加拿大
      

  10.   

    --修改第二个排序--第二个排序的问题需要用一个自定义函数来解决create table a(TC_Id int,TC_PID int,TC_Name varchar(200))
    insert a values(1,0,'中国')
    insert a values(2,0,'美国')
    insert a values(3,0,'加拿大')
    insert a values(4,1,'北京')
    insert a values(5,1,'上海')
    insert a values(6,1,'江苏')
    insert a values(7,6,'苏州')
    insert a values(8,7,'常熟')
    insert a values(9,6,'南京')
    insert a values(10,6,'无锡')
    insert a values(11,2,'纽约')
    insert a values(12,2,'旧金山')
    go--创建辅助排序的自定义函数
    create function f_getmergid(@TC_Id int)
    returns varchar(1000)
    as
    begin
    declare @re varchar(1000),@TC_PID int
    set @re=right('0000'+cast(@TC_Id as varchar),4)
    select @TC_PID=TC_PID from a where TC_Id=@TC_Id
    while @@rowcount>0
    select @re=right('0000'+cast(@TC_PID as varchar),4)+','+@re
    ,@TC_PID=TC_PID from a where TC_Id=@TC_PID
    return(@re)
    end
    go--调用此函数进行排序
    select * from a order by dbo.f_getmergid(TC_Id)--删除数据测试环境
    drop table a
    drop function f_getmergid
      

  11.   

    多谢zjcxc(邹建)!!你的结果完全正确。不好意思,:-)
    我的第一个排序:select tfield from #t where (isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) or ((isnumeric(tfield)=0 or isnumeric(@s)=0) and tfield >@s)
    为什么有错误啊?
      

  12.   

    运算问题,对于
    (isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) 无论tfield和@s的值是什么,都会计算整条,所以为字符的时候会出错.
      

  13.   

    多谢!!!
    那 myflok(阿棋) 的回答为什么不对呢???
    select tfield from #t where (case when isnumeric(tfield)=1 and isnumeric(@s)=1 then tfield >cast(@s as bigint) else  tfield >@s end )是case when 不能用于where吗??
      

  14.   

    另:
    关于第二个排序,
    pengdali(大力 V3.0)的回答也很精彩啊!!
    但老有些许问题,
    zjcxc(邹建)帮忙看看能否解决??
    多谢!!
      

  15.   

    tfield >cast(@s as bigint) 和 tfield >@s
    是比较运算,不能用于 case when 的结果中.
      

  16.   

    tfield >cast(@s as bigint) 和 tfield >@s
    是比较运算,不能用于 case when 的结果中.
      

  17.   

    zjcxc(邹建)兄请帮忙再看看大力的回答啊:-)