数据库10(代码相关语句)

while循环

declare @avgprice numeric(10,2)

set @avgprice=(select avg(price)from titles)

//自定义参数
while @avgprice<10

//循环条件
begin

update titles

set price=price*1.1
end

//循环语句操作,当avgprice<10,所有price都加0.1

case语句

查询authors表,当state为各种情况时,为新加的一列赋上不同的值。功能类似于C语言中的switch语句

select *, //加逗号
case state

when'CA' then 'California'

when 'KS' then 'kaness'

when 'MI' then 'Michigan'

when 'IN' then 'India'

when 'MD' then 'Maryland'

when 'UT' then 'Utah'

when 'HN' then 'hunan'

else 'Other'
end as statename

from authors

存储过程

创建一个存储过程名为searchbook,自定义变量后可以查询符合条件的对象信息,使用自定义变量可以让用户自定义查询条件。
create procedure searchbook

@inputlow float,

@inputhigh float

as

begin

select title,price,pub_name

from titles inner join publishers

on(publishers.pub_id=titles.pub_id)
where price>@inputlow and price<@inputhigh

end

//查询语句:
exec searchbook 10.1, 20.2

使用存储过程后,用户可以用很简单的查询语句传入参数,查询到自己想要的结果

--存储过程优点,3-4条

--1.简化代码

--实现共享,提高开发速度

--隔离复杂性

--简化软件分发

--提高运行速度

写一个存储过程,查询在某个价格区间的书籍

如果该价格区间没有任何书,则给出销量最高的3本书

create procedure searchbook

@inputlow float,

@inputhigh float //全局变量

as

begin

declare @num int //局部变量

set @num=(select count(*) from titles

where price>@inputlow and price<@inputhigh)

//if语句

if @num>0

begin

select title,price,pub_name

from titles inner join publishers

on(publishers.pub_id=titles.pub_id)

where price>@inputlow and price<@inputhigh

end

//else语句

else

begin

select top 3 title,price,pub_name

from titles inner join publishers

on(publishers.pub_id=titles.pub_id)

order by ytd_sales desc

end

end

//查询语句

exec searchbook 30,50.1

output参数

设一个全局变量out为OUTPUT,在存储过程中定义它的数据,

在使用查询语句时,用户可以自定义变量,将OUTPUT的值赋给该变量,用select语句得到输出

//定义存储结构

create procedure searchbook1

@inputlow int,

@inputhigh int,

@out int OUTPUT //定义全局变量为OUTPUT

as

begin

select title,price,pub_name

from titles inner join publishers

on(publishers.pub_id=titles.pub_id)

where price>@inputlow and price<@inputhigh

//定义OUTPUT的数据

set @out=(select count(*) from titles

where price>@inputlow and price<@inputhigh)

end

//查询语句

declare @myout int

exec searchbook1 30,50, @myout OUTPUT

select @myout

可能报错

1.超出了存储过程、函数、触发器或视图的最大嵌套层数(最大层数为 32)

可能是触发器的影响,在 titles 或者 publishers 表上存在触发器,执行 SELECT 语句时,触发器会被触发,导致嵌套调用。

解决:

-- 禁用 titles 表上的所有触发器

DISABLE TRIGGER ALL ON titles;

-- 禁用 publishers 表上的所有触发器

DISABLE TRIGGER ALL ON publishers;

2.CREATE/ALTER PROCEDURE'必须是查询批次中的第一个语句

在 SQL Server 里,CREATE PROCEDURE 语句需要作为一个批次里的首个语句,它的前面不应有其他语句

如果有其他语句,可使用 GO 关键字来划分不同的批次

如报错1需要增加语句

可如下:

//添加语句

DISABLE TRIGGER ALL ON titles;

DISABLE TRIGGER ALL ON publishers;
GO

//原语句

create procedure searchbook1

@inputlow int,

@inputhigh int,

@out int OUTPUT

as

begin

select title,price,pub_name

from titles inner join publishers

on(publishers.pub_id=titles.pub_id)

where price>@inputlow and price<@inputhigh

set @out=(select count(*) from titles

where price>@inputlow and price<@inputhigh)
end; //加分号
GO

declare @myout int

exec searchbook1 30,50, @myout OUTPUT

select @myout

相关推荐
霖霖总总9 分钟前
[小技巧19]MySQL 权限管理全指南:用户、角色、授权与安全实践
数据库·mysql·安全
heartbeat..5 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
麦聪聊数据7 小时前
MySQL并发与锁:从“防止超卖”到排查“死锁”
数据库·sql·mysql
AC赳赳老秦7 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
YMatrix 官方技术社区8 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix
辞砚技术录9 小时前
MySQL面试题——索引2nd
数据库·mysql·面试
linweidong9 小时前
C++thread pool(线程池)设计应关注哪些扩展性问题?
java·数据库·c++
欧亚学术10 小时前
突发!刚刚新增17本期刊被剔除!
数据库·论文·sci·期刊·博士·scopus·发表
黑白极客11 小时前
怎么给字符串字段加索引?日志系统 一条更新语句是怎么执行的
java·数据库·sql·mysql·引擎
大厂技术总监下海11 小时前
数据湖加速、实时数仓、统一查询层:Apache Doris 如何成为现代数据架构的“高性能中枢”?
大数据·数据库·算法·apache