×Ô¶¨Òå¶à²ÎÊý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
Ïà¹ØÎĵµ£º
ÒÔÏÂÊÇ·¢ÔÚ÷×ÓÂÛ̳µÄÌù×Ó£¬×ª·¢¹ýÀ´£¬Ï£Íû¸øÓõÃ×ŵĺüÓѲο¼¡£
ÎҵijÌÐòÒÔǰһֱÊÇÓû¨Éú¿Ç°ó¶¨IPʵÏÖµÄÔ¶³Ì£¬ÓÉÓÚ¿Í»§·þÎñÆ÷ºÍ¿Í»§¶Ë¶¼ÊÇͨ¹ý¿í´øÉÏÍø£¬Ò»°ãµÄ²Ù×÷£¨¿ª½ø»õµ¥¡¢ÏúÊÛµ¥µÈ£©ËÙ¶ÈÒ²»¹²»´í£¬µ«ÊÇÔÚÔ¶³Ì¿Í»§¶ËÐÞ¸ÄÉÌÆ·×ÊÁÏ£¨10000¶àÌõ¼Ç¼£©¡¢²éѯһ¶ÎʱÆÚµÄ½øÏú´æÁ÷Ë®£¨Ò»ÖÜ5000ÌõÒÔÉÏ£©µÈÉæ¼°µ½È¡¼Ç¼Á¿´óµ ......
ÇåÀíSQL Server 2008Êý¾Ý¿âÈÕÖ¾
ÊÕ²Ø
µÚÒ»²½, ÔÚÊÕËõǰÏȲ鿴ÈÕÖ¾µÄ´óС:
SELECT
*
from
sysfiles
WHERE
name
LIKE
'
%
LOG
%'
GO
µÚ¶þ²½, °ÑÊý¾Ý¿âµÄ»Ö¸´Ä£Ê½Éè³É”¼òµ¥”:
ALTER
DATABASE
¿âÃû
SET
......
·ÀÖ¹SQL×¢Èë¹¥»÷µÄ×¢ÒâÊÂÏî
Ò». SQL Injection¼°Æä·À·¶µÄ»ù±¾ÖªÊ¶
¿ÉÄÜ´ó¼Ò¶¼ÖªµÀ£¬SQL×¢ÈëÖ÷ÒªÊÇÀûÓÃ×Ö·ûÐͲÎÊýÊäÈëµÄ¼ì²é©¶´¡£
±ÈÈç˵£¬³ÌÐòÖÐÓÐÕâÑùµÄ²éѯ£º
string sql = "SELECT * from SiteUsers WHERE UserName='" + userName + "'";
ÆäÖеÄuserName²ÎÊýÊÇ´ÓÓû§½çÃæÉÏÊäÈëµÄ¡£
Èç¹ûÊÇÕý³£µÄÊäÈ룬±ÈÈç ......
Ò»¡¢»ù´¡
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
CREATE DATABASE database-name
2¡¢ËµÃ÷£ºÉ¾³ýÊý¾Ý¿â
drop database dbname
3¡¢ËµÃ÷£º±¸·Ýsql server
--- ´´½¨ ±¸·ÝÊý¾ÝµÄ device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- ¿ªÊ¼ ±¸·Ý
BACKUP DATABASE pubs TO testBack
4¡¢Ëµ ......
SQL ²é詢²»區·Ö´óС寫
2007Äê04ÔÂ19ÈÕ ÐÇÆÚËÄ 15:27
Õý³£ÓÃÕâ¸ö·½·¨¾Í¸ø達µ½Ð§¹ûÁË¡£select * from
pl_account where UPPER(fname) like 'PE%'
¸½¼Ó£º
ÔÚsql2000ºÍ7.0µÄ²éѯÓï¾äÖÐ,Çø·Ö´óдµÄ²éѯ·½·¨
--sql2000,¾ÍÓÃÏÂÃæµÄ·½·¨.
--¾ÍÊÇÔÚ×Ö¶ÎÃûºó¼Ó collate Chi ......