创建存储过程

create table cunchu
(
id int,
age int,
score int,
)
insert into cunchu values
(1,20,80)
insert into cunchu
select 2,25,85
union
select 3,30,90
union
select 4,35,95

select*from cunchu

go
create procedure p_omg
as
select avg(score) as ‘平均分噢’from cunchu—–普通t_sql语句
select * from cunchu where age>25————–普通t_sql语句

exec p_omg

go
create procedure p_omgd
@zdy int
as
select avg(score) as ‘平均分噢’from cunchu
select * from cunchu where age>@zdy

exec p_omgd @zdy=25
exec p_omgd 25————————-如果变量多要注意顺序

go
create procedure p_omgdo
@zdy int = 30
as
select avg(score) as ‘平均分噢’from cunchu
select * from cunchu where age>@zdy

exec p_omgdo @zdy=25——————-赋值大于给定的
exec p_omgdo 25————————赋值大于给定的
exec p_omgdo—————————不加说明就是给定的30

if exists (select *from sysobjects where name =’bianhao’)
drop table bianhao
create table bianhao
(orderID varchar(20),
type varchar(20),
price int,
date datetime)
insert into bianhao
select ‘2018-5-25-001’,’华为’,2000,’2018-5-25 00:00:00’——00:00:00.000自动转换
union
select ‘2018-5-25-002’,’苹果’,4000,’2018-5-25 00:00:00’
union
select ‘2018-5-26-001’,’三星’,6000,’2018-5-26 00:00:00’
union
select ‘2015-5-27-001’,’oppo’,3000,’2015-5-27 00:00:00’

select*from bianhao

go
create procedure p_omgdom————动态添加存储过程——增改`查
@orderID varchar(20)=null,———–为null就可以不填数据
@type varchar(20)=null,
@price int=null,
@date datetime=null
AS
if(@orderID is null)
begin
print ‘orderID不能为空’
return
end
insert into bianhao values
(@orderID,isnull(@type,’华为’),isnull(@price,3000),@date)——@xx相当于一个中介

exec p_omgdom @orderID=12345
exec p_omgdom 123456,’小米’,10000

select*from bianhao

go
create procedure p_omgdomg——-动态查询存储过程select换成delete就是删除语句
@orderID varchar(20)=null,
@type varchar(20)=null,
@price int=null,
@date datetime=null
AS
select * from bianhao
where(@orderID is null or orderID=@orderID)
and(@type is null or type=@type)
and(@price is null or price=@price)
and(@date is null or date=@date)

exec p_omgdomg @type=小米

select*from bianhao where type =’小米’

go
alter procedure p_omgdomgd——-动态修改存储过程
@orderID varchar(20)=null,
@type varchar(20)=null,
@price int=null,
@date datetime=null,

@typeNew varchar(20)=null,
@priceNew int=null,
@dateNew datetime=null
as
update bianhao
set type=isnull(@typeNew,type),———等于新值如为空等于旧值(理解)
price=isnull(@priceNew,price),
date=isnull(@dateNew,date)
where(@orderID is null or orderID=@orderID)
and(@type is null or type=@type)
and(@price is null or price=@price)
and(@date is null or date=@date)

exec p_omgdomgd @orderID=’12345’,@priceNew=2989,@typeNew=’苹果’

select * from bianhao

select * from(
select top 4 * from(
select top 6 * from bianhao——-取后四条
order by date)as A
order by A. date desc)as B
order by B.date

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值