T-SQL(SQL Sever) 简单语句实例

说明

此部分只注重语句的语法,使用场景和合理性不考虑在内。

此例子是主要是对一个货物表的价格进行操作。


创建表

//创建表
create table goods(
	gid int primary key,
	gname varchar(10),
    gprice float
)


插入数据

//插入数据
insert into goods values(1,'apple',15)
insert into goods values(2,'banana',10)
insert into goods values(3,'orange',5)


查询数据

select * from goods
查询价格上调 50% 的价格
select gname,gprice,gprice*1.5 as riceprice from goods

定义函数

此函数的作用很简单, 就是和上面价格上调的功能类似。根据上调比例计算新价格

create function riceprice(@price float,@ratio float) 
  returns float 
as
  begin
  declare @newprice float
  set @newprice = @price*@ratio
  return(@newprice);
  end
 调用这个函数:

select gname,gprice,dbo.riceprice(gprice,1.4) as riceprice from goods


定义触发器

作用很简单,在insert 新的货物后,打印货品价格+1 后的价格

create trigger ricepricetrigger 
  on goods for insert
as
  declare @id int,@price float
  select @id=gid,@price =gprice+1  from inserted
  print @price
在执行insert 语句之后会执行
insert into goods values(4,'pear',20)


定义存储过程

作用打印所有价格的汇总:

create proc ricepriceproc
  @ratio float,
  @totalprice float output
as
  select @totalprice = sum(gprice*@ratio) from goods

调用如下:

begin
declare @tprice float
exec ricepriceproc 1.2,@tprice output
print @tprice
end 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oscar999

送以玫瑰,手留余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值