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. vue--js里跳转页面

    我们知道在vue里进行页面跳转的话,我们使用<router-link>这个标签 那在构造函数里我们不能直接操纵DOM元素,我们又该如何进行页面跳转呢? 步骤1: 我们先在DOM里设置三个按 ...

  2. 本地连接属性:Internet协议版本4(TCP/IPv4)打开闪退解决办法

    1.命令窗口配置网络连接指定IP netsh interface ip set address "本地连接" static IP地址 子网掩码 默认网关 例:netsh inter ...

  3. http协议详解及htt面试题目,常见的http状态码

    http协议详解及htt面试题目,常见的http状态码 HTTP报文是面向文本的,报文中的每一个字段都是一些ASCII码串,各个字段的长度是不确定的.HTTP有两类报文:请求报文和响应报文. HTTP ...

  4. php安全开发(1)文件包含漏洞

    开发过程总结的漏洞: 一,,如何造成包含漏洞:在通过函数包含文件时,由于没有对包含的文件名进行有效的过滤处理,被攻击者利用从而导致了包含了Web根目录以外的文件进来,就会导致文件信息的泄露甚至注入了恶 ...

  5. LayaAir引擎开发HTML5最简单教程(面向JS开发者)

    LayaAir引擎开发HTML5最简单教程(面向JS开发者) 一.总结 一句话总结:开发游戏还是得用游戏引擎来开发,其实很简单啦 切记:开发游戏还是得用游戏引擎来开发,其实很简单,引擎很多东西都帮你做 ...

  6. Matlab -- Portfolio

    1.创建空 p = Portfolio; 2.需要了解 均值,方差,协方差实现 X为矩阵 均值 = mean(X): 中位数 = median(X): 方差 = var(X): 标准差 = std(X ...

  7. elasticSearch安装 Kibana安装 Sense安装

    安装最新版本,安装6.*版本 先提示一个重要的事情,kibana新版本不需要安装Sense, 官方的是老版的Kibana才需要,我们现在用devTool http://localhost:5601/a ...

  8. Win10安装LoadRunner11

    一.下载 地址:http://www.51testing.com/?uid-4827-action-viewspace-itemid-225451 二.安装 本来想写,结果和别人的一样就不写了:htt ...

  9. 使用JS判断不同的终端设备

    const ua: string = window.navigator.userAgent; const isWeixin: boolean = /MicroMessenger/i.test(ua); ...

  10. linux 下mysql多实例安装

    1.软件下载 https://dev.mysql.com/downloads/file/?id=479096 免编译二进制包 mysql-5.6.21-linux-glibc2.5-x86_64.ta ...