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. Spring之bean的生命周期

    这篇博文是spring生命周期的详解,目前限于作者自身的水平对于一些内容可能只知其然不知其所以然,所以博文中如果出现错误欢迎各位指出,同时我也会逐步提升自己的水平,争取能够多发布一些能让大家获益的博文 ...

  2. Unity3D|-XLua热更新用法的大致流程

    xlua是由腾讯维护的一个开源项目,我们可以在github上下载这个开源项目并查看一些相关文档 官网:https://github.com/Tencent/xLua 配置文档:https://gith ...

  3. ES6删除对象中的某个元素

    const form = { id: '011', name: '测试一', description: '测试demo' } // 目标: 取到删除description属性的对象, 即下文的data ...

  4. python高阶函数(Map、Reduce、Filter、lamba)

    Map函数 map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 代码如下: >>> def f(x): . ...

  5. JVM调优(一)

    JVM调优的主要过程有: 确定堆内存大小(-Xmx, -Xms).合理分配新生代和老生代(-XX:NewRation, -Xmn, -XX:SurvivorRatio).确定永久区大小: -XX:Pe ...

  6. 关于p-Laplace的想法

    最近的想法 关于p-Laplace的想法 对于一类p-laplace方程的问题的想法. 现在摆在面前的是首先我要考虑的问题是$W^{1,p}$估计对于凸区域上的p-laplace是否成立,或者更广.. ...

  7. 【实战问题】【3】iPhone无法播放video标签中的视频

    问题:视频都是MP4格式,视频可以在手机上正常播放.video标签中的视频在安卓点击可以播放,但在iPhone无法播放 解决方案: 1,视频编码格式问题,具体iPhone手机支持的是哪些格式可见官方的 ...

  8. 记一次mybatis bindingexception 问题排查

    看到的错误信息如出一辙都是这样的:Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound s ...

  9. fiddler安装及mock数据

    1,fiddler安装,解决无法抓到https问题 可用本机的火狐浏览器测试,不行,就fiddler生成证书,拷到火狐里 在firefox中,选项->进入配置界面:高级-> 证书 -> ...

  10. Java中判断对象是否为空的方法

    首先来看一下工具StringUtils的判断方法:    一种是org.apache.commons.lang3包下的:    另一种是org.springframework.util包下的.这两种S ...