MsSqlServer
use MyItcast --求1--100的和 declare @sum int=0,@number int=1; while(@number=100) begin set @sum=@sum+@number; set @number=@number+1; end select @sum --求1--100之间所有基数和 declare @sum int=0,@num int=0; while(@num=100) begin if(@num%20) b
use MyItcast--求1--100的和
declare @sum int=0,@number int=1;
while(@number
begin
set @sum=@sum+@number;
set @number=@number+1;
end
select @sum
--求1--100之间所有基数和
declare @sum int=0,@num int=0;
while(@num
begin
if(@num%20)
begin
set @sum=@sum+@num;
end
set @num=@num+1;
end
print @sum
----事物
use nonononodelete
select * from bank --该表有一个约束 每个账号里 不能少于10元钱
update bank set balance=balance-1000 where cId='0001'
update bank set balance=balance+1000 where cId='0002'
--执行这两行代码会报这个错误
--消息 547,级别 16,状态 0,第 1 行
--UPDATE 语句与 CHECK 约束"CH_balance"冲突。该冲突发生于数据库"nonononodelete",表"dbo.bank", column 'balance'。
--语句已终止。
--解决办法 使用事务处理
begin transaction --开始一个事务
declare @sumError int=0;
update bank set balance=balance-1000 where cId='0001'
set @sumError+=@@ERROR
update bank set balance=balance+1000 where cId='0002'
set @sumError+=@@ERROR;
if(@sumError0)
begin
--失败了
rollback transaction;
end
else
begin
--成功了
commit transaction
end
select * from bank
--使用事务
---存储过程
exec sp_databases --数据库中所有的数据库
exec sp_tables --数据库中所有的表
exec sp_columns tblStudent --tblStudent 这个表中所有的列
exec sp_help
exec sp_helptext sp_databases
--创建一个存储过程求两个数的和
create proc usp_TwoNumbersAdd
@num1 int,
@num2 int
as
begin
select @num1+@num2
end
--第一种传参数的方法
exec usp_TwoNumbersAdd 1,5
declare @num int=10,@numOne int=20
--第二种传参数的方法
exec usp_TwoNumbersAdd @num1=@num,@num2=@numOne
drop proc usp_TwoNumbersAdd --删除存储过程
--创建一个存储过程计算连个数的差
create proc usp_TwoNumberSub
@numberOne int=20,
@numberTwo int=10
as
begin
select @numberOne-@numberTwo
end
drop proc usp_TwoNumberSub
exec usp_TwoNumberSub 10,20
drop proc usp_TwoNumberSub
--创建一个带输出参数的存储过程
create proc usp_TwoNumberSub
@numberOne int,
@numberTwo int,
@Result int output
as
begin
set @Result=@numberOne-@numberTwo;
end
declare @result int
exec usp_TwoNumberSub 20,10,@Result output --执行存储过程
print @result
--模糊查询 --存储过程 用户传入 张,和年龄 >20返回来有多少条数据 并把这些数据显示出来
create proc usp_myselectstuByNameandAge
@name nvarchar(10),--名字
@age int,--年龄
@count int output--条数
as
begin
--条数
set @count=(select COUNT(*) from tblstudent where tsname like @name+'%' and tsage>@age )
select * from tblstudent where tsname like @name+'%' and tsage>@age
end
declare @ct int
exec usp_myselectstuByNameandAge '张',20, @ct output
select @ct
select * from tblscore
create proc usp_tblScore
@scoreLines int,
@addScore int=2,
@count int
as
begin
set @count=0
--总人数
declare @countPerson int=(select COUNT(*) from TblScore)
--不及格的人数
declare @bjgPerson int=(select COUNT(*) from tblScore where tmath
while(@bjgPerson>@count/2)
begin
update tblscore set tmath= tmath +@addScore;
set @bjgPerson=(select COUNT(*) from tblScore where tmath
set @count=@count+1;
end
end
declare @cou int=0
exec usp_tblScore 120,2, @cou
select @cou
select * from tblscore
select COUNT(*) from tblscore
--创建一个存储过程如果不及格的人数小于一半每个同学提分
select * from TblScore
create proc usp_TblScoreLine
@scoreLine int,
@addScore int,
@counts int output
as
begin
--没及格的人数
declare @countMeiPersons int=(select COUNT(*) from TblScore where tEnglish
--总的人数
declare @countPersons int=(select COUNT(*) from TblScore)
while(@countMeiPersons>@countPersons/2)
begin
update TblScore set tEnglish=tEnglish+@addScore;
set @countMeiPersons=(select COUNT(*) from TblScore where tEnglish
set @counts=@counts+1;
end
end
select * from tblscore --tblscore 表
declare @n int
exec usp_TblScoreLine 155,1,@n output
select @n;
select count(*)from tblscore where tenglish=155
use nonononodelete
select * from TblStudent
--pagecount 总的页数
--count
--页数 5 每页显示几条
--分页的sql语句
declare @count int=(select COUNT(*) from TblStudent)
declare @PageCount int=(CEILING((select COUNT(*) from tblstudent)*1.0/@count))
select * from (select 编号=ROW_NUMBER() over(order by tSid),* from TblStudent ) as t where t.编号
between and
--分页的存储过程
--分页的存储过程
create proc usp_TblStudent
@page int, ---页数
@pageCount int,--条数
@sumPage int output--总页数
as
begin
set @sumPage=Ceiling((select count(*) from TblStudent)/@pageCount*1.0) --总页数
select * from
(select 编号=ROW_NUMBER() over(order by tsid),* from tblstudent)as tstu
where tstu.编号 between (@page-1)*@pageCount+1 and @page*@pageCount
end
declare @c int
exec usp_TblStudent 2,3,@c output
select @c
select top 1 * into newStu from TblStudent
select * from newStu
delete from TblStudent where TSId=1
select * from newStu
insert into TblStudent(TSName,TSGender,TSAddress,TSPhone,TSAge,TSBirthday,TSCardId,TClassId) select TSName,TSGender,TSAddress,TSPhone,TSAge,TSBirthday,TSCardId,TClassId from newStu ---一次性插入多条数据
select * from newStu
select * from TblStudent
--创建一个删除的触发器
create trigger tr_TblStudent on TblStudent
after delete
as
begin
insert into newStu select * from deleted
end
--回出现的错误 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'newStu'中的标识列指定显式值。
--解决办法 表设计 表示 改成否Ok
select * from newStu
delete from newStu where TSId=1
delete from TblStudent where TSId=2
select * from newStu
select * from newStu
select * from TblStudent
select * from newStu
insert into TblStudent(TSName,TSGender,TSAddress,TSPhone,TSAge,TSBirthday,TSCardId,TClassId) select TSName,TSGender,TSAddress,TSPhone,TSAge,TSBirthday,TSCardId,TClassId from newStu
create trigger tr_TblStudent
select * from tblStudent

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文探讨了Docker中的优化MySQL内存使用量。 它讨论了监视技术(Docker统计,性能架构,外部工具)和配置策略。 其中包括Docker内存限制,交换和cgroups

本文介绍了MySQL的“无法打开共享库”错误。 该问题源于MySQL无法找到必要的共享库(.SO/.DLL文件)。解决方案涉及通过系统软件包M验证库安装

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

本文比较使用/不使用PhpMyAdmin的Podman容器直接在Linux上安装MySQL。 它详细介绍了每种方法的安装步骤,强调了Podman在孤立,可移植性和可重复性方面的优势,还

本文提供了SQLite的全面概述,SQLite是一个独立的,无服务器的关系数据库。 它详细介绍了SQLite的优势(简单,可移植性,易用性)和缺点(并发限制,可伸缩性挑战)。 c

本指南展示了使用自制在MacOS上安装和管理多个MySQL版本。 它强调使用自制装置隔离安装,以防止冲突。 本文详细详细介绍了安装,起始/停止服务和最佳PRA

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]
