您的位置:程序门 -> ms-sql server -> 基础类



这句sql语句该如何写?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


这句sql语句该如何写?
发表于:2007-06-28 22:13:55 楼主
库里的share1dept为text类型,存的是以逗号分隔的多个id号,我想找出来里面包含多个指定id号的记录

刚开始想用   share1dept   in   (55,56,57,58,59)
但是提示将 ', '转换为整型时出错,可能是text里不能找id为整型的值吧
后来想改成类似
charindex( ', '+rtrim(rtrim(59))+ ', ', ', '+share1dept+ ', ')> 0
这种的,但只能找一个id号,不能同时找多个id号是否存在了,怎么办??

因为找哪个id号是动态取出来的,所以不能用多个这样的语句,太没效率了
发表于:2007-06-28 22:16:231楼 得分:0
对text类型心有余悸的说
发表于:2007-06-28 22:49:162楼 得分:0
--sample


create   table   a(txt   text)

insert   into   a
select   '12,11,13,15 '
select   *   from   a   where   charindex(cast(12   as   char(2)),txt) <> 0

drop   table   a
发表于:2007-06-28 23:20:533楼 得分:0
看不太懂,我是在.net里写的sql语句
发表于:2007-06-29 06:22:324楼 得分:0
好像一次只能用charindex()判断一个,可能只能用or连接一片charindex()了。。。
发表于:2007-06-29 08:21:255楼 得分:0
给你一个思路:
可以用游标,循环查找,找到一个把符号后的重新赋值再查找,用函数charindex()
发表于:2007-06-29 08:38:086楼 得分:0
up
发表于:2007-06-29 09:52:277楼 得分:0
不是好明白你的意思,
不过可以给你字符串换成表的方法

1
declare   @str   varchar(200),@sql   varchar(200)
set   @str= 'sdhg,w324,gwre,rweh,we '
select   @sql= 'select   ' ' '+   replace(@str, ', ', ' ' '   union   all   select   ' ' ')   + ' ' ' '
EXEC(@sql)

2
select   top   100     id=identity(int,1,1)   into   #   from   sysobjects   a,sysobjects   b

declare   @str   varchar(200)
set   @str= 'sdhg,w324,gwre,rweh,we '
select   substring(@str+ ', ',id,charindex( ', ',@str+ ', ',id)-id)   from   #
where   substring( ', '+@str,id,1)= ', '


快速检索

最新资讯
热门点击