程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> sqlserver 函數、存儲進程、游標與事務模板

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

編輯:MSSQL

sqlserver 函數、存儲進程、游標與事務模板。本站提示廣大學習愛好者:(sqlserver 函數、存儲進程、游標與事務模板)文章只能為提供參考,不一定能成為您想要的結果。以下是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: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
)
RETURNS <Function_Data_Type, ,int>
AS
BEGIN
-- Declare the return variable here
DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int>

-- Add the T-SQL statements to compute the return value here
SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1>

-- Return the result of the function
RETURN <@ResultVar, sysname, @Result>

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: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@param1, sysname, @p1> <Data_Type_For_Param1, , int>,
<@param2, sysname, @p2> <Data_Type_For_Param2, , char>
)
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: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION <Table_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@param1, sysname, @p1> <data_type_for_param1, , int>,
<@param2, sysname, @p2> <data_type_for_param2, , char>
)
RETURNS
<@Table_Variable_Name, sysname, @Table_Var> TABLE
(
-- Add the column definitions for the TABLE variable here
<Column_1, sysname, c1> <Data_Type_For_Column1, , int>,
<Column_2, sysname, c2> <Data_Type_For_Column2, , int>
)
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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved