本系列博文還在更新中,收錄在專欄:#MS-SQL Server 專欄中。
本系列文章列表如下:
【SQL Server】 Linux 運(yùn)維下對(duì) SQL Server 進(jìn)行安裝、升級(jí)、回滾、卸載操作
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(一)數(shù)據(jù)庫(kù)設(shè)計(jì)的核心概念和基本步驟
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(二)MSSQL數(shù)據(jù)庫(kù)開發(fā)對(duì)于庫(kù)、表、數(shù)據(jù)類型、約束等相關(guān)操作
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(三)面向數(shù)據(jù)分析的 T-SQL 編程技巧與實(shí)踐
[ 云原生 | Docker ] 構(gòu)建高可用性的 SQL Server:Docker 容器下的主從同步實(shí)現(xiàn)指南
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(五)T-SQL 高級(jí)查詢綜合應(yīng)用與實(shí)戰(zhàn)
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(六)索引和視圖的使用技巧、方法與綜合應(yīng)用
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(七)MS-SQL存儲(chǔ)過(guò)程全面解析:種類、優(yōu)點(diǎn)和創(chuàng)建方法詳解
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(八)高級(jí)數(shù)據(jù)處理技術(shù) MS-SQL 事務(wù)、異常和游標(biāo)的深入研究
【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(九)詳細(xì)講解 MS-SQL 觸發(fā)器的基礎(chǔ)概念與應(yīng)用場(chǎng)景
前言
Transact-SQL(T-SQL)是一種用于 Microsoft SQL Server 和 Azure SQL Database 等關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)的擴(kuò)展 SQL 語(yǔ)言。
在數(shù)據(jù)庫(kù)開發(fā)中,存儲(chǔ)過(guò)程是一個(gè)非常重要的概念,它是一個(gè)可重復(fù)使用的程序,可用于執(zhí)行特定的任務(wù)和數(shù)據(jù)處理。它具有很多的優(yōu)點(diǎn),例如減少網(wǎng)絡(luò)流量,維護(hù)方便,提高性能等等。在本文章中,我們將深入探討存儲(chǔ)過(guò)程的定義,種類以及使用方法。
一、什么是存儲(chǔ)過(guò)程
存儲(chǔ)過(guò)程(Procedure)是一組為了完成特定功能的 SQL 語(yǔ)句集合,經(jīng)編譯后存儲(chǔ)在數(shù)據(jù)庫(kù)中,用戶通過(guò)指定存儲(chǔ)過(guò)程的名稱并給出參數(shù)來(lái)執(zhí)行。
存儲(chǔ)過(guò)程中可以包含邏輯控制語(yǔ)句和數(shù)據(jù)操縱語(yǔ)句,它可以接受參數(shù)、輸出參數(shù)、返回單個(gè)或多個(gè)結(jié)果集以及返回值。
由于存儲(chǔ)過(guò)程在創(chuàng)建時(shí)即在數(shù)據(jù)庫(kù)服務(wù)器上進(jìn)行了編譯并存儲(chǔ)在數(shù)據(jù)庫(kù)中,所以存儲(chǔ)過(guò)程運(yùn)行要比單個(gè)的 SQL 語(yǔ)句塊要快。同時(shí)由于在調(diào)用時(shí)只需用提供存儲(chǔ)過(guò)程名和必要的參數(shù)信息,所以在一定程度上也可以減少網(wǎng)絡(luò)流量、簡(jiǎn)單網(wǎng)絡(luò)負(fù)擔(dān)。
1.1 存儲(chǔ)過(guò)程的優(yōu)點(diǎn)
存儲(chǔ)過(guò)程提供了性能優(yōu)化、代碼復(fù)用、安全性、事務(wù)控制、網(wǎng)絡(luò)傳輸減少等多個(gè)方面的優(yōu)點(diǎn),使得數(shù)據(jù)庫(kù)應(yīng)用程序更加高效、安全、可靠和易于維護(hù)。
總結(jié)起來(lái)存儲(chǔ)結(jié)構(gòu)的幾個(gè)特點(diǎn)有:
特點(diǎn) | 說(shuō)明 |
---|---|
提高性能 | 存儲(chǔ)過(guò)程的語(yǔ)句在首次執(zhí)行時(shí)會(huì)被編譯和優(yōu)化,并生成執(zhí)行計(jì)劃,下次執(zhí)行時(shí)可以直接使用緩存中的執(zhí)行計(jì)劃,減少了編譯和優(yōu)化的開銷,提高了執(zhí)行效率。 |
代碼復(fù)用 | 存儲(chǔ)過(guò)程可以在多個(gè)地方被調(diào)用,實(shí)現(xiàn)了代碼的復(fù)用,避免了重復(fù)編寫相同的SQL語(yǔ)句,提高了開發(fā)效率。 |
安全性和權(quán)限控制 | 存儲(chǔ)過(guò)程可以定義特定的權(quán)限,只有具有足夠權(quán)限的用戶才能執(zhí)行存儲(chǔ)過(guò)程,有效地保護(hù)數(shù)據(jù)庫(kù)的安全性。 |
事務(wù)控制 | 存儲(chǔ)過(guò)程可以將一系列數(shù)據(jù)庫(kù)操作封裝在一個(gè)事務(wù)中,保證這些操作要么全部執(zhí)行成功,要么全部回滾,確保數(shù)據(jù)的一致性和完整性。 |
網(wǎng)絡(luò)傳輸減少 | 存儲(chǔ)過(guò)程在數(shù)據(jù)庫(kù)服務(wù)器上執(zhí)行,可以減少客戶端與服務(wù)器之間的網(wǎng)絡(luò)傳輸,降低了網(wǎng)絡(luò)開銷和延遲。 |
簡(jiǎn)化復(fù)雜操作 | 存儲(chǔ)過(guò)程可以封裝復(fù)雜的業(yè)務(wù)邏輯和數(shù)據(jù)處理過(guò)程,簡(jiǎn)化了客戶端應(yīng)用程序的開發(fā)和維護(hù)。 |
提高數(shù)據(jù)的一致性 | 通過(guò)存儲(chǔ)過(guò)程執(zhí)行數(shù)據(jù)操作,可以確保對(duì)數(shù)據(jù)的訪問(wèn)方式是一致的,避免了數(shù)據(jù)不一致性的問(wèn)題。 |
二、存儲(chǔ)過(guò)程的種類
2.1 SQL Server 系統(tǒng)存儲(chǔ)過(guò)程
系統(tǒng)存儲(chǔ)過(guò)程是 SQL Server系統(tǒng)自身提供的存儲(chǔ)過(guò)程,可以作為命令執(zhí)行各種操作。
系統(tǒng)存儲(chǔ)過(guò)程目的在于能夠方便的從系統(tǒng)表中查詢信息或完成與更新數(shù)據(jù)庫(kù)表相關(guān)的管理任務(wù)或其他的系統(tǒng)管理任務(wù)。系統(tǒng)存儲(chǔ)過(guò)程主要存儲(chǔ)在master數(shù)據(jù)庫(kù)中,以sp
下劃線開頭的存儲(chǔ)過(guò)程。盡管這些系統(tǒng)存儲(chǔ)過(guò)程定義在系統(tǒng)定義和用戶定義的數(shù)據(jù)庫(kù)中,在調(diào)用時(shí)不必在存儲(chǔ)過(guò)程前加數(shù)據(jù)庫(kù)限定名。有一些系統(tǒng)存儲(chǔ)過(guò)程會(huì)在創(chuàng)建新的數(shù)據(jù)庫(kù)的時(shí)候被自動(dòng)創(chuàng)建在當(dāng)前數(shù)據(jù)庫(kù)中。
常用系統(tǒng)存儲(chǔ)過(guò)程有:
-- sp_help:顯示數(shù)據(jù)庫(kù)對(duì)象的詳細(xì)信息,如表、視圖、存儲(chǔ)過(guò)程等。
EXEC sp_help 'TableName';
-- sp_helptext:顯示存儲(chǔ)過(guò)程、觸發(fā)器、函數(shù)等對(duì)象的定義文本。
EXEC sp_helptext 'StoredProcedureName';
-- sp_who:顯示當(dāng)前活動(dòng)的數(shù)據(jù)庫(kù)會(huì)話和進(jìn)程信息。
EXEC sp_who;
-- sp_renamedb:重命名數(shù)據(jù)庫(kù)。
EXEC sp_renamedb 'OldDatabaseName', 'NewDatabaseName';
-- sp_adduser:在數(shù)據(jù)庫(kù)中創(chuàng)建新用戶。
EXEC sp_adduser 'UserName', 'Password', 'DatabaseName';
-- sp_addlogin:在SQL Server實(shí)例中創(chuàng)建新的登錄賬號(hào)。
EXEC sp_addlogin 'LoginName', 'Password', 'DefaultDatabase';
-- sp_change_users_login:修復(fù)數(shù)據(jù)庫(kù)中的用戶與登錄賬號(hào)之間的不一致性。
EXEC sp_change_users_login 'Auto_Fix', 'UserName', 'LoginName';
-- sp_spaceused:顯示數(shù)據(jù)庫(kù)或表的空間使用情況。
EXEC sp_spaceused 'TableName';
-- sp_configure:配置和修改SQL Server實(shí)例的全局配置選項(xiàng)。
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
-- sp_executesql:執(zhí)行動(dòng)態(tài)生成的SQL語(yǔ)句。
EXEC sp_executesql N'SELECT * FROM TableName WHERE ColumnName = @Param', N'@Param int', @Param = 123;
-- sp_rename:重命名數(shù)據(jù)庫(kù)對(duì)象,如表、列等。
EXEC sp_rename 'TableName', 'NewTableName';
-- sp_depends:查找指定對(duì)象的依賴關(guān)系,包括引用該對(duì)象的其他對(duì)象和被該對(duì)象所引用的對(duì)象。
EXEC sp_depends 'ObjectName';
-- sp_helpindex:顯示表的索引信息。
EXEC sp_helpindex 'TableName';
-- sp_helpconstraint:顯示表的約束信息。
EXEC sp_helpconstraint 'TableName';
-- sp_lock:顯示當(dāng)前數(shù)據(jù)庫(kù)中的鎖信息。
EXEC sp_lock;
-- sp_monitor:顯示SQL Server實(shí)例的系統(tǒng)統(tǒng)計(jì)信息。
EXEC sp_monitor;
-- sp_resetstatus:重置數(shù)據(jù)庫(kù)的狀態(tài)為正常。
EXEC sp_resetstatus 'DatabaseName';
-- sp_refreshview:刷新視圖的元數(shù)據(jù)信息。
EXEC sp_refreshview 'ViewName';
-- sp_change_users_default_db:修改用戶的默認(rèn)數(shù)據(jù)庫(kù)。
EXEC sp_change_users_default_db 'UserName', 'NewDefaultDatabase';
-- sp_estimate_data_compression_savings:估計(jì)表的數(shù)據(jù)壓縮節(jié)省空間的情況。
EXEC sp_estimate_data_compression_savings 'TableName';
系統(tǒng)存儲(chǔ)過(guò)程創(chuàng)建并存放在與系統(tǒng)數(shù)據(jù)庫(kù) master 中,一些系統(tǒng)存儲(chǔ)過(guò)程只能由系統(tǒng)管理員使用,而有些系統(tǒng)存儲(chǔ)過(guò)程通過(guò)授權(quán)可以被其它用戶所使用。
2.2 SQL Server 擴(kuò)展存儲(chǔ)過(guò)程
擴(kuò)展存儲(chǔ)過(guò)程(Extended Stored Procedure)是一種特殊的存儲(chǔ)過(guò)程,它是使用 C 或 C++ 編程語(yǔ)言編寫的外部可執(zhí)行程序,可以在 SQL Server 中被調(diào)用。擴(kuò)展存儲(chǔ)過(guò)程允許開發(fā)人員在 SQL Server 中執(zhí)行特定的操作,例如訪問(wèn)操作系統(tǒng)資源,實(shí)現(xiàn)自定義的數(shù)據(jù)訪問(wèn)方法,或與第三方庫(kù)進(jìn)行交互等。相比于傳統(tǒng)的 T-SQL 存儲(chǔ)過(guò)程,擴(kuò)展存儲(chǔ)過(guò)程更加靈活和強(qiáng)大,但也更加復(fù)雜和難以維護(hù)。需要注意的是,擴(kuò)展存儲(chǔ)過(guò)程的使用需要小心謹(jǐn)慎,特別是在安全方面,以防止惡意代碼的執(zhí)行和數(shù)據(jù)泄露。
擴(kuò)展存儲(chǔ)過(guò)程以前綴xp_
來(lái)標(biāo)識(shí),對(duì)于用戶來(lái)說(shuō),擴(kuò)展存儲(chǔ)過(guò)程和普通話存儲(chǔ)過(guò)程一樣,可以用相同的方法來(lái)執(zhí)行。
2.2.1 常見的擴(kuò)展存儲(chǔ)過(guò)程與用法舉例
xp_cmdshell:該擴(kuò)展存儲(chǔ)過(guò)程允許在SQL Server中執(zhí)行操作系統(tǒng)命令,例如創(chuàng)建、刪除文件目錄等等。例如,在執(zhí)行以下語(yǔ)句之后,將在SQL Server主機(jī)上創(chuàng)建c:\test目錄:
EXEC xp_cmdshell 'mkdir c:\test'
xp_readerrorlog:該擴(kuò)展存儲(chǔ)過(guò)程可用于從SQL Server錯(cuò)誤日志中讀取信息。例如,以下語(yǔ)句將返回最近50個(gè)錯(cuò)誤日志記錄:
EXEC xp_readerrorlog 0, 1, N'', NULL, NULL, NULL, N'asc'
xp_fixeddrives:該擴(kuò)展存儲(chǔ)過(guò)程可用于返回計(jì)算機(jī)上所有可用的邏輯驅(qū)動(dòng)器的名稱和類型。例如,以下語(yǔ)句將返回有關(guān)計(jì)算機(jī)上所有邏輯驅(qū)動(dòng)器的信息:
EXEC xp_fixeddrives
xp_sprintf:這個(gè)擴(kuò)展存儲(chǔ)過(guò)程看起來(lái)就有點(diǎn)像入門寫C語(yǔ)言的printf 函數(shù)了。燃鵝即時(shí)這樣,我認(rèn)為其實(shí)用到它的機(jī)會(huì)并不多。大部分使用的場(chǎng)景如下:
DECLARE @i VARCHAR(20) = 1,
@j varchar(50) = 'ajofen',
@s VARCHAR(80)
EXEC master..xp_sprintf @s OUTPUT,'there are two varaint one is %s and another is %s ',@i,@j
PRINT @s
xp_sendmail:該擴(kuò)展存儲(chǔ)過(guò)程可用于通過(guò)SMTP服務(wù)器發(fā)送電子郵件。例如,以下語(yǔ)句將向收件人發(fā)送一封測(cè)試郵件:
EXEC master.dbo.xp_sendmail
@recipients = 'receiver@example.com',
@subject = 'Test email',
@message = 'This is a test email',
@anonymous = 'true',
@body_format = 'HTML'
xp_regread 和 xp_regwrite:這兩個(gè)擴(kuò)展存儲(chǔ)過(guò)程用于讀取和寫入Windows注冊(cè)表中的值。例如,以下語(yǔ)句將從HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion鍵中讀取注冊(cè)表項(xiàng)的值:
EXEC master.dbo.xp_regread
@rootkey = 'HKEY_LOCAL_MACHINE',
@key = 'Software\Microsoft\Windows\CurrentVersion',
@value_name = 'ProductName',
@value = @product_name OUTPUT
xp_create_subdir:該擴(kuò)展存儲(chǔ)過(guò)程用于創(chuàng)建新目錄。例如,以下語(yǔ)句將在c:\temp目錄下創(chuàng)建名為test的新目錄:
EXEC xp_create_subdir 'c:\temp\test'
xp_sscanf:這個(gè)擴(kuò)展存儲(chǔ)過(guò)程是對(duì)插入的字符串變量進(jìn)行格式化取值。
DECLARE @i VARHAR(50) = 1,
@j varchar(50) ,
@s VARCHAR(80)
EXEC master..xp_sscanf '3 + 31 = 34','%s + %s =34',@i OUTPUT,@s OUTPUT
SELECT @i,@j,@s
xp_enumgroups 和 xp_logininfo:這兩個(gè)擴(kuò)展存儲(chǔ)過(guò)程用于獲取有關(guān)Windows組和用戶的信息。例如,以下語(yǔ)句將返回所有本地用戶和組的信息:
EXEC xp_enumgroups '', 0, @groups OUTPUT
EXEC xp_logininfo '', @groups, @accounts OUTPUT
xp_delete_file 和 xp_deletemail:這兩個(gè)擴(kuò)展存儲(chǔ)過(guò)程可用于刪除文件和電子郵件收件箱中的郵件。例如,以下語(yǔ)句將刪除c:\temp\test.txt文件:
EXEC xp_delete_file 'c:\temp\test.txt', NULL, 0
xp_terminate_process:該擴(kuò)展存儲(chǔ)過(guò)程用于殺死指定的進(jìn)程。例如,以下語(yǔ)句將殺死進(jìn)程ID為1234的進(jìn)程:
EXEC xp_terminate_process 1234
2.3 用戶自定義存儲(chǔ)過(guò)程
2.3.1 創(chuàng)建存儲(chǔ)過(guò)程的語(yǔ)法
create proc | procedure pro_name
[{@參數(shù)數(shù)據(jù)類型} [=默認(rèn)值] [output],
{@參數(shù)數(shù)據(jù)類型} [=默認(rèn)值] [output],
....
]
as
SQL_statements
2.3.2 創(chuàng)建不帶參數(shù)存儲(chǔ)過(guò)程
--創(chuàng)建存儲(chǔ)過(guò)程
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;
--調(diào)用、執(zhí)行存儲(chǔ)過(guò)程
exec proc_get_student;
2.3.3 修改存儲(chǔ)過(guò)程
這里有兩種方法進(jìn)行修改:
-
使用
alter
語(yǔ)句進(jìn)行修改--修改存儲(chǔ)過(guò)程 alter proc proc_get_student as select * from student;
-
使用
sp_rename
函數(shù)修改存儲(chǔ)過(guò)程的名稱sp_rename '存儲(chǔ)過(guò)程名', '新存儲(chǔ)過(guò)程名', 'OBJECT'
2.3.4 創(chuàng)建帶參存儲(chǔ)過(guò)程
--帶參存儲(chǔ)過(guò)程
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;
2.3.5 創(chuàng)建通配符參數(shù)的存儲(chǔ)過(guò)程
在工作用,可以使用通配符參數(shù)存儲(chǔ)過(guò)程來(lái)搜索滿足特定條件的數(shù)據(jù),從而避免查詢的重復(fù)和繁瑣,同時(shí)也可以對(duì)搜索結(jié)果進(jìn)行排序或其他變換操作,進(jìn)一步提高查詢效率。
--帶通配符參數(shù)存儲(chǔ)過(guò)程
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%';
2.3.6 創(chuàng)建帶輸出參數(shù)的存儲(chǔ)過(guò)程
帶有輸出參數(shù)的存儲(chǔ)過(guò)程在 SQL Server 中的應(yīng)用場(chǎng)景主要是用于向調(diào)用過(guò)程傳遞有關(guān)在存儲(chǔ)過(guò)程執(zhí)行過(guò)程中計(jì)算出的值,因此,可以在SQL服務(wù)器中使用輸出參數(shù)來(lái)實(shí)現(xiàn)跨存儲(chǔ)過(guò)程的參數(shù)傳遞,或者用來(lái)實(shí)現(xiàn)輸出參數(shù)的值及其狀態(tài)的檢查。
輸出參數(shù)的主要作用是讓程序可以從存儲(chǔ)過(guò)程中獲取計(jì)算出的值,它允許在運(yùn)行時(shí)在存儲(chǔ)過(guò)程中作出更多的決策,因?yàn)橛脩艨梢允褂幂敵鰠?shù)來(lái)方便地獲取數(shù)據(jù),從而避免執(zhí)行另一個(gè)查詢來(lái)獲取數(shù)據(jù),并且可以將數(shù)據(jù)傳遞給另一個(gè)存儲(chǔ)過(guò)程。
if (object_id('proc_getStudentRecord', 'P') is not null)
drop proc proc_getStudentRecord
go
create proc proc_getStudentRecord(
@id int, --默認(rèn)輸入?yún)?shù)
@name varchar(20) out, --輸出參數(shù)
@age varchar(20) output--輸入輸出參數(shù)
)
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;
2.3.7 創(chuàng)建不緩存存儲(chǔ)過(guò)程
不緩存存儲(chǔ)過(guò)程的應(yīng)用場(chǎng)景主要有兩個(gè):
- 執(zhí)行非常耗時(shí)的任務(wù),比如復(fù)雜的查詢和大數(shù)據(jù)的處理等;
- 當(dāng)數(shù)據(jù)庫(kù)環(huán)境變化時(shí),可以快速更新存儲(chǔ)過(guò)程信息而不需要重啟服務(wù)器。
不緩存存儲(chǔ)過(guò)程的作用是提高執(zhí)行效率和實(shí)現(xiàn)動(dòng)態(tài)更新存儲(chǔ)過(guò)程的信息。它可以有效地減少數(shù)據(jù)庫(kù)服務(wù)器的開銷,確保服務(wù)器的穩(wěn)定可用性,并且能夠有效地處理海量的數(shù)據(jù)。
--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;
2.3.8 創(chuàng)建加密的存儲(chǔ)過(guò)程
SQL Server 中的加密存儲(chǔ)過(guò)程的應(yīng)用場(chǎng)景主要是為了提高數(shù)據(jù)庫(kù)安全性,例如,可以避免對(duì)數(shù)據(jù)庫(kù)對(duì)象的意外刪除或修改,防止對(duì)數(shù)據(jù)的不恰當(dāng)訪問(wèn),以及為數(shù)據(jù)庫(kù)安全性預(yù)防網(wǎng)絡(luò)黑客攻擊。加密存儲(chǔ)過(guò)程也可以用來(lái)防止用戶從數(shù)據(jù)庫(kù)獲取非授權(quán)信息。加密存儲(chǔ)過(guò)程也可以用來(lái)存儲(chǔ)和傳輸敏感數(shù)據(jù),保護(hù)數(shù)據(jù)庫(kù)的安全性和完整性。
這種創(chuàng)創(chuàng)建方式以及作用類似于我之前的視圖講解中的一樣,有興趣的同學(xué)可以參看之前的博文《【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(六)索引和視圖的使用技巧、方法與綜合應(yīng)用》
--加密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';
2.3.9 創(chuàng)建帶游標(biāo)參數(shù)存儲(chǔ)過(guò)程
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
--調(diào)用
declare @exec_cur cursor;
declare @id int,
@name varchar(20),
@age int;
exec proc_cursor @cur = @exec_cur output;--調(diào)用存儲(chǔ)過(guò)程
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;--刪除游標(biāo)
2.3.10 創(chuàng)建分頁(yè)存儲(chǔ)過(guò)程
分頁(yè)存儲(chǔ)過(guò)程可以在SQL Server數(shù)據(jù)庫(kù)中實(shí)現(xiàn)結(jié)果集的分頁(yè)顯示、靈活的指定頁(yè)碼和頁(yè)大小,實(shí)現(xiàn)不同結(jié)果集的分頁(yè)顯示:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-464923.html
---存儲(chǔ)過(guò)程、row_number完成分頁(yè)
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
--
--分頁(yè)存儲(chǔ)過(guò)程
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;
文末總結(jié)
本文章詳細(xì)介紹了存儲(chǔ)過(guò)程的定義、類型以及創(chuàng)建、修改方法。通過(guò)學(xué)習(xí)本文章,我們可以了解到存儲(chǔ)過(guò)程的優(yōu)點(diǎn),了解存儲(chǔ)過(guò)程的種類以及創(chuàng)建方法,并深入了解如何創(chuàng)建帶參數(shù)的存儲(chǔ)過(guò)程、帶輸出參數(shù)的存儲(chǔ)過(guò)程和分頁(yè)存儲(chǔ)過(guò)程等。相信這些知識(shí)將為數(shù)據(jù)庫(kù)開發(fā)者提供寶貴的幫助和參考。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-464923.html
到了這里,關(guān)于【SQL Server】數(shù)據(jù)庫(kù)開發(fā)指南(七)MS-SQL存儲(chǔ)過(guò)程全面解析:種類、優(yōu)點(diǎn)和創(chuàng)建方法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!