数据库存储过程的示例分析
短信预约 -IT技能 免费直播动态提醒
这篇文章给大家分享的是有关数据库存储过程的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
select * from sysobjects
select * from syscomments
go
select * from syscomments
where id = object_id('custorderhist')
go
select name,text
from sysobjects inner join syscomments
on sysobjects.id = syscomments.id
where sysobjects.name = 'custorderhist'
go
sp_helptext sp_helptext
go
use northwind
go
exec sp_help custorderhist
exec sp_helptext custorderhist
exec sp_depends custorderhist
exec sp_stored_procedures 'custorderhist'
-- -- -- select top 1 * from products
-- -- -- select top 1 * from orders
-- -- -- select top 1 * from [order details]
alter proc sp_qry_salesdetails
as
select a.productid as 商品编号,a.productname as 商品名称,b.unitprice as 数量,b.quantity as 价格,
b.unitprice*b.quantity as 金额,c.requireddate as 销售时间
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid
go
print '测试'
execute sp_qry_salesdetails
--递归算法
--视图 存储过程 函数
alter view v_qry_salesdetails
as
select a.productid as 商品编号,a.productname as 商品名称,b.unitprice as 数量,b.quantity as 价格,
b.unitprice*b.quantity as 金额,c.requireddate as 销售时间
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid
print '测试'
select * from v_qry_salesdetails
create procedure sp1
as
select * from customers
exec sp1
alter procedure sp1
as
select * from customers
alter procedure sp1
with recompile
as
select * from customers
sp_recompile sp1
--加密存储过程 with encryption
select objectproperty(object_id('sp_qry_salesdetails'), 'isencrypted')
use northwind
go
create proc dbo.sp_dropproc
as
select 'northwind.dbo.sp_dropproc'
go
exec northwind.dbo.sp_dropproc
go
use master
go
create proc dbo.sp_dropproc
as
select 'master.dbo.sp_dropproc'
go
exec master.dbo.sp_dropproc
go
use northwind
go
drop proc sp_dropproc
go
exec sp_dropproc
exec master.dbo.sp_dropproc
create proc qry_salesdetails @y int,@m int --varchar(10)
as
select a.productid as 商品编号,a.productname as 商品名称,b.unitprice as 数量,b.quantity as 价格,b.unitprice*b.quantity as 金额,c.requireddate as 销售时间
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid
--where convert(varchar(2),month(c.requireddate)) = @m
where year(c.requireddate) = @y and month(c.requireddate) = @m
go
exec qry_salesdetails 1996,9
exec qry_salesdetails 9,1996
exec qry_salesdetails @m=9,@y=1996
exec qry_salesdetails @y=1996,@m=9
go
use northwind
go
--创建存储过程
-- drop proc qry_showorders
create proc qry_showorders @custid nchar(5)
with encryption --加密
as
if @custid is null
-- begin
-- print '提供了不正确的参数'
-- return
-- end
select *
from orders od inner join [order details] oddt
on od.orderid = oddt.orderid
where shippeddate >='1996-07-01' and shippeddate <='1997-07-01'
and od.customerid = @custid
go
--调用、检验刚刚创建的存储过程
exec qry_showorders @custid = 'vinet'
exec qry_showorders null
go
--检查是否已经被加密
exec sp_helptext qry_showorders
create proc testoutput @a varchar(10) output
as
select @a = 100
go
declare @b varchar(10)
--exec testoutput @b output
exec testoutput @a=@b output
select @b
--error
create proc sum_money @count money, @unitprice money
as
select @count*@unitprice
go
declare @sum_temp money ,@sum_temp2 money
set @sum_temp2 = exec sum_money @count= 1.1,@unitprice = 2.2
create proc sum_money @count money, @unitprice money ,@sum money output
as
set @sum = @count*@unitprice
go
declare @sum_temp money ,@sum_temp2 money
exec sum_money @count= 1.1,@unitprice = 2.2,@sum = @sum_temp output
set @sum_temp2= @sum_temp*100
select @sum_temp2
create proc test_output @in nvarchar(100),@out nvarchar(100) output
as
print 'i''m @in ' + @in
set @out = @in
print 'i''m @out '+@out
go
declare @i nvarchar(100),@o nvarchar(100)
set @i = '让我们一起来测试'
exec test_output @in = @i,@out = @o output
select @o
--return 主要用来进行错误处理
create proc testreturn @a int
as
if @a<0
begin
return(-1)
end
else if @a = 0
begin
return(0)
end
else
begin
return(1)
end
go
declare @rtn int
exec @rtn = testreturn @a=-100
select @rtn
go
select @@error
go
select 'a'+1
go
select @@error
select error, description from master.dbo.sysmessages
where error = 245
create proc testerror
as
select 'a'+1
go
exec testerror
go
create proc testerror
as
declare @e int,@a int ,@b int
set @e = 0
set @a = 1
set @b = 0
select @a/@b
if @@error<>0
begin
print '有错误'
set @e = @@error
end
return @e
go
declare @er int
exec @er = testerror
select @er
select @@rowcount
select * from customers
select @@rowcount
create proc testreturn @a int
as
if @a is null
begin
return(100)
end
else if @a<0
begin
return(-1)
end
else if @a = 0
begin
return(0)
end
else
begin
return(1)
end
use master
go
create sp_test
as
select '现在是master数据库'
go
use northwind
go
create sp_test
as
select '现在是northwind数据库'
go
exec sp_test
exec master.dbo.sp_test
drop sp_test
create proc sp1_test
as
select '这是master'
go
use northwind
go
create proc sp1_test
as
select '这是northwind'
exec sp1_test
drop proc sp1_test
--按契约编程
use northwind
go
create proc testdelay
as
select * from tbldelay
go
exec testdelay
create proc groupedproc;1
as
select 'groupedproc;1 '
go
create proc groupedproc;2
as
select 'groupedproc;2 '
go
sp_helptext groupedproc
go
exec groupedproc;1
go
exec groupedproc;2
go
exec groupedproc
go
drop proc groupedproc
create proc a
as
select 'a'
go
create proc b
as
select 'b'
exec a
go
exec b
-- -- drop proc testdefault
create proc testdefault @a int,@b int=2
as
select @a,@b
go
exec testdefault 1
go
exec testdefault @a=1
exec testdefault 1,100
use northwind
go
create table start
(
dt datetime
)
go
use master
go
create proc autostart
as
insert into northwind.dbo.start
values(getdate())
go
--设置为自动运行
execute sp_procoption
@procname = autostart,
@optionname = startup,
@optionvalue = true
go
use master
--判断是否自动运行
select objectproperty(object_id('autostart'), 'execisstartup')
go
select * from northwind.dbo.start
--停止自动运行
execute sp_procoption
@procname = autostart,
@optionname = startup,
@optionvalue = false
execute sp_configure
@configname = 'scan for startup procs', @configvalue = 0
reconfigure
go
-- -- --
-- exec xp_dirtree "D:\"
-- -- --
-- -- -- ------msg 15281, level 16, state 1, procedure xp_cmdshell, line 1
-- -- -- ------sql server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. a system administrator can enable the use of 'xp_cmdshell' by using sp_configure. for more information about enabling 'xp_cmdshell', see "surface area configuration" in sql server books online.
---exec xp_cmdshell "dir *.exe"
-- -- --
-- -- -- exec xp_cmdshell tree
-- -- --
select
insert
update
delete
create proc insertcust @custid nchar(5),
@cmpnm nvarchar(40),
@cntnm nvarchar(30),
@cntttl nvarchar(30),
@addr nvarchar(60),
@city nvarchar(15),
@rg nvarchar(15),
@pscd nvarchar(10),
@cntry nvarchar(15),
@phone nvarchar(24),
@fax nvarchar(24)
as
--业务逻辑
insert into customers(customerid,companyname,contactname,contacttitle,
address,city,region,postalcode,country,phone,fax)
values(@custid,@cmpnm,@cntnm,@cntttl,
@addr,@city,@rg,@pscd,@cntry,@phone,@fax)
go
exec insertcust @custid='abcd',@cmpnm='abc company',@cntnm='anyone',@cntttl='mr.',@addr='anywhere',
@city='shanghai',@rg='huangpu',@pscd='200000',@cntry='chian',@phone='021-88888888',@fax='021-66666666'
go
--简单实现
create proc createcustid @id nchar(5) output
as
--自动产生客户ID
create proc insertcust
@cmpnm nvarchar(40),
@cntnm nvarchar(30),
@cntttl nvarchar(30),
@addr nvarchar(60),
@city nvarchar(15),
@rg nvarchar(15),
@pscd nvarchar(10),
@cntry nvarchar(15),
@phone nvarchar(24),
@fax nvarchar(24)
as
declare @id nchar(t5)
exec createcustid @id output
insert into customers(customerid,companyname,contactname,contacttitle,address,city,region,postalcode,country,phone,fax)
values(@id,@cmpnm,@cntnm,@cntttl,@addr,@city,@rg,@pscd,@cntry,@phone,@fax)
go
------------------------------------------------------------------------------------------------------------------------
set nocount off
select 'a'
go
-- -- --
set nocount on
select 'a'
create proc qry_salesdetails @no int = -1,@start char(10),@end char(10)
as
select a.productid as 商品编号,a.productname as 商品名称,b.unitprice as 数量,b.quantity as 价格,b.unitprice*b.quantity as 金额,c.requireddate as 销售时间
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid
where a.productid= @no and c.requireddate<=@end
and c.requireddate>=@start
go
exec qry_salesdetails 6,'1996-01-01','1997-01-01'
alter proc qry_salesdetails @no int = -1,@start char(10),@end char(10)
as
declare @sql varchar(4000)
set @sql = 'select a.productid as 商品编号,a.productname as 商品名称,
b.unitprice as 数量,b.quantity as 价格,b.unitprice*b.quantity as 金额,
c.requireddate as 销售时间
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid where 1=1 '
if @no is not null
set @sql = @sql + ' and a.productid = '+convert(varchar(10),@no)
if @start is not null and @end is not null
set @sql = @sql + ' and c.requireddate >= '''+ @start+''''
+ ' and c.requireddate <= '''+ @end+''''
--print @sql
exec(@sql)
print ''''
go
exec qry_salesdetails @end=null,@start=null
exec qry_salesdetails @no=35,@end=null,@start=null
exec qry_salesdetails @no=null,@end='1997-07-01',@start='1996-07-01'
exec qry_salesdetails @no=38,@end='1997-07-01',@start='1996-07-01'
sp_stored_procedures qry_salesdetails
drop table tempdb..#temp
go
create table #temp
(
商品编号 varchar(100),
商品名称 varchar(100),
金额 money,
销售时间 datetime,
排序 int
)
insert into #temp
select a.productid as 商品编号,a.productname as 商品名称,
b.unitprice*b.quantity as 金额,c.requireddate as 销售时间,
month(c.requireddate)
from [order details] as b join products as a
on b.productid=a.productid
join orders as c
on b.orderid=c.orderid
where year(c.requireddate) = 1996
insert into #temp(商品编号,金额,排序)
select '月汇总',sum(金额),month(销售时间)
from #temp
group by year(销售时间),month(销售时间)
insert into #temp(商品编号,金额,排序)
select '年汇总',sum(金额),12
from #temp
where 销售时间 is not null
select * from #temp
order by 排序 ,商品名称 desc
select * from #temp
drop table tempdb..#temp
感谢各位的阅读!关于“数据库存储过程的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341