Sql Server自定義聚合函數(shù)詳細(xì)步驟

成都創(chuàng)新互聯(lián)是專業(yè)的田家庵網(wǎng)站建設(shè)公司,田家庵接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行田家庵網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
開始-
輸出-
首先用VS2008/VS2005建立一個(gè)SQL Server項(xiàng)目,右鍵解決方案添加新項(xiàng)
點(diǎn)擊“確定”按鈕后,SQL Server項(xiàng)目會(huì)要求連接一個(gè)數(shù)據(jù)庫,我們可以選擇一個(gè)數(shù)據(jù)庫
然后在工程中加入一個(gè)聚合類(joinstr.cs),如圖
joinstr.cs中的最終代碼如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text;
[Serializable]
[SqlUserDefinedAggregate(
Format.UserDefined, //use custom serialization to serialize the intermediate result
IsInvariantToNulls = true, //optimizer property
IsInvariantToDuplicates = false, //optimizer property
IsInvariantToOrder = false, //optimizer property????
MaxByteSize = 8000), //maximum size in bytes of persisted value
]
public struct JoinStr:IBinarySerialize
{
private StringBuilder sbIntermediate;
public void Init()
{
sbIntermediate = new StringBuilder();
}
public void Accumulate(SqlString Value)
{
if (Value == null || Value.ToString().ToLower().Equals("null"))
{
return;
}
else
{
sbIntermediate.Append(Value);
}
}
public void Merge(JionStr Group)
{
sbIntermediate.Append(Group.sbIntermediate);
}
public SqlString Terminate()
{
return new SqlString(sbIntermediate.ToString());
}
// This is a place-holder member field
#region IBinarySerialize Members
public void Read(System.IO.BinaryReader r)
{
sbIntermediate = new StringBuilder(r.ReadString());
}
public void Write(System.IO.BinaryWriter w)
{
w.Write(this.sbIntermediate.ToString());????
}
#endregion
}
在編寫完上述代碼后,可以使用Visual Studio來部署(右向工程,在彈出菜單上選“部署”即可)。
在執(zhí)行上面的SQL語句之前,需要將SQL Server2005的clr功能打開
現(xiàn)在可以使用joinstr來聚合字符串了。
select [t_code_role].[role_mc] as '角色',dbo.JoinStr([t_code_right].[right_mc]+',') as '權(quán)限' from [t_data_roleright],[t_code_right],[t_code_role] where [t_data_roleright].[role_bm]=[t_code_role].[role_bm] and [t_data_roleright].[right_bm]=[t_code_right].[right_bm] group by [t_code_role].[role_mc]
部署成功后,可調(diào)用并執(zhí)行用戶定義的函數(shù)。注意
在默認(rèn)情況下,Microsoft
SQL
Server
中關(guān)閉了
公共語言運(yùn)行庫
(CLR)
集成功能。必須啟用該功能才能使用
SQL
Server
項(xiàng)目項(xiàng)。若要啟用
CLR
集成,請使用
sp_configure
存儲(chǔ)過程
的“啟用
clr”選項(xiàng)。有關(guān)更多信息,請參見啟用
CLR
集成。注意顯示的對(duì)話框和菜單命令可能會(huì)與幫助中的描述不同,具體取決于您現(xiàn)用的設(shè)置或版本。若要更改設(shè)置,請?jiān)凇肮ぞ摺辈藛紊线x擇“導(dǎo)入和導(dǎo)出設(shè)置”。有關(guān)更多信息,請參見
Visual
Studio
設(shè)置。
創(chuàng)建用戶定義的
SQL
Server
函數(shù)
使用Visual
Studio
建立和部署此函數(shù)
C++
示例在編譯時(shí)必須使用
/clr:safe
編譯器
選項(xiàng)。
對(duì)于Visual
Basic
和
Visual
C#,在“解決方案
資源管理器
”中,打開“TestScripts”文件夾,然后雙擊“Test.sql”文件,以打開它進(jìn)行編輯。添加執(zhí)行您的
用戶定義函數(shù)
的代碼。請參見下面的第二個(gè)示例。對(duì)于Visual
C++,在“解決方案資源管理器”中雙擊“debug.sql”文件,以打開它進(jìn)行編輯。添加執(zhí)行您的用戶定義函數(shù)的代碼。請參見下面的第二個(gè)示例。將用戶定義函數(shù)部署到
SQL
Server。有關(guān)更多信息,請參見如何:將
SQL
Server
項(xiàng)目項(xiàng)部署到
SQL
Server
中。按F5
通過在
SQL
Server
上執(zhí)行用戶定義的函數(shù)來對(duì)其進(jìn)行調(diào)試。下面的代碼示例創(chuàng)建用戶定義的將價(jià)格作為參數(shù)的
標(biāo)量函數(shù)
addTax,添加
銷售稅
并返回價(jià)格和銷售稅的加和。創(chuàng)建該函數(shù)后,將其部署到
SQL
Server。有關(guān)更多信息,請參見如何:將
SQL
Server
項(xiàng)目項(xiàng)部署到
SQL
Server
中Visual
Basic
復(fù)制代碼
Imports
System.Data.SqlTypes
Imports
Microsoft.
SqlServer
.ServerPartial
Public
Class
UserDefinedFunctions
Public
Const
SALES_TAX
As
Double
=
0.086
_
Public
Shared
Function
addTax(ByVal
originalAmount
As
SqlDouble)
As
SqlDouble
Dim
taxAmount
As
SqlDouble
=
originalAmount
*
SALES_TAX
Return
originalAmount
+
taxAmount
End
FunctionEnd
ClassC#
復(fù)制代碼
using
System.Data.SqlTypes;
using
Microsoft.SqlServer.Server;public
partial
class
UserDefinedFunctions{public
const
double
SALES_TAX
=
.086;
[SqlFunction()]
public
static
SqlDouble
addTax(SqlDouble
originalAmount){SqlDouble
taxAmount
=
originalAmount
*
SALES_TAX;
return
originalAmount
+
taxAmount;}}C++
復(fù)制代碼
#include
"stdafx.h"#using
#using
#using
using
namespace
System;
using
namespace
System::Data;
using
namespace
System::Data::Sql;
using
namespace
System::Data::SqlTypes;
using
namespace
Microsoft::SqlServer::Server;//
In
order
to
debug
your
Aggregate,
add
the
following
to
your
debug.sql
SELECT
dbo.addTax(10)
//public
ref
class
UserDefinedFunctions{public:static
initonly
double
SALES_TAX
=
0.086;
[SqlFunction()]
static
SqlDouble
AddTax(SqlDouble
originalAmount){SqlDouble
taxAmount
=
originalAmount
*
SALES_TAX;
return
originalAmount
+
taxAmount;}};向您的項(xiàng)目中
TestScripts
文件夾中的
Test.sql(在
Visual
C++
中為
debug.sql)文件,添加用于測試用戶定義函數(shù)的代碼。例如,若要測試此函數(shù),請使用查詢,如“SELECT
dbo.addTax(10)”。您應(yīng)見到返回的值“10.86”。
復(fù)制代碼
存儲(chǔ)過程可以 你就建存儲(chǔ)過程唄
你這個(gè) 報(bào)錯(cuò)很明顯 函數(shù) 不讓你這么干
在函數(shù)內(nèi)對(duì)帶副作用的運(yùn)算符 'SELECT INTO' 的使用無效。
你換個(gè)招試試
比如 select count(*)=@v_count as dd from khn_configgroup where config_id=@template_id and group_id=@group_id;
sqlsever函數(shù)變量如何賦值我也不是很清楚 沒用過
或者用 set let
create function f_factorial (@t int)
returns int
as
begin
declare @i int,@total int
set @i=1
set @total=1
while @i=@t
begin
set @total=@total*@i
set @i=@i+1
end
return @total
end
調(diào)用
select dbo.f_factorial(10)
SELECT (表名+getdate()+cast(rand() * (1000) AS int)) as value from t_table
sqlserver 應(yīng)該有一個(gè)timestamp類型 那個(gè)是秒后面6位。。。 用那個(gè)其實(shí)就可以了
創(chuàng)建自定義函數(shù):
use 數(shù)據(jù)庫名
go
create function 函數(shù)名
(@pno int)
returns int
as
begin
declare @a int
if not exists(select * from person where pno=@pno)
set @a=-1
else
set @a=1
return @a
end
調(diào)用函數(shù):
use 數(shù)據(jù)庫名
go
select dbo.函數(shù)名(13250)
分享題目:sqlserver建函數(shù),數(shù)據(jù)庫中創(chuàng)建函數(shù)的sql語句是
文章鏈接:http://chinadenli.net/article43/dsgjhes.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、建站公司、營銷型網(wǎng)站建設(shè)、網(wǎng)站改版、App設(shè)計(jì)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)