SqlServer子查詢、高級


  1. 子查詢:把一個結果集讓別人繼續分析查詢的就叫子查詢
  2. 子查詢如果定義了別名,在查詢引用時,必須使用別名
  3. --子查詢定義了別名,引用就必須用別名
    select id,n
    from Person,(select depname as n from Depment ) as d

    常用運算符:

  4. in:表示對多個單列結果進行條件匹配
  5. --in例子
    
    select name,age 
    from Person
    where Age in(select Age from Person where Age < 19)
    
    --any例子:與運算符結合使用,大於表示要大於查詢到的結果集
    
    select name,age
    from Person
    where Age <any(select Age from Person where Age < 19)

    exists

  6. --exists:判斷是否為空,為空false,否則true
    
    select * from Person
    where exists(select * from Depment where id='002')
    
    
    select * from Person
    where exists(
    select * from Depment d where d.id = Person.DepID
    )

    將結果集直接在數據庫插入一張新表,多數據插入

    1. --數據量多插入數據:列必須前后對應
      
      create table person3(id int primary key,name varchar(30),age int, depid varchar(20))
      
      insert into person3( id,name,age,depid)
      select ID,Name,Age,DepID from person

      更新,刪除類似

  7. Sql注入攻擊問題,采用參數化傳參形式可解決
  8. 事務:保證數據原子性、一致性、隔離性、持久性
  9. 開窗函數:可以對每一行結果都返回一個值,避免了group by必須分組的問題
    1. select count(*) over() from 表名
    2. select COUNT(*) over(partition by city) from shakerecode  參數意思是根據城市來分組顯示出城市的數量

  10. 連接池:訪問數據庫時所產生的鏈接驗證成功后,當釋放連接對象時,數據庫就把此連接對象放到了連接池,下次使用時先從連接池找,如果連接池有這個連接對象直接使用,減少驗證等消耗。
    1. 打開數據庫連接對象的時間要正好,使用完連接對象要盡快釋放,這樣連接池的利用率高
    2. 連接字符串默認自動打開連接池,如果就想關閉連接池加語法pooling=false;
  11. 子查詢分頁
  12. --子查詢分頁
    --between (頁碼-1)*每頁條數+1   and  頁碼*每頁條數 
    select * from
    (
    select ROW_NUMBER() over(order by id) as num, * from Student
    ) t
    where t.num between (1-1)*10+1 and 1*10

     創建視圖、修改視圖分頁

go
create view vw_student
as
select * from
(
select ROW_NUMBER() over(order by id) as num, * from Student
) t
where t.num between (1-1)*10+1 and 1*10
go

--修改視圖

alter view vw_student
as
select * from
(
select ROW_NUMBER() over(order by id) as num, * from Student
) as t
go

select * from vw_student where num between 10 and 20
  • 事務
--事務

--三個步驟:開始事務,提交事務,或者回滾事務

begin transaction

commit transaction

rollback transaction

set @@error  --記錄最近一次數據操作的錯誤,如果沒有錯誤默認是0

select @@error
  • 存儲過程
  • 存儲過程結合事務+變量+參數返回值,執行轉賬例子
go
create proc usp_BankToBank
@from nvarchar(10),
@to nvarchar(10),
@money money,
@issuccess int output
as
declare @myError int
set @myError = 0
begin
begin tran
update Bank set MoneyVal-=@money where ID = @from
set @myError = @myError+@@ERROR
update Bank set MoneyVal += @money where ID=@to
set @myError = @myError+@@ERROR
if @myError > 0
begin
set @issuccess = 0
rollback tran
end
else
begin
set @issuccess = 1
commit tran
end
end
go

declare @res int
exec usp_BankToBank '001','002',200,@res output
select @res
  •  存儲過程分頁
create proc usp_StudentPage
@pageIndex int,
@pageCount int
as
begin
select * from
(
select ROW_NUMBER() over(order by id) as num,* from Student
) t
where t.num between (@pageIndex-1)*@pageCount + 1 and @pageIndex*@pageCount
end
go;

exec usp_StudentPage 2,20
  •  觸發器
alter trigger tr_刪除觸發器 on bank
after
delete
as
insert into Bank(ID,moneyval)
select * from deleted;
  • 獲取新增數據本身值
  • -獲取最近一次插入數據的值
    
    insert into Bank(ID,MoneyVal) output inserted.* values('004',20)

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM