14 SQL Server 儲存過程

2022-01-29 13:32:03 字數 4063 閱讀 8355

sql server 儲存過程

儲存過程類似函式,可以重複使用。相對於函式,儲存過程擁有更強大的功能和更高的靈活性。

儲存過程中可以包含邏輯控制語句和資料操作語句,可以接受引數,輸出引數,返回單個值或多個結果集。

儲存過程帶來的好處:

1、效能的提公升

儲存過程執行時,第一次會進行編譯和優化。但批處理t-sql語句每次執行都需要預編譯和優化,所以沒有儲存過程快。

2、易於維護

儲存過程建立後儲存在資料庫中,可以被程式多次呼叫執行。當需要修改儲存過程時,對應用程式**毫無影響。

3、安全性

應用程式只需要呼叫儲存過程名,給幾個引數,而不是直接訪問基礎物件。需要賦予的不是增刪改的許可權,而是exec的許可權。

系統儲存過程

系統儲存過程主要儲存在master資料庫中,以sp_開頭,可以在所有資料庫物件中使用。

常用的系統儲存過程

exec sp_databases    --

檢視所有資料庫

exec sp_tables --

檢視所有資料表

exec sp_columns student --

檢視student表的所有列

exec sp_helpindex student --

檢視student表的索引

exec sp_helpconstraint student --

檢視student表的約束

exec sp_helptext '

sp_databases'--

檢視定於語句

exec sp_rename oldname,newname --

修改表、索引、列的名稱

exec sp_renamedb webdb,newdb --

修改資料庫名稱

exec sp_helpdb webdb --

檢視資料庫資訊

使用者定義儲存過程語法:

create

proc

|procedure

proc_name

[ [=default][

out | output],

[=default][

out | output]]

as[begin]t

-sql**

[end

]

不帶引數

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc proc_test --

建立create 修改alter

asselect

*from student order

by id desc

--呼叫

exec proc_test

執行儲存過程使用execute關鍵字,可以簡寫為exec。在sql server 2012中得到加強,可以修改結果集中列名和型別。

execute

proc_test

with

result sets

( (

序號 varchar(5

), 姓名

varchar(10

), 性別

varchar(2

), 年齡

varchar(5

), 郵箱

輸入引數

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc proc_test (@id

int)

asselect

*from student where id =

@id--

呼叫exec proc_test 10

預設引數

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc proc_test (@id

int=10)

asselect

*from student where id =

@id--

呼叫exec proc_test --

10exec proc_test 15

--15

輸出引數

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc

proc_test (

@idint, --

輸入引數

@name

varchar(10) out, --

輸出引數

@age

int output --

輸入輸出引數)as

begin

--可寫可不寫

select

@name

= name,@age

= age from student where id =

@idend

--呼叫

declare

@name

varchar(10),@age

intexec proc_test 10,@name out,@age

output

select

@name,@age

不快取

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc

proc_test

with recompile --

不快取,每次都編譯

asselect

*from student order

by id desc

--呼叫

exec proc_test

加密

if(exists(select

*from sys.objects where name =

'proc_test'))

drop

proc proc_test --

刪除go

create

proc

proc_test

with encryption --

加密後無法檢視

asselect

*from student order

by id desc

--呼叫

exec

proc_test

exec

sp_helptext proc_test

--提示物件 'proc_test' 的文字已加密。

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...

SQLSERVER儲存過程

sqlserver儲存過程使用說明書 引言首先介紹一下什麼是儲存過程 儲存過程就是將常用的或很複雜的工作,預先用 sql語句寫好並用乙個指定的名稱儲存起來,並且這樣的語句是放在資料庫中的,還可以根據條件執行不同 sql語句,那麼以後要叫資料庫提供與已定義好的儲存過程的功能相同的服務時,只需呼叫 ex...

SQL Server 儲存過程

儲存過程概念 儲存過程優點 儲存過程的介面 儲存過程的解析 編譯過程 儲存過程安全性 檢視儲存過程 加密 解密儲存過程 儲存過程概念 儲存過程 stored procedure 是一組為了完成特定功能的sql語句集,經編譯後儲存在資料庫中。使用者通過指定儲存過程的名字並給出引數 帶參儲存過程 來執行...