存储过程分割字符串查询
2014-05-30 14:10
369 查看
create proc [dbo].[Proc_RS_VRDPAC_MRC1_SelectPassCountByYearMonthtest]
@month varchar(100)
as
declare @m as int, @sql as nvarchar(1000),@n as int ,@id as nvarchar(100),@id1 as nvarchar(100),@num as int
set @m = CHARINDEX (',',@month)
set @n = 0
set @num=0
set @sql='select month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)) as months,b.BU,
sum(CONVERT(decimal(18, 0), a.IsPassed)) as pasman,
count(distinct a.stuid) -sum(CONVERT(decimal(18, 0), a.IsPassed)) as nopas
from VK_RDPAC_MRC1_Record as a
join Employee as b
on a.stuid=b.FID
where BU is not null and bu is not null and b.ProduceLine is not null'
WHILE @m>0
BEGIN
set @id=substring(@month,@n,@m-@n)
set @n=@m+1
set @m=CHARINDEX(',',@month,@n)
print @m
if @num=0
begin
if @m=0
begin
set @sql=@sql+' and CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
else
begin
set @sql=@sql+' and (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
end
else
begin
if @m=0
begin
set @sql=@sql+' or CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+'''))'
end
else
begin
set @sql=@sql+' or (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
end
set @num=@num+1
END
set @sql=@sql+' group by month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)),b.BU order by b.BU'
exec sp_executesql @sql
@month varchar(100)
as
declare @m as int, @sql as nvarchar(1000),@n as int ,@id as nvarchar(100),@id1 as nvarchar(100),@num as int
set @m = CHARINDEX (',',@month)
set @n = 0
set @num=0
set @sql='select month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)) as months,b.BU,
sum(CONVERT(decimal(18, 0), a.IsPassed)) as pasman,
count(distinct a.stuid) -sum(CONVERT(decimal(18, 0), a.IsPassed)) as nopas
from VK_RDPAC_MRC1_Record as a
join Employee as b
on a.stuid=b.FID
where BU is not null and bu is not null and b.ProduceLine is not null'
WHILE @m>0
BEGIN
set @id=substring(@month,@n,@m-@n)
set @n=@m+1
set @m=CHARINDEX(',',@month,@n)
print @m
if @num=0
begin
if @m=0
begin
set @sql=@sql+' and CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
else
begin
set @sql=@sql+' and (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
end
else
begin
if @m=0
begin
set @sql=@sql+' or CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+'''))'
end
else
begin
set @sql=@sql+' or (CONVERT(varchar(7),a.ExamingOn,120) in ('''+@id+''')'
end
end
set @num=@num+1
END
set @sql=@sql+' group by month(dbo.fn_PrimeObjects_GetLocalTime(a.ExamingOn)),b.BU order by b.BU'
exec sp_executesql @sql
相关文章推荐
- SQL 分割字符串和通用分页查询存储过程
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 利用MySQL存储过程分割字符串
- 利用MySQL存储过程分割字符串
- mysql 通过存储过程分割字符串为数组
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 登陆,分割字符串存储过程
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 存储过程接收多个值(用分隔符号的字符串参数)的用in查询
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 利用MySQL存储过程分割字符串
- 查询sqlserver数据库视图、存储过程等包含特定的字符串
- 查询所有的存储过程哪些中包含某个字符串
- 自定义分割存储过程 SP_Split,将一个字符串分割成一个表
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- 查询所有的存储过程哪些中包含某个字符串
- SQLServer 存储过程中不拼接SQL字符串实现多条件查询
- SQLServer2008运用存储过程拆分字符串并查询结果并把结果存入临时表中。
- 利用MySQL存储过程分割字符串