图解SQL Server 存储过程教程一
? ? 以下給出存儲過程代碼和鄙人運行成功的截圖。把以下存儲過程代碼中的tablename替換為自己的表名。
一 不帶參數存儲過程
if (exists (select * from sys.objects where name = 'proc_get_tablename'))
? ? drop proc proc_get_tablename
go
create proc proc_get_tablename
as
? ? select * from tablename;
二?帶參存儲過程
if (object_id('proc_find_tablename', 'P') is not null)
? ? drop proc proc_find_tablename
go
create proc proc_find_tablename(@startId int, @endId int)
as
? ? select * from tablename where id between @startId and @endId
go
三 帶通配符參數存儲過程
if (object_id('proc_findtablenameByName', 'P') is not null)
? ? drop proc proc_findtablenameByName
go
create proc proc_findtablenameByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
? ? select * from tablename where name like @name and name like @nextName;
go
四?帶輸出參數存儲過程
if (object_id('proc_gettablenameRecord', 'P') is not null)
? ? drop proc proc_gettablenameRecord
go
create proc proc_gettablenameRecord(
? ? @id int, --默認輸入參數
? ? @name varchar(20) out, --輸出參數
? ? @age varchar(20) output--輸入輸出參數
)
as
? ? select @name = name, @age = age ?from tablename where id = @id and sex = @age;
go
五?不緩存存儲過程
--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 tablename;
go
六?加密存儲過程****
--加密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 tablename;
go
? ? 可以看到加密存儲過程顯示的圖標與別的不同,有一個小鎖的標識;
七?帶游標參數存儲過程
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 tablename;
? ? open @cur;
go
如果出現 必須聲明標量變量 "@cur" 錯誤;
可試著加入SET NOCOUNT ON
編寫如下的執行語句,然后執行,
USE dboa;
GO
DECLARE @MyCursor CURSOR;
exec dbo.proc_cursor @cur=@MyCursor OUTPUT;
WHILE (@@FETCH_STATUS = 0)
BEGIN;
? ? ?FETCH NEXT FROM @MyCursor;
END;
CLOSE @MyCursor;
DEALLOCATE @MyCursor;
GO
結果如下圖,一行一行的提取了行;
關于 須聲明標量變量 "@cur" 錯誤,網友有言:
變量的使用必須和定義在一起
1.declare @name varchar(30) = 'aaaa'
print @name
2.declare @name varchar(30) = 'aaaa'
go
print @name
1可以執行,而2由于聲明與使用分開,就會報錯
八分頁存儲過程1
---存儲過程、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 tablename
; ? ?
? ? select * from (
? ? ? ? select row_number() over(order by pid) as rowId, * from tablename?
? ? ) temp
? ? where temp.rowId between @startIndex and @endIndex
go
? ? 執行前提示輸入參數,此處每頁5條;當然實際需要分頁的情況都是記錄數很多;例如鄙人曾做過10萬記錄每頁分1萬的情況;
九?分頁存儲過程2
if (object_id('pro_page', 'P') is not null)
? ? drop proc pro_tablename
go
create procedure pro_tablename(
? ? @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 tablename?
? ? ) t
? ? where t.number between @startRow and @endRow;
? ? 輸入參數是起始行號,頁尺寸;
示例數據庫 dboa 下載:
http://pan.baidu.com/s/1bntWzSV
全部存儲過程代碼下載:
http://pan.baidu.com/s/1o6kgAP8
總結
以上是生活随笔為你收集整理的图解SQL Server 存储过程教程一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图解JSP页面教程
- 下一篇: 图解JDK命令行工具实例教程