SQLServer存储过程

阅读更多
create proc [dbo].[PhoneRec] (@haoma varchar(32),@ser varchar(16) output)
as
if @haoma='' and @ser=''
begin
select a.serverid,c.serverip,a.filename,b.callernumber,b.callednumber,b.createtime,b.releasetime
from dbo.AGENTRECFILE as a
inner join dbo.COLOG as b
on a.coid=b.coid
inner join dbo.DEVICECFG as c
on a.serverid=c.serverid
order by b.createtime asc
end
else if @haoma=''
begin
select a.serverid,c.serverip,a.filename,b.callernumber,b.callednumber,b.createtime,b.releasetime
from dbo.AGENTRECFILE as a
inner join dbo.COLOG as b
on a.coid=b.coid
inner join dbo.DEVICECFG as c
on c.serverid=@ser and a.serverid=c.serverid 
order by b.createtime asc
end
else if @ser=''
begin
select a.serverid,c.serverip,a.filename,b.callernumber,b.callednumber,b.createtime,b.releasetime
from dbo.AGENTRECFILE as a
inner join dbo.COLOG as b
on a.coid=b.coid
inner join dbo.DEVICECFG as c
on a.serverid=c.serverid
where b.callernumber=@haoma or b.callednumber=@haoma
order by b.createtime asc
end
else
begin
select a.serverid,c.serverip,a.filename,b.callernumber,b.callednumber,b.createtime,b.releasetime
from dbo.AGENTRECFILE as a
inner join dbo.COLOG as b
on a.coid=b.coid
inner join dbo.DEVICECFG as c
on c.serverid=@ser and a.serverid=c.serverid
where b.callernumber=@haoma or b.callednumber=@haoma
order by b.createtime asc
end


drop proc PhoneRec

exec PhoneRec 'xxx','yyy'


inner join group by
select d.serverip,a.数量 from devicecfg as d
inner join (
select serverid, count(recordid) as 数量
from agentrecfile
where createtime >='2014-12-05 10:30:00'
and createtime <='2014-12-05 15:00:00'
group by serverid
)as a
on a.serverid = d.serverid

备份表
SELECT * INTO [备份表] FROM [源表]

select d.serverid,count(e.extensionnumber) as co,d.serverip,d.serverdesc ,count(e.extensionnumber) from devicecfg as d
inner join EXTENSIONCFG as e
on d.serverid = e.recserverid
GROUP BY d.serverid,d.serverip,d.serverdesc
order by d.serverid

你可能感兴趣的:(SQL,Server)