濮阳杆衣贸易有限公司

主頁 > 知識庫 > sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板

sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板

熱門標(biāo)簽:小e電話機(jī)器人 百度地圖標(biāo)注改顏色 鎮(zhèn)江網(wǎng)路外呼系統(tǒng)供應(yīng)商 電銷外呼有錄音系統(tǒng)有哪些 外呼運營商線路收費 貴州房產(chǎn)智能外呼系統(tǒng)供應(yīng)商 臨沂智能電銷機(jī)器人加盟哪家好 一個導(dǎo)航軟件能用幾個地圖標(biāo)注點 申請400電話在哪辦理流程
1.標(biāo)量函數(shù):結(jié)果為一個單一的值,可包含邏輯處理過程。其中不能用getdate()之類的不確定性系統(tǒng)函數(shù).
復(fù)制代碼 代碼如下:

--標(biāo)量值函數(shù)
-- ================================================
-- 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.內(nèi)聯(lián)表值函數(shù):返回值為一張表,僅通過一條SQL語句實現(xiàn),沒有邏輯處理能力.可執(zhí)行大數(shù)據(jù)量的查詢.

復(fù)制代碼 代碼如下:

--內(nèi)聯(lián)表值函數(shù)

-- ================================================
-- 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.多語句表值函數(shù):返回值為一張表,有邏輯處理能力,但僅能對小數(shù)據(jù)量數(shù)據(jù)有效,數(shù)據(jù)量大時,速度很慢.

復(fù)制代碼 代碼如下:

--多語句表值函數(shù)

-- ================================================
-- 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.游標(biāo):對多條數(shù)據(jù)進(jìn)行同樣的操作.如同程序的for循環(huán)一樣.有幾種循環(huán)方向控制,一般用FETCH Next.

復(fù)制代碼 代碼如下:

--示意性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
--定義一個游標(biāo)對象[merge_cursor]
--該游標(biāo)中包含的為:[SELECT MasterCustomerId, DuplicateCustomerId FROM DuplicateCustomers WHERE IsMerged = 0 ]查詢的結(jié)果.

OPEN merge_cursor
--打開游標(biāo)
FETCH NEXT FROM merge_cursor INTO @MasterId, @DuplicateId
--取數(shù)據(jù)到臨時變量
WHILE @@FETCH_STATUS = 0 --系統(tǒng)@@FETCH_STATUS = 0 時循環(huán)結(jié)束
--做循環(huán)處理
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
--關(guān)閉游標(biāo)
DEALLOCATE merge_cursor
--刪除游標(biāo)

[說明:游標(biāo)使用必須要配對,Open--Close,最后一定要記得刪除游標(biāo).]

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

復(fù)制代碼 代碼如下:

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
您可能感興趣的文章:
  • c#實現(xiàn)sqlserver事務(wù)處理示例
  • SQL Server觸發(fā)器及觸發(fā)器中的事務(wù)學(xué)習(xí)
  • sqlserver中的事務(wù)和鎖詳細(xì)解析
  • Sqlserver 存儲過程中結(jié)合事務(wù)的代碼
  • SQLSERVER分布式事務(wù)使用實例
  • 淺析SQL Server中包含事務(wù)的存儲過程
  • SQLServer存儲過程中事務(wù)的使用方法
  • SQL Server存儲過程中編寫事務(wù)處理的方法小結(jié)
  • Sql Server中的事務(wù)介紹
  • Sql Server事務(wù)語法及使用方法實例分析

標(biāo)簽:延邊 晉城 合肥 嘉興 三明 保定 日照 澳門

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板》,本文關(guān)鍵詞  sqlserver,函數(shù),存儲,過程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板》相關(guān)的同類信息!
  • 本頁收集關(guān)于sqlserver 函數(shù)、存儲過程、游標(biāo)與事務(wù)模板的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    泰州市| 甘孜县| 阜平县| 栖霞市| 金阳县| 英山县| 宜兴市| 承德县| 康乐县| 三原县| 分宜县| 河北区| 大余县| 商南县| 逊克县| 吉木萨尔县| 巴青县| 灵璧县| 揭阳市| 德令哈市| 进贤县| 桂平市| 綦江县| 三江| 肃北| 徐水县| 都安| 固安县| 特克斯县| 花莲县| 葵青区| 淳化县| 鸡东县| 临夏市| 德州市| 榆树市| 兴文县| 奇台县| 莎车县| 都兰县| 马公市|