use TestDataBase;
go

-- 派生表
-- 第3页,每页5条数据
select *
from
(select ROW_NUMBER() over(order by stuId) as num, * from Student) as t
where
t.num between (3 - 1) * 5 + 1 and 3 * 5;
-- 公用表表达式CTE
with t
as
(
select ROW_NUMBER() over(order by stuId) as num, * from Student
)
select * from t where num between (3-1) * 5 + 1 and 3 * 5;
-- 视图
go
create view vw_Person
as
select ROW_NUMBER() over(order by stuId) as num, * from Student;
go

select * from vw_Person where num between 11 and 15;

---------------------------------------------------
update vw_Person set stuName = '米米' where stuId = 17;

update Student set stuName = '米尔' where stuId = 17;

----------------
-- 使用内联表值函数,实现分页
-- 显示的第几页@pageIndex as int
-- 每页显示多少条@pageSize as int
go
create function fn_FenYe
(@pageIndex as int, @pageSize as int)
returns table
as
return
select * from vw_Person
where num between
(@pageIndex - 1) * @pageSize + 1
and
@pageIndex * @pageSize;
go
-----------------
select * from fn_FenYe(1, 13);

select * from Student where stuName='濮阳语儿';

---------------------------------------------
-- 查询濮阳语儿的分数
declare @id int;
--set @id = 5723;
-- set @id = (select stuId from Student where stuName='濮阳语儿');
select @id = stuId from Student where stuName='濮阳语儿';

select * from Student where stuId = @id;
select * from Score where stuId = @id;

--
select stuName from Student group by stuName having COUNT(*) = 2;
select * from Student where stuName = '边绍辉';
go

declare @id int;
-- set @id = (select stuId from Student where stuName='边绍辉');
select @id = stuId from Student where stuName='边绍辉'
select @id;
-- 14207
-- 29753
go

declare @name nvarchar(10);-- = N'牛亮亮';
select @name + 1;

select @@ERROR;

select @@VERSION;

print @@version;

select * from MyTest.dbo.logintbl;
insert into MyTest.dbo.logintbl(uid, pwd) values('亮亮亮', 'llllll');

select @@IDENTITY;

-------------------

-- if else
go
declare @num int;
-- set @num = 11;

if(@num % 2 = 0)
begin
print '这是个偶数';
end
else if(@num % 2 = 1)
begin
print '这是个奇数';
end
else
begin
print '不知道';
end
go

-- 判断一个数是不是为null
declare @num int = 10;

if(@num is null)
begin
print '是null';
end
else
begin
print '是数字';
end

-- 双引号是界定符
select 1 as "n u m";
select 1 as num;

----------------
go

declare @num int;
declare @sum int;
set @num = 1;
set @sum = 0;
while(@num <= 100)
begin
set @sum = @sum + @num;
set @num = @num + 1;
end
select @sum;
go
-- 求1到100中偶数的和
declare @i int;
declare @sum int;
set @i = 1;
set @sum = 0;

while(@i <= 100)
begin
if(@i % 2 = 0)
begin
set @sum = @sum + @i;
end
set @i = @i + 1;
end
print @sum;

---------------------
select * from TestDataBase.dbo.Student;

delete from TestDataBase.dbo.Student where stuId = 2;

-------------------------
use MyTest;

--事务
create table bank
(
cId char(4) primary key,
balance money, --余额
)

alter table bank
add constraint CH_balance check(balance >=10)

go
--delete from bank
insert into bank values('0001',1000)
insert into bank values('0002',10)
go

update bank set balance=balance - 1000 where cid='0001';
update bank set balance=balance - 1000 where cid='0002';

select * from bank;

begin transaction
update bank set balance=balance - 1000 where cid='0001';
update bank set balance=balance + 1000 where cid='0002';
-- 回滚
-- rollback;
-- 提交
commit;

---------------------------
-- 将分数的近18万条数据删除

use TestDataBase;

select * from Score;

begin transaction
delete from Score;

rollback;
go
--------------

-- 银行问题

begin transaction
declare @myError int;
update bank set balance=balance + 900 where cid='0001';
set @myError = @@ERROR;
update bank set balance=balance - 900 where cid='0002';
set @myError = @myError + @@ERROR;
if(@myError > 0)
begin
rollback;
end
else
begin
commit;
end

select * from bank;

------------------------------
use MyTest;
exec sp_rename bank, 银行;

select * from 银行;

exec sp_executesql @statement = N'select * from 银行;'
go
-------
alter proc usp_bank
as
begin
begin transaction
declare @myError int;
update 银行 set balance=balance + 900 where cid='0001';
set @myError = @@ERROR;
update 银行 set balance=balance - 900 where cid='0002';
set @myError = @myError + @@ERROR;
if(@myError > 0)
begin
rollback;
end
else
begin
commit;
end
end;
go

select * from 银行;

exec usp_bank;
go
-------------------
alter proc usp_bank1
@money money,
@to char(4),
@from char(4)
as
begin
begin transaction
declare @myError int;
update 银行 set balance=balance + @money where cid=@to;
set @myError = @@ERROR;
update 银行 set balance=balance - @money where cid=@from;
set @myError = @myError + @@ERROR;
if(@myError > 0)
begin
rollback;
end
else
begin
commit;
end
end;
go
------------
select * from 银行;

exec usp_bank1 900, '0002', '0001';
go
exec usp_bank1 @from='0002', @to='0001', @money=900;
go

create proc usp_bank2
@to char(4),
@from char(4),
@money money = 100
as
begin
begin transaction
declare @myError int;
update 银行 set balance=balance + @money where cid=@to;
set @myError = @@ERROR;
update 银行 set balance=balance - @money where cid=@from;
set @myError = @myError + @@ERROR;
if(@myError > 0)
begin
rollback;
end
else
begin
commit;
end
end;
go

select * from 银行;

exec usp_bank2 @from='0001', @to='0002', @money=400;

go

create proc usp_bank3
@to char(4),
@from char(4),
@money money = 100,
@isSuccess int output
as
begin
begin transaction
declare @myError int;
update 银行 set balance=balance + @money where cid=@to;
set @myError = @@ERROR;
update 银行 set balance=balance - @money where cid=@from;
set @myError = @myError + @@ERROR;
if(@myError > 0)
begin
rollback;
set @isSuccess = 0;
end
else
begin
commit;
set @isSuccess = 1;
end
end;
go

--

select * from 银行;

declare @myPar int;
exec usp_bank3 '0002', '0001', 190, @mypar output;
if(@myPar=0)
begin
print '转账失败';
end
else
begin
print '转账成功';
end

go

select * from 银行;

declare @myPar int;
exec usp_bank4 @to='0001', @from='0002', @money=300, @isSuccess=@mypar output;
select @myPar;

--
go
create proc usp_bank4
@to char(4),
@from char(4),
@money money = 100,
@isSuccess int output
as
begin
begin transaction
begin try
update 银行 set balance=balance + @money where cid=@to;
update 银行 set balance=balance - @money where cid=@from;
commit;
set @isSuccess = 1;
end try
begin catch
rollback;
set @isSuccess = 0;
end catch
end;
go

----------------------------------
select * from 银行;

delete from 银行;

go
create trigger tr_名字 on 银行
after
delete
as
insert into 银行(cid, balance) select cid, balance from deleted;

select SUSER_NAME();

use TestDataBase;

create table MyIndex
(
id int identity(1,1),
name nvarchar(10)
);

select * from MyIndex;

select * from Student where stuName = '濮阳语儿';

--CREATE TABLE bank
--(
-- cId CHAR(4) PRIMARY KEY,
-- balanace MONEY,
--)
--alter table bank
--add constraint CH_balance check(balance >=10)
----delete from bank
--insert into bank values('0001',1000)
--insert into bank values('0002',10)
--go
--SELECT * FROM dbo.bank
----------银行问题

--BEGIN TRANSACTION
-- DECLARE @myError INT;
-- UPDATE bank SET balanace=balanace+900 WHERE cid='0001';
-- SET @myError = @@ERROR;
-- UPDATE dbo.bank SET balanace = balanace -900 WHERE cid='0002';
-- SET @myError=@myError + @@ERROR;
-- IF(@myError>0)
-- BEGIN
-- ROLLBACK;
-- END
-- ELSE
-- BEGIN
-- COMMIT;
-- END
--SELECT * FROM dbo.bank
--go
----------存储过程
--CREATE PROC usp_bank
--AS
--BEGIN
--BEGIN TRANSACTION
-- DECLARE @myError INT;
-- UPDATE bank SET balanace=balanace + 900 WHERE cid='0001';
-- SET @myError = @@ERROR;
-- UPDATE dbo.bank SET balanace = balanace -900 WHERE cid='0002';
-- SET @myError=@myError + @@ERROR;
-- IF(@myError>0)
-- BEGIN
-- ROLLBACK;
-- END
-- ELSE
-- BEGIN
-- COMMIT;
-- END
--END

--EXEC dbo.usp_bank

--go
--ALTER PROC usp_bank1
--@money MONEY,
--@to CHAR(4),
--@from CHAR(4)
--AS
--BEGIN
--BEGIN TRANSACTION
-- DECLARE @myError INT;
-- UPDATE bank SET balanace=balanace - @money WHERE cid=@from;
-- SET @myError = @@ERROR;
-- UPDATE dbo.bank SET balanace = balanace + @money WHERE cid=@to;
-- SET @myError=@myError + @@ERROR;
-- IF(@myError>0)
-- BEGIN
-- ROLLBACK;
-- END
-- ELSE
-- BEGIN
-- COMMIT;
-- END
--END

--EXEC dbo.usp_bank1 @money = 900, -- money
-- @to = '0002', -- char(4)
-- @from = '0001' -- char(4)

--go
--ALTER PROC usp_bank2
--@to CHAR(4),
--@from CHAR(4),
--@money MONEY = 100
--AS
--BEGIN
--BEGIN TRANSACTION
-- DECLARE @myError INT;
-- UPDATE bank SET balanace=balanace - @money WHERE cid=@from;
-- SET @myError = @@ERROR;
-- UPDATE dbo.bank SET balanace = balanace + @money WHERE cid=@to;
-- SET @myError=@myError + @@ERROR;
-- IF(@myError>0)
-- BEGIN
-- ROLLBACK;
-- END
-- ELSE
-- BEGIN
-- COMMIT;
-- END
--END
--EXEC dbo.usp_bank2 -- money
-- @to = '0001', -- char(4)
-- @from = '0002' -- char(4)

--SELECT * FROM dbo.bank

----------分页存储过程
go
alter PROC usp_PageMeetingUser
@pageIndex int =1,
@pageSize INT =10
AS
BEGIN
SELECT * FROM(SELECT ROW_NUMBER() OVER (ORDER BY id DESC) num,* FROM [dbo].[MeetingUser]) AS tbl1
WHERE tbl1.num BETWEEN
(@pageIndex - 1) * @pageSize + 1
AND
@pageIndex * @pageSize
END
SELECT * FROM dbo.meetingUser
EXEC usp_PageMeetingUser @pageIndex=2,@pageSize=5

SELECT * FROM bank

--alter table bank
--add constraint CH_balanace check(balanace >=10)

go
CREATE PROC usp_bank3
@to CHAR(4),
@from CHAR(4),
@money MONEY = 100,
@isSucess INT OUTPUT
AS
BEGIN
BEGIN TRANSACTION
DECLARE @myError INT;
UPDATE bank SET balanace=balanace - @money WHERE cid=@from;
SET @myError = @@ERROR;
UPDATE dbo.bank SET balanace = balanace + @money WHERE cid=@to;
SET @myError=@myError + @@ERROR;
IF(@myError>0)
BEGIN
ROLLBACK;
SET @isSucess=0
END
ELSE
BEGIN
COMMIT;
SET @isSucess=1
END
END
GO

DECLARE @subSucess INT ;
EXEC dbo.usp_bank3 -- money
@to = '0002', -- char(4)
@from = '0001', -- char(4)
@money = 100,
@isSucess = @subSucess OUTPUT;
IF( @subSucess=0)
BEGIN
PRINT '转账失败';
END
PRINT '转账成功';
SELECT * FROM dbo.bank

SQLServer学习记录的更多相关文章

  1. SQLServer学习笔记系列2

    一.写在前面的话 继上一次SQLServer学习笔记系列1http://www.cnblogs.com/liupeng61624/p/4354983.html以后,继续学习Sqlserver,一步一步 ...

  2. SQLServer 学习笔记之超详细基础SQL语句 Part 11

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 10------------------- DECLARE @myavg ...

  3. SQLServer 学习笔记之超详细基础SQL语句 Part 8

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 7------------------- --触发器str_trigge ...

  4. SQLServer 学习笔记之超详细基础SQL语句 Part 7

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 6------------------- 29 存储过程和触发器 存储过 ...

  5. SQLServer 学习笔记之超详细基础SQL语句 Part 6

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 5------------------- 28 聚合函数 --求平均分 ...

  6. SQLServer 学习笔记之超详细基础SQL语句 Part 4

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 3------------------- 17 带比较运算符的嵌套查询 ...

  7. SQLServer 学习笔记之超详细基础SQL语句 Part 3

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 2------------------- 13. 使用compute对查 ...

  8. SQLServer 学习笔记之超详细基础SQL语句 Part 2

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 1------------------- 建立如下数据表 CREATE ...

  9. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

随机推荐

  1. ASP.NET Core API 接收参数去掉烦人的 [FromBody]

    在测试ASP.NET Core API 项目的时候,发现后台接口参数为类型对象,对于PostMan和Ajax的Post方法传Json数据都获取不到相应的值,后来在类型参数前面加了一个[FromBody ...

  2. 解决一次git问题

    由于特殊原因,工作中不能使用sourcetree,今天遇到了一个问题记录一下解决办法,后续还是得好好去廖雪峰大神那里学习一下git的命令行操作呀 You have not concluded your ...

  3. python小游戏

    import time,random # 需要的数据和变量放在开头player_list = ['[狂血战士]','[森林箭手]','[光明骑士]','[独行剑客]','[格斗大师]','[枪弹专家] ...

  4. java - 策略模式、状态模式、卫语句,避免多重if-else(转)

    前言 当代码中出现多重if-else语句或者switch语句时.弊端之一:如果这样的代码出现在多处,那么一旦出现需求变更,就需要把所有地方的if-else或者switch代码进行更改,要是遗漏了某一处 ...

  5. 2.1 maven配置多镜像地址

    背景: 自己在平时写项目用的是阿里的镜像地址,而在开发公司的项目是用的是公司提供的镜像地址,这就导致了每次使用的时候 都需要来回的修改maven的settings.xml文件,这样很容易出错,而且还浪 ...

  6. gitlab-runner部署

    Gitlab在线安装部署参考 一.    环境 系统 版本 备注 Centos 7_64 建议使用4GB RAM和4或8个CPU内核 二.    安装并配置必要的依赖项 a)      在centos ...

  7. oracle更改字符集为zhs16GBK

    PDBalter pluggable database PDBANBOB open; alter session set container=pdbanbob; ALTER SYSTEM ENABLE ...

  8. [Dababase - MySQL- Linux] 数据库安装位置

    数据库安装最好是安装在 usr/local/ 里面,因为默认的很多东西都是指向这个文件夹中的.

  9. win2008 401 - 未授权: 由于凭据无效,访问被拒绝。解决方法

    iiis中一个小配置的问题,“身份验证”里面“启用匿名身份验证”,编辑匿名身份验证凭据,选中下面的“应用程序池标识”  就可以了

  10. 创建springboot项目

    springboot 就是为简化spring的创建 配置 部署 运行 而创建的. springboot 直接引入依赖jar包 就行了,无须配置xml 一 创建springboot 1.创建一个mave ...