自定义多参数Sql聚合函数
Creating a CLR user define aggregate (part 2). Use multiple columns in the aggregation function
In part 1 we created a nice user defined aggregate. Now we are going to make it more sophisticated and let its value depend on two parameters ShipCountry and ShipShipCity. You might try having two parameters in Accumulate function of the aggregate but you will get an error
The Accumulate method in user defined aggregate "Bonus" must have exactly one parameter.
We are definitely looking into adding “multi-column aggregates” feature in the future versions of SQL Server. For now you can use a workaround. The idea is to create a worker UDT that contain all the fields required for the aggregation. So if you want to take Orders.ShipCountry and Orders.ShipCity into account the UDT should have two corresponding fields. You also need to create a user defined function that takes a number of parameters and returns an instance of the worker UDT. And finally you create an aggregate that takes the worker UDT as a parameter in its aggregation function.
Let’s say XYZ wants to consider German sales that has been shipped to Berlin as regular sales. To take ShipCity this into account you first need to create a UDT. I won’t implement several methods to keep the sample short.
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize=8000)]
public struct OrderData : INullable, IBinarySerialize
{
public override string ToString()
{
throw new Exception("The method or operation is not implemented.");
}
public bool IsNull
{
get
{
return false;
}
}
&n
相关文档:
exec sp_spaceused [[@objname=]'objname'][,[@updateusage=]'updateusage']
检查数据库空间大小
dbcc showfilestats
统计数据文件的使用情况
dbcc showcontig
检查数据库控件分配情况
dbcc sqlperf(logspace)
查看日志文件的使用情况 ......
1 TOP
这是一个大家经常问到的问题,例如在SQLSERVER中可以使用如下语句来取得记录集中的前十条记录:
SELECT TOP 10 * from [index] ORDER BY indexid DESC;
但是这条SQL语句在SQLite中是无法执行的,应该改为:
SELECT * from [index] ORDER BY indexid DESC limit 0,10;
其中limit 0,10表示从第0条记录开始,往后 ......
SQL游标原理和使用方法
数据库开发过程中,当你检索的数据只是一条记录时,你所编写的事务语句代码往往使用SELECT INSERT 语句。但是我们常常会遇到这样情况,即从某一结果集中逐一地读取一条记录。那么如何解决这种问题呢?游标为我们提供了一种极为优秀的解决方案。
1.1 游标和游标的优点
在数据 ......
ORACLE与SQL SERVER语法区别
一、数据类型
ORACLE与SQL SERVER在数据类型的对比如下:
SQL SERVER
ORACLE
数字类型
DECIMAL[(P[, S])]
NUMBER[(P[, S])]
NUMERIC[(P[, S])]
NUMBER[(P[, S])]
FLOAT[(N)]
NUMBER[(N)]
INT
NUMBER
SMALLINT
NUMBER
TINYINT
NUMBER
MONEY
NUMBER[19,4]
SM ......
示例:
传入多个Email地址,通过每个Email地址间的','分隔符,将各Email地址分开。
SELECT * from dbo.uf_Split('aa@aa.com,bb@bb.com,cc@cc.com,dd@dd.com',',');
查询结果:
subid autoid
aa@aa.com 1
bb@bb.com 2
cc@cc.com 3
dd@dd.com 4
下面是[uf_Split]方法的具体实现:
CREATE ......