存儲過程就是作爲可執行對象存放在數據庫中的一個或多個SQL命令。
一般分爲十種情況,每種語法各不相同:
1、 創建語法
create proc | procedure pro_name
[{@參數數據類型} [=默認值] [output],
{@參數數據類型} [=默認值] [output],
....
]
as
SQL_statements
2、 創建不帶參數存儲過程
--創建存儲過程
if (exists (select * from sys.objects where name = 'proc_get_student'))
drop proc proc_get_student
go
create proc proc_get_student
as
select * from student;
--調用、執行存儲過程
exec proc_get_student;
3、 修改存儲過程
--修改存儲過程
alter proc proc_get_student
as
select * from student;
4、 帶參存儲過程
--帶參存儲過程
if (object_id('proc_find_stu', 'P') is not null)
drop proc proc_find_stu
go
create proc proc_find_stu(@startId int, @endId int)
as
select * from student where id between @startId and @endId
go
exec proc_find_stu 2, 4;
5、 帶通配符參數存儲過程
--帶通配符參數存儲過程
if (object_id('proc_findStudentByName', 'P') is not null)
drop proc proc_findStudentByName
go
create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
select * from student where name like @name and name like @nextName;
go
exec proc_findStudentByName;
exec proc_findStudentByName '%o%', 't%';
6、 帶輸出參數存儲過程
if (object_id('proc_getStudentRecord', 'P') is not null)
drop proc proc_getStudentRecord
go
create proc proc_getStudentRecord(
@id int, --默認輸入參數
@name varchar(20) out, --輸出參數
@age varchar(20) output--輸入輸出參數
)
as
select @name = name, @age = age from student where id = @id and sex = @age;
go
--
declare @id int,
@name varchar(20),
@temp varchar(20);
set @id = 7;
set @temp = 1;
exec proc_getStudentRecord @id, @name out, @temp output;
select @name, @temp;
print @name + '#' + @temp;
7、 不緩存存儲過程
--WITH RECOMPILE 不緩存
if (object_id('proc_temp', 'P') is not null)
drop proc proc_temp
go
create proc proc_temp
with recompile
as
select * from student;
go
exec proc_temp;
8、 加密存儲過程
--加密WITH ENCRYPTION
if (object_id('proc_temp_encryption', 'P') is not null)
drop proc proc_temp_encryption
go
create proc proc_temp_encryption
with encryption
as
select * from student;
go
exec proc_temp_encryption;
exec sp_helptext 'proc_temp';
exec sp_helptext 'proc_temp_encryption';
9、 帶遊標參數存儲過程
if (object_id('proc_cursor', 'P') is not null)
drop proc proc_cursor
go
create proc proc_cursor
@cur cursor varying output
as
set @cur = cursor forward_only static for
select id, name, age from student;
open @cur;
go
--調用
declare @exec_cur cursor;
declare @id int,
@name varchar(20),
@age int;
exec proc_cursor @cur = @exec_cur output;--調用存儲過程
fetch next from @exec_cur into @id, @name, @age;
while (@@fetch_status = 0)
begin
fetch next from @exec_cur into @id, @name, @age;
print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
end
close @exec_cur;
deallocate @exec_cur;--刪除遊標
10、 分頁存儲過程
---存儲過程、row_number完成分頁
if (object_id('pro_page', 'P') is not null)
drop proc proc_cursor
go
create proc pro_page
@startIndex int,
@endIndex int
as
select count(*) from product
;
select * from (
select row_number() over(order by pid) as rowId, * from product
) temp
where temp.rowId between @startIndex and @endIndex
go
--drop proc pro_page
exec pro_page 1, 4
--
--分頁存儲過程
if (object_id('pro_page', 'P') is not null)
drop proc pro_stu
go
create procedure pro_stu(
@pageIndex int,
@pageSize int
)
as
declare @startRow int, @endRow int
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
select * from (
select *, row_number() over (order by id asc) as number from student
) t
where t.number between @startRow and @endRow;
exec pro_stu 2, 2;
優點:
1.由於應用程序隨着時間推移會不斷更改,增刪功能,T-SQL過程代碼會變得更復雜,StoredProcedure爲封裝此代碼提供了一個替換位置。
2.執行計劃(存儲過程在首次運行時將被編譯,這將產生一個執行計劃-- 實際上是 Microsoft SQL Server爲在存儲過程中獲取由 T-SQL 指定的結果而必須採取的步驟的記錄。)緩存改善性能。
…但sql server新版本,執行計劃已針對所有 T-SQL 批處理進行了緩存,而不管它們是否在存儲過程中,所以沒比較優勢了。
3.存儲過程可以用於降低網絡流量,存儲過程代碼直接存儲於數據庫中,所以不會產生大量T-sql語句的代碼流量。
4.使用存儲過程使您能夠增強對執行計劃的重複使用,由此可以通過使用遠程過程調用 (RPC) 處理服務器上的存儲過程而提高性能。RPC 封裝參數和調用服務器端過程的方式使引擎能夠輕鬆地找到匹配的執行計劃,並只需插入更新的參數值。
5.可維護性高,更新存儲過程通常比更改、測試以及重新部署程序集需要較少的時間和精力。
6.代碼精簡一致,一個存儲過程可以用於應用程序代碼的不同位置。
7.更好的版本控制,通過使用 Microsoft Visual SourceSafe 或某個其他源代碼控制工具,您可以輕鬆地恢復到或引用舊版本的存儲過程。
8.增強安全性:
a、通過向用戶授予對存儲過程(而不是基於表)的訪問權限,它們可以提供對特定數據的訪問;
b、提高代碼安全,防止 SQL注入(但未徹底解決,例如,將數據操作語言--DML,附加到輸入參數);
c、SqlParameter 類指定存儲過程參數的數據類型,作爲深層次防禦性策略的一部分,可以驗證用戶提供的值類型(但也不是萬無一失,還是應該傳遞至數據庫前得到附加驗證)。
缺點:
1.如果更改範圍大到需要對輸入存儲過程的參數進行更改,或者要更改由其返回的數據,則您仍需要更新程序集中的代碼以添加參數、更新 GetValue() 調用,等等,這時候估計比較繁瑣了。
2.可移植性差
由於存儲過程將應用程序綁定到 SQL Server,因此使用存儲過程封裝業務邏輯將限制應用程序的可移植性。如果應用程序的可移植性在您的環境中非常重要,則將業務邏輯封裝在不特定於 RDBMS 的中間層中可能是一個更佳的選擇。