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. linux 添加环境变量

    You have to edit three files to set a permanent environment variable as follow: ~/.bashrc When you o ...

  2. C# Finalize和Dispose的区别

    一:总结 1.Finalize方法(C#中是析构函数,以下称析构函数)是用于释放非托管资源的,而托管资源会由GC自动回收.所以,我们也可以这样来区分 托管和非托管资源.所有会由GC自动回收的资源,就是 ...

  3. js 整理

    类型 1.js 中的数据类型,解释清楚原始数据类型和引用数据类型 js中共有null, underfined, string, number, boolean, object 六种数据类型 原始数据类 ...

  4. SpringCloud问题解决:spring-cloud-eureka启动出错Cannot execute request on any known server

    场景: 在启动eureka server时,出现以下错误: com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectExce ...

  5. python 学习笔记 3 ----> dive into python 3

    Python内置数据类型 注意: Python可以不需要声明变量的数据类型.它是根据变量的初始赋值情况分析数据类型,并在内部跟踪变量. 比较重要的数据类型: 1 布尔型(Booleans):True. ...

  6. 【转】解决Android 6.0 NoSuchContextException 和WEBVIEW_undefined 的问题

    在 Android 4.4 操作系统上测试混合应用时,可以直接顺利的在native模式和webview模式之间切换,但是在Android6.0 操作系统上却报NoSuchContextExceptio ...

  7. PHP随机生成要求位数个字符(大小写字母+数字)

    php随机生成要求位数个字符 /** * 随机生成要求位数个字符 * @param length 规定几位字符 */ function getRandChar($length){ $str = nul ...

  8. 安装weblogic

    附:wls1036_generic.jar 链接:https://pan.baidu.com/s/1W5g-SHeKL96yrOeDKJJoxw 提取码:vxft 注:以下安装步骤是一个前辈整理的,我 ...

  9. .NET+MySql 踩坑1

    换成MySql数据库后,遇到的问题: 已解决,但不理解的问题: var test = db.test; 报如下图错误: 加上DefaultIfEmpty()则解决. var test = db.Tes ...

  10. C语言博客作业05——指针

    1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量 1.2.1学习体会 可能因为之前数组那块儿的作业拖得太久了,以至于我觉得指针学的好快,还没反应过来就教完了,然后一开始做题的时候,就是一脸 ...