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
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
解决方案 »
- sql server2000如何导出MDF格式数据库文件
- 连接不上远程SQL2005,真是欲哭无泪啊!!
- 求一关于SQL语句的解决方案!
- 子查询返回的值多于一个时,把所的值连接成一个字符串,怎么写?
- 如何将 N 个select * from t语句合并为一个,显示一张表里?
- 百分求助,超难得SQL数据已经构造好了,请高手指教。急~~~~~~~~~~~~~~~~~~
- 如何实现这样的查询,关于两表操作?在线等
- 自连接问题,请教各位大侠,在线等。。
- 请教高手关于sql2005数据转换服务在哪里找啊?
- 数据库权限设置问题
- sql 升级(从6.5 到2000)急!!!!!!!!!!!!!!!
- 两表关联求查询结束,大家讨论最简单方法~~~
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、用函数,等......
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
http://expert.csdn.net/Expert/topic/1375/1375432.xml?temp=.65823
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
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
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
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)
为什么有错误啊,请指出,再次感谢!!
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
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 加拿大
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
我的第一个排序: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)
为什么有错误啊?
(isnumeric(tfield)=1 and isnumeric(@s)=1 and cast(tfield as bigint)>cast(@s as bigint)) 无论tfield和@s的值是什么,都会计算整条,所以为字符的时候会出错.
那 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吗??
关于第二个排序,
pengdali(大力 V3.0)的回答也很精彩啊!!
但老有些许问题,
zjcxc(邹建)帮忙看看能否解决??
多谢!!
是比较运算,不能用于 case when 的结果中.
是比较运算,不能用于 case when 的结果中.