【51CTO/BBS】请教: SQL里有没有字符串组合Join的函数??
【51CTO/BBS】請教: SQL里有沒有字符串組合Join的函數(shù)??
原帖地址:http://bbs.51cto.com/thread-1133863-1.html
?
問題描述:
VB 中有兩個非常好用的字符串處理函數(shù):
Split(字符串,分隔符)作用:將【字符串】以【分隔符】作為邊界,分解成數(shù)組。 返回:一個字符串?dāng)?shù)組。
Join(字符數(shù)組,分隔符)作用:將【字符數(shù)組】中的元素,以【分隔符】作為邊界,連接成一個字符串。返回:一個字符串。
請教老師們,SQL里是否有類似的函數(shù)?
?
解決方案:
如何用SQL Server Function實現(xiàn)Join?
?
如何用SQL CLR實現(xiàn)Join?
步驟一:
?
開始,運行Visual Studio 2012,選擇“New Project”,選擇“Visual C#”,“類庫”,命名類庫為fnConcatenate。
?
?
步驟二:
?
默認(rèn),Visual studio創(chuàng)建一個空的類命名為“Class1.cs”,右鍵重命名為Concatenate.cs。
?
?
步驟三:
?
雙擊“Concatenate.cs”文件,輸入如下代碼:
?
using?System; using?System.Data; using?Microsoft.SqlServer.Server; using?System.Data.SqlTypes; using?System.IO; 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?=?-1)?//maximum?size?in?bytes?of?persisted?value ] public?class?Concatenate?:?IBinarySerialize { ///?<summary> ///?The?variable?that?holds?the?intermediate?result?of?the?concatenation ///?</summary> private?StringBuilder?intermediateResult; ///?<summary> ///?Initialize?the?internal?data?structures ///?</summary> public?void?Init() { this.intermediateResult?=?new?StringBuilder(); } ///?<summary> ///?Accumulate?the?next?value,?not?if?the?value?is?null ///?</summary> ///?<param?name="value"></param> ///?<param?name="separator"></param> public?void?Accumulate(SqlString?value,?SqlString?separator) { if?(value.IsNull) { return; } this.intermediateResult.Append(value.Value).Append(separator); } ///?<summary> ///?Merge?the?partially?computed?aggregate?with?this?aggregate. ///?</summary> ///?<param?name="other"></param> public?void?Merge(Concatenate?other) { this.intermediateResult.Append(other.intermediateResult); } ///?<summary> ///?Called?at?the?end?of?aggregation,?to?return?the?results?of?the?aggregation. ///?</summary> ///?<returns></returns> public?SqlString?Terminate() { string?output?=?string.Empty; //delete?the?trailing?comma,?if?any if?(this.intermediateResult?!=?null &&?this.intermediateResult.Length?>?0) { output?=?this.intermediateResult.ToString(0,?this.intermediateResult.Length?-?1); } return?new?SqlString(output); } public?void?Read(BinaryReader?r) { intermediateResult?=?new?StringBuilder(r.ReadString()); } public?void?Write(BinaryWriter?w) { w.Write(this.intermediateResult.ToString()); } }?
步驟四:
?
從BUILD菜單,選擇“Build fnConcatenate”。編譯后,在bin目錄生成“fnConcatenate.dll”文件。拷貝該文件到SQL Server可訪問目錄,如D:\MSSQL\DATA\CLRLibraries。
?
?
?
步驟五:
?
打開SQL Server Management Studio,連接到需要部署該DLL的實例。
?
?
步驟六:
?
CLR集成默認(rèn)在SQL Server是禁用的。執(zhí)行下面的命令啟用CRL集成。
?
sp_configure?'show?advanced?options',?1 RECONFIGURE GO sp_configure?'clr?enabled',?1 RECONFIGURE GO sp_configure?'show?advanced?options',?0 RECONFIGURE GO?
步驟七:
?
在應(yīng)用的數(shù)據(jù)庫中通過該DLL創(chuàng)建Assemblies。
?
USE?AdvantureWorks2012 GO create?assembly?fnConcatenate from?N'D:\MSSQL\DATA\CLRLibraries\fnConcatenate.dll' with?permission_set?=?safe go?
?
步驟八:
?
創(chuàng)建concatenate函數(shù),語法類似創(chuàng)建標(biāo)準(zhǔn)函數(shù),除了使用“External”定位實際的程序邏輯到你的DLL中。
create?aggregate?concatenate?( @value?nvarchar(max), @separator?nvarchar(10) )?returns?nvarchar(max) external?name?fnConcatenate.Concatenate go?
?
步驟九:
?
測試concatenate函數(shù)
?
select?dbo.concatenate(FirstName,?',')?from?Person.Person?
轉(zhuǎn)載于:https://blog.51cto.com/ultrasql/1593982
總結(jié)
以上是生活随笔為你收集整理的【51CTO/BBS】请教: SQL里有没有字符串组合Join的函数??的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 联合索引详解
- 下一篇: mysql安装sphinx引擎