设为首页 加入收藏

TOP

一些比较难的sql问题:7(三)
2014-11-23 20:26:15 来源: 作者: 【 】 浏览:36
Tags:一些 比较 sql 问题
易于理解,也不易于修改,因为别人不一定能理解你这么写的意思。另一个问题就是性能的问题,因为在原来的公司就用过这种方法,一段时间后,查询非常慢,本来几秒就能出结果,后来几分钟都出不了结果。说实在的,这种方法要求较高的技巧性,也容易出错,不建议使用。
下面是我的解法,用了动态语句来实现,但考虑了维护、测试方面的要求:
[sql] 
--drop table #tableTest  
  
create table #tableTest(id int identity , name varchar(20),age int,)  
go  
insert into #tableTest  
select '小明',23 union all  
select '小红',28 union all  
select '小军',27  
go  
select *from #tableTest  
go  
  
create proc procTest  
@name varchar(20)=null,@age int = null,@IDs varchar(30) = null  
as  
  
declare @sql nvarchar(max);  
set @sql = '';  
  
set @sql = 'select * from #tableTest where 1 = 1';  
  
set @sql = @sql +   
           case when @name is not null  
                     then ' and name like ' + QUOTENAME(@name +'%','''')  
                when @age is not null  
                     then ' and age = ' + cast(@age AS varchar)  
                when @ids Is not null  
                     then ' and id in (' + @ids +')'  
                else ' '  
           end  
  
--打印出语句  
select @sql  as '语句'  
  
--执行语句  
--exec(@sql)  
go  
  
  
exec procTest  
/*  
语句  
select * from #tableTest where 1 = 1   
*/  
  
exec procTest '小明',23  
/*  
语句  
select * from #tableTest where 1 = 1 and name like '小明%'  
*/  
  
  
exec procTest @ids = '2,3'  
/*  
语句  
select * from #tableTest where 1 = 1 and id in (2,3)  
*/  

3、为什么replace函数替换不了 ,请高手指点
想把一个字符串中所有出现在delete_author表中的字符都删除掉(也就是替换成空)
delete_author表中的内容是:
我写成一个存储过程:(有些输出是为了测试)
create proc proc_del_special 
@input_str varchar(100),@output_str varchar(100) output
as
declare @author  varchar(256),@delsym varchar(10),@temp_str varchar(100)
declare del_cur scroll cursor for select del_author from delete_author
open del_cur
fetch next from del_cur into @delsym
set @author=@input_str
while @@fetch_status=0
     begin 
         print replace(@author,@delsym,'')
        set @temp_str=replace(@author,@delsym,'')
         set @author=@temp_str
         print @author
         print @delsym
         fetch next from del_cur into @delsym
     end 
set @output_str=@author
close del_cur
deallocate del_cur

测试例子:
declare @a1 varchar(100),@a2 varchar(100)
set @a1='(美)啊那编译'
exec proc_del_special @a1,@a2 output
select @a2

执行结果还是:(美)啊那编译
执行过程中的消息:
美)啊那编译
(美)啊那编译
主编
(美)啊那编译
(美)啊那编译
编译
(美)啊那编译
(美)啊那编译
(美)
(美)啊那编译
(美)啊那编译
(英)
(美)啊那编译
(美)啊那编译
(苏)
(美)啊那编译
(美)啊那编译
[等]
(美)啊那编译
(美)啊那编译
(日)
(美)啊那编译
(美)啊那编译
编著
(美)啊那编译
(美)啊那编译
(西)
(1 行受影响)
就是replace函数没有替换成功,这是为什么啊 ,请哪位大侠指点一下 ,在线等,非常感谢了
我的解法,尽量少用游标,用函数也可以实现
[sql] 
--drop table tb  
  
create table tb(v nvarchar(100))  
  
insert into tb  
select '(英)啊那编译' union all  
select '小泉纯一郎(日)' union all  
select '皇家马德里对(西)主编' union all  
select '奥巴马(美)编著' union all  
select '中国[等]'  
  
  
create table delete_author (del_author nvarchar(50))  
  
insert into delete_author  
select '主编' union all  
select '编译' union all  
select '(美)' union all  
select '(英)' union all  
select '(苏)' union all  
select '[等]' union all  
select '(日)' union all  
select '编著' union all  
select '(西)'   
  
  
  
if exists(select * from sys.objects where name = 'fn_replace_symbol')  
   drop function dbo.fn_replace_symbol;  
go  
  
create function dbo.fn_replace_symbol(@n nvarchar(1000))  
returns nvarchar(1000)  
as  
begin  
declare @i int;  
declare @count int;  
  
set @i = 1  
set @count = (select count(*) from delete_author);  
         
while @i <= @count  
begin  
   ;with t  
   as  
   (  
   select del_author,  
          row_number() over(order by @@servername) as rownum  
   from delete_author  
   )  
      
   select @n = replace(@n,(select del_author from t where rownum = @i),'')  
   set @i = @i + 1  
end  
  
return @n  
end  
go  
  
--替换效果  
select * ,  
       dbo.fn_replace_symbol(v) as 替换完后的字符  
from tb  
/*  
v                   替换完后的字符  
(英)啊那编译          啊那  
小泉纯一郎(日)        小泉纯一郎  
皇家马德里对(西)主编 皇家马德里对  
奥巴马(美)编著        奥巴马  
中国[等]               中国  
*/  


首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle数据库索引聚簇与哈希聚簇.. 下一篇SQL Server 中各个系统表的作用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: