declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%%'' and bianhaohp like ''%%'' and yanse like ''%%'' group by leibie,bianhaoHP,yanse order by leibie,bianhaoHP,yanse '
exec(@s)
但是如下套入存储过程后,调用不到任可东西。谁能指点一下?这个过程程该怎么写?怎么调用?(主要是想在调用过程时可以按三个不同条件返回数据,如果三个条件为空时则返回所有数据)急用呀!
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'''
24 个解决方案
#1
create proc sp_kucunFB%
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'
''
#2
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+' %''
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+' %''
#3
楼上的,你这是怎么回事?
#4
set @s=@s+' from vs_kucun where leibie like ''%'+@条件1+'%'''
不过没明白 from vs_kucun where leibie like ''%%'' and bianhaohp like ''%%'' and yanse like ''%%''
这么设置条件有什么意义
不过没明白 from vs_kucun where leibie like ''%%'' and bianhaohp like ''%%'' and yanse like ''%%''
这么设置条件有什么意义
#5
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'''%''
and bianhaohp like ''%'''+@条件2+'''%''
and yanse like ''%'''+@条件3+'''%''
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
go
试一下,未测。
#6
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'''%''
and bianhaohp like ''%'''+@条件2+'''%''
and yanse like ''%'''+@条件3+'''%''
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s) --补充一下
go
#7
请问楼上的 htl258 ,我调用 exec sp_kucunfb '','','' 。只能返回商品名称与仓库名称。如果调用
exec sp_kucunfb '1','1','1' 时提示1附近有错误。请求帮忙解决!!
exec sp_kucunfb '1','1','1' 时提示1附近有错误。请求帮忙解决!!
#8
我这是想在调用时可以根据条件返回需要数据。
#9
create proc sp_kucunFB再看看
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where charindex('''+@条件1+''',leibie)>0
and charindex('''+@条件2+''',bianhaohp)>0
and charindex('''+@条件3+''',yanse)>0
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#10
yanse,bianhaohp,leibie字段类型是什么?
#11
create proc sp_kucunFB如还有问题就用这样。
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where (charindex('''+@条件1+''',rtrim(leibie))>0 or '+@条件1+' is null)
and (charindex('''+@条件2+''',rtrim(bianhaohp))>0 or '+@条件2+' is null)
and (charindex('''+@条件3+''',rtrim(yanse))>0 or '+@条件3+' is null)
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#12
这个调用“exec sp_kucunfb '','',''”时提示“在关键字 'is' 附近有语法错误。”
#13
这三个字段的属性都是 varchar(30), 这个没有问题!
#14
有新发现了,如果用这个过程的话调用“exec sp_kucunfb '100D','2','1'”可以返回数据了,每个条件都必须带条件查询,如有一个不带就返回不了数据。
#15
换个思路:
把多个条件设置为一个字符串变量即可
把多个条件设置为一个字符串变量即可
#16
麻烦你说得清楚一点嘛。
#17
如果用这个过程调用“exec sp_kucunfb 'NULL','NULL','NULL'”时可以返回所有数据,当调用“exec sp_kucunfb '9D','NULL','NULL'”或“exec sp_kucunfb 'NULL','NULL','9D'”时提示D排名无效,反正条件中只能用数字。反之用符号,字母、汉字都不行。怎样解决?
#18
exec sp_kucunfb 'where 1=1 and xxxx=''xxxx1'' and yyyy=''yyyy1''' ”
--条件设置组合成为一个字符串。包括条件列名一起写入。
而不要设置成分开的条件 exec sp_kucunfb 'NULL','NULL','NULL'
--条件设置组合成为一个字符串。包括条件列名一起写入。
而不要设置成分开的条件 exec sp_kucunfb 'NULL','NULL','NULL'
#19
有没有人为我解决?我想直接调用exec sp_kucunfb '','','',则查询全部,可以在随便在那个加或多个加条件(含字符)都通返数准确数据。为我解决者我定加分送。
#20
LZ的需求其实根本用不到动态查询语句, 这样写根本利用不上索引, 查询效率很低的
LZ的需求在报表按组合条件查询时非常常见的, 既然LZ已经写好了存储过程, 稍改动两点儿就可以满足要求了
在语句 select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'') 之后添加
set @条件1=@条件1+'%'
set @条件2=@条件2+'%'
set @条件3=@条件3+'%'
补充未完成的语句set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+''' 如下
set @s=@s+' from vs_kucun where leibie like '''+ @条件1+''' and bianhaohp like '''+@条件2+''' and yanse like '''+@条件3+''' group by leibie,bianhaoHP,yanse order by leibie,bianhaoHP,yanse '
说明一下
1.按''查询或按NULL查询, 其实@条件变换成了%, 这样where 字段 like % 等价于 查询所有
2.如果@条件有值(如LeiBie), 变换成LeiBie%, 这样where 字段 like @条件 等价于 精确查询
依此类推, 这样的话, 再多的 组合条件 都可以如此照做
啰嗦一下
数据量大的情况下还是改成非动态查询语句吧
#21
create proc sp_kucunFB这样试试
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where (charindex(''['+@条件1+']'',rtrim(leibie))>0 or ['+@条件1+'] is null)
and (charindex(''['+@条件2+']'',rtrim(bianhaohp))>0 or ['+@条件2+'] is null)
and (charindex(''['+@条件3+']'',rtrim(yanse))>0 or ['+@条件3+'] is null)
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#22
还是不行呀,谁来点真功夫吧!
#23
还不行吗?
#24
我自己的环境下是没问题的,要不远程帮你看下。
#1
create proc sp_kucunFB%
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'
''
#2
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+' %''
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+' %''
#3
楼上的,你这是怎么回事?
#4
set @s=@s+' from vs_kucun where leibie like ''%'+@条件1+'%'''
不过没明白 from vs_kucun where leibie like ''%%'' and bianhaohp like ''%%'' and yanse like ''%%''
这么设置条件有什么意义
不过没明白 from vs_kucun where leibie like ''%%'' and bianhaohp like ''%%'' and yanse like ''%%''
这么设置条件有什么意义
#5
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'''%''
and bianhaohp like ''%'''+@条件2+'''%''
and yanse like ''%'''+@条件3+'''%''
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
go
试一下,未测。
#6
create proc sp_kucunFB
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+'''%''
and bianhaohp like ''%'''+@条件2+'''%''
and yanse like ''%'''+@条件3+'''%''
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s) --补充一下
go
#7
请问楼上的 htl258 ,我调用 exec sp_kucunfb '','','' 。只能返回商品名称与仓库名称。如果调用
exec sp_kucunfb '1','1','1' 时提示1附近有错误。请求帮忙解决!!
exec sp_kucunfb '1','1','1' 时提示1附近有错误。请求帮忙解决!!
#8
我这是想在调用时可以根据条件返回需要数据。
#9
create proc sp_kucunFB再看看
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'')
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where charindex('''+@条件1+''',leibie)>0
and charindex('''+@条件2+''',bianhaohp)>0
and charindex('''+@条件3+''',yanse)>0
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#10
yanse,bianhaohp,leibie字段类型是什么?
#11
create proc sp_kucunFB如还有问题就用这样。
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where (charindex('''+@条件1+''',rtrim(leibie))>0 or '+@条件1+' is null)
and (charindex('''+@条件2+''',rtrim(bianhaohp))>0 or '+@条件2+' is null)
and (charindex('''+@条件3+''',rtrim(yanse))>0 or '+@条件3+' is null)
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#12
这个调用“exec sp_kucunfb '','',''”时提示“在关键字 'is' 附近有语法错误。”
#13
这三个字段的属性都是 varchar(30), 这个没有问题!
#14
有新发现了,如果用这个过程的话调用“exec sp_kucunfb '100D','2','1'”可以返回数据了,每个条件都必须带条件查询,如有一个不带就返回不了数据。
#15
换个思路:
把多个条件设置为一个字符串变量即可
把多个条件设置为一个字符串变量即可
#16
麻烦你说得清楚一点嘛。
#17
如果用这个过程调用“exec sp_kucunfb 'NULL','NULL','NULL'”时可以返回所有数据,当调用“exec sp_kucunfb '9D','NULL','NULL'”或“exec sp_kucunfb 'NULL','NULL','9D'”时提示D排名无效,反正条件中只能用数字。反之用符号,字母、汉字都不行。怎样解决?
#18
exec sp_kucunfb 'where 1=1 and xxxx=''xxxx1'' and yyyy=''yyyy1''' ”
--条件设置组合成为一个字符串。包括条件列名一起写入。
而不要设置成分开的条件 exec sp_kucunfb 'NULL','NULL','NULL'
--条件设置组合成为一个字符串。包括条件列名一起写入。
而不要设置成分开的条件 exec sp_kucunfb 'NULL','NULL','NULL'
#19
有没有人为我解决?我想直接调用exec sp_kucunfb '','','',则查询全部,可以在随便在那个加或多个加条件(含字符)都通返数准确数据。为我解决者我定加分送。
#20
LZ的需求其实根本用不到动态查询语句, 这样写根本利用不上索引, 查询效率很低的
LZ的需求在报表按组合条件查询时非常常见的, 既然LZ已经写好了存储过程, 稍改动两点儿就可以满足要求了
在语句 select @条件1=isnull(@条件1,''),@条件2=isnull(@条件2,''),@条件3=isnull(@条件3,'') 之后添加
set @条件1=@条件1+'%'
set @条件2=@条件2+'%'
set @条件3=@条件3+'%'
补充未完成的语句set @s=@s+' from vs_kucun where leibie like ''%'''+@条件1+''' 如下
set @s=@s+' from vs_kucun where leibie like '''+ @条件1+''' and bianhaohp like '''+@条件2+''' and yanse like '''+@条件3+''' group by leibie,bianhaoHP,yanse order by leibie,bianhaoHP,yanse '
说明一下
1.按''查询或按NULL查询, 其实@条件变换成了%, 这样where 字段 like % 等价于 查询所有
2.如果@条件有值(如LeiBie), 变换成LeiBie%, 这样where 字段 like @条件 等价于 精确查询
依此类推, 这样的话, 再多的 组合条件 都可以如此照做
啰嗦一下
数据量大的情况下还是改成非动态查询语句吧
#21
create proc sp_kucunFB这样试试
@条件1 varchar(30),
@条件2 varchar(30),
@条件3 varchar(30)
as
set nocount on
declare @s varchar(8000)
set @s='select leibie+bianhaoHP+yanse as 商品名称 '
select @s=@s+',sum(case mingcheng_ck when '''+mingcheng_ck+''' then shuliang else 0 end) as ['+mingcheng_ck+']'
from (select distinct mingcheng_ck from vs_kucun) t
set @s=@s+' from vs_kucun
where (charindex(''['+@条件1+']'',rtrim(leibie))>0 or ['+@条件1+'] is null)
and (charindex(''['+@条件2+']'',rtrim(bianhaohp))>0 or ['+@条件2+'] is null)
and (charindex(''['+@条件3+']'',rtrim(yanse))>0 or ['+@条件3+'] is null)
group by leibie+bianhaoHP+yanse
order by leibie+bianhaoHP+yanse '
exec(@s)
go
#22
还是不行呀,谁来点真功夫吧!
#23
还不行吗?
#24
我自己的环境下是没问题的,要不远程帮你看下。