sqlserver 函數、存儲過程、游標與事務模板

SQL 函數、存儲過程、游標與事務模板,學習sqlserver的朋友很多情況下都需要用得到。

1.標量函數:結果為一個單一的值,可包含邏輯處理過程。其中不能用getdate()之類的不確定性系統函數.
代碼如下:
–標量值函數
— ================================================
— Template generated from Template Explorer using:
— Create Scalar Function (New Menu).SQL

— Use the Specify Values for Template Parameters
— command (Ctrl-Shift-M) to fill in the parameter
— values below.

— This block of comments will not be included in
— the definition of the function.
— ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
— =============================================
— Author:
— Create date:
— Description:
— =============================================
CREATE FUNCTION
(
— Add the parameters for the function here

)
RETURNS
AS
BEGIN
— Declare the return variable here
DECLARE

— Add the T-SQL statements to compute the return value here
SELECT =

— Return the result of the function
RETURN

END

2.內聯表值函數:返回值為一張表,僅通過一條SQL語句實現,沒有邏輯處理能力.可執行大數據量的查詢.

代碼如下:
–內聯表值函數

— ================================================
— Template generated from Template Explorer using:
— Create Inline Function (New Menu).SQL

— Use the Specify Values for Template Parameters
— command (Ctrl-Shift-M) to fill in the parameter
— values below.

— This block of comments will not be included in
— the definition of the function.
— ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
— =============================================
— Author:
— Create date:
— Description:
— =============================================
CREATE FUNCTION
(
— Add the parameters for the function here
,

)
RETURNS TABLE
AS
RETURN
(
— Add the SELECT statement with parameter references here
SELECT 0
)
GO

3.多語句表值函數:返回值為一張表,有邏輯處理能力,但僅能對小數據量數據有效,數據量大時,速度很慢.

代碼如下:
–多語句表值函數

— ================================================
— Template generated from Template Explorer using:
— Create Multi-Statement Function (New Menu).SQL

— Use the Specify Values for Template Parameters
— command (Ctrl-Shift-M) to fill in the parameter
— values below.

— This block of comments will not be included in
— the definition of the function.
— ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
— =============================================
— Author:
— Create date:
— Description:
— =============================================
CREATE FUNCTION (
— Add the parameters for the function here
,

)
RETURNS
TABLE
(
— Add the column definitions for the TABLE variable here
,

)
AS
BEGIN
— Fill the table variable with the rows for your result set

RETURN
END
GO

4.游標:對多條數據進行同樣的操作.如同程序的for循環一樣.有幾種循環方向控制,一般用FETCH Next.

代碼如下:
–示意性SQL腳本

DECLARE @MergeDate Datetime
DECLARE @MasterId Int
DECLARE @DuplicateId Int

SELECT @MergeDate = GetDate()

DECLARE merge_cursor CURSOR FAST_FORWARD FOR SELECT MasterCustomerId, DuplicateCustomerId FROM DuplicateCustomers WHERE IsMerged = 0
–定義一個游標對象[merge_cursor]
–該游標中包含的為:[SELECT MasterCustomerId, DuplicateCustomerId FROM DuplicateCustomers WHERE IsMerged = 0 ]查詢的結果.

OPEN merge_cursor
–打開游標
FETCH NEXT FROM merge_cursor INTO @MasterId, @DuplicateId
–取數據到臨時變量
WHILE @@FETCH_STATUS = 0 –系統@@FETCH_STATUS = 0 時循環結束
–做循環處理
BEGIN
EXEC MergeDuplicateCustomers @MasterId, @DuplicateId

UPDATE DuplicateCustomers
SET
IsMerged = 1,
MergeDate = @MergeDate
WHERE
MasterCustomerId = @MasterId AND
DuplicateCustomerId = @DuplicateId

FETCH NEXT FROM merge_cursor INTO @MasterId, @DuplicateId
–再次取值
END

CLOSE merge_cursor
–關閉游標
DEALLOCATE merge_cursor
–刪除游標

[說明:游標使用必須要配對,Open–Close,最后一定要記得刪除游標.]

5.事務:當一次處理中存在多個操作,要么全部操作,要么全部不操作,操作失敗一個,其他的就全部要撤銷,不管其他的是否執行成功,這時就需要用到事務.

代碼如下:
begin tran
update tableA
set columnsA=1,columnsB=2
where RecIs=1
if(@@ERROR 0 OR @@ROWCOUNT 1)
begin
rollback tran
raiserror( ‘此次update表tableA出錯!!’ , 16 , 1 )
return
end

insert into tableB (columnsA,columnsB) values (1,2)
if(@@ERROR 0 OR @@ROWCOUNT 1)
begin
rollback tran
raiserror( ‘此次update表tableA出錯!!’ , 16 , 1 )
return
end

end
commit

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享