Asia小程

导航

SQLServer学习记录

 

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

posted on 2018-10-09 15:46  Asia小程  阅读(126)  评论(0编辑  收藏  举报