Ò»´ÎÉñÆæµÄSQL ´íÎóµ÷ÊÔ¾Àú
ÉÏÖܽӵ½Ò»¸öÆæ¹ÖµÄbug£¬Ò»¸öÔø¾ÔËÐеúܺõĴ洢¹ý³ÌͻȻ²úÉúÁË´íÎóµÄ½á¹û¡£
¸ºÔðά»¤µÄÐÖµÜÃǺܸºÔðÈεĶԴíÎó½øÐÐÁ˸ú×Ù£¬²¢°Ñ´íÎó¶¨Î»Ò»¸öÈçϵÄÓï¾ä£º
SELECT *
into SomeTable
from A join B on A.id=B.id
join C on A.id=C.id
ËûÃÇ·¢ÏÖ´ÓSomeTable×ö²éѯµÄʱºò£¬³öÀ´µÄ½á¹û±Èʵ¼Ê½á¹ûÒª´óÈô¸É±¶£¬Í¦ÆëÕûµÄ£¬4±¶»òÕß2±¶¡£
ÎҵĵÚÒ»¸ö¸Ð¾õÊÇJOIN³öÎÊÌâÁË£¬±ØÈ»ÊÇÐγÉÁ˶à¶Ô¶àµÄJOIN£¬±ÈÈçAÀïÃæÓÐÁ½ÐÐIDÒ»Ñù£¬BÒ²ÓÐÁ½ÐÐIDÒ»Ñù£¬½á¹û±í¾Í»á³öÏÖ4ÐÐÒ»ÑùµÄ£¬µÃµ½Ò»¸ö4±¶µÄ½á¹û£¬Í¬ÑùµÄµÀÀí£¬Èç¹ûAÀïÓÐÒ»ÐУ¬BÀïÓÐ2ÐУ¬½á¹û¾ÍÊÇ2±¶ÁË¡£
ÓÚÊÇÎÒÈÃËûÃÇ°ÑSomeTable×ö³ÉÒ»¸öÎı¾Îļþ´«¸øÎÒ£¬àÅ£¬300MµÄÒ»¸öÎļþ£¬BCPµ½Êý¾Ý¿âÀSELECTÁËÒ»°Ñ£¬°Ñ³öÎÊÌâµÄÄǸöID×¥³öÀ´£¬¹ûÈ»¿´µ½¼¸¸öÖظ´µÄÐС£È·ÇеÄ˵¿´µ½¼¸Áм¸ºõÒ»ÑùµÄÐС£
ÓÚÊÇÎÒ¾ÍÐ˳å³åµÄ¸øËûÃÇ˵£¬¼ì²ìÒ»ÏÂBºÍCÉÏÃæµÄIDÊÇ·ñÊÇuniqueµÄ¡£
½á¹ûºÜ¿ì·µ»Ø£¬Ëµ£¬ÊÇuniqueµÄ¡£
ÕâÏÂÎÒ¾ÍɵÑÛÁË£¬ÕâÔõô¿ÉÄÜÄØ¡£ÖØÐÂ×ÐϸµÄ¿´Á˱ãÉϴβ鴦µÄ½á¹û£¬·¢ÏÖÔ±¾ÒÔΪÖظ´µÄÐУ¬¾¹È»²»ÊÇÍêÈ«ÏàͬµÄ£¬àÅ£¬ÓÐÒ»ÁÐÊDz»Í¬µÄ£¬¶øÕâÒ»ÁÐÕýʽB.ID¡£Çë×¢Ò⣬ÕâЩÐÐÔÚA.idÉÏÊÇÒ»ÑùµÄ¡£
ÕâÊÇÒ»¸ö¾ªÈ˵ķ¢ÏÖ£¬ÎÒͨ¹ýA JOIN B JOIN C³öÀ´µÄ½á¹û¾ÓÈ»²»Âú×ãJIONµÄÌõ¼þ£¬Õâ³¹µ×µß¸²Á˶àÄêѧϰµÄ¹ØϵÊý¾Ý¿â֪ʶ¡£
ºÁÎÞÒÉÎÊ£¬ÕâÊÇSQL ServerµÄÒ»¸öbug¡£È»¶øÈçºÎ½¨Ò»¸ö×îСµÄÊý¾Ý¼¯ºÏÀ´ÖØÏÖÕâ¸öbug³ÉÁËеÄÌôÕ½¡£ÒòΪABCÈý¸ö±í¼ÓÆðÀ´ÓÐ200G£¬¶øÈç¹ûÎÒɾµôһЩ¿´ÆðÀ´²»Ïà¹ØµÄÐУ¬´íÎó¾ÍÏûʧÁË¡£ÎÒÒ²ÊÔͼ½«ABCµÄJION·Ö³ÉÁ½²½À´×ö£¬
±ÈÈ磺
select *
from
(SELECT * from A JOIN B) AS D
JOIN C ON...
½á¹ûÈÔÈ»´íÎó¡£
µ«ÊÇÈç¹ûÔÙ½øÒ»²½£¬
SElect * into temp from A join B...
Select * from temp join C...
´íÎóÏûʧÁË¡£
Ç¿¼¼ÇîÖ®ºó£¬°³Ö»ºÃÇó¾ÈÓÚSQl serverµÄ¼¼ÊõÖ§³Ö¡£¸Õ¿ªÊ¼·¢ÁË·âÐÅ£¬ÎÊÓÐûÓÐÈËÓöµ½¹ýÕâÖÖÎÊÌ⣬ABC ½»³öµÄ½á¹û°üº¬²»Âú×ãJOINÌõ¼þµÄÐС£½á¹ûÒ»¸öС×Ó¾ÓȻ˵£¬Õâ¸öÎÊÌâÌ«»ù±¾£¬ÕâÖÖÇé¿ö¸ù±¾²»¿ÉÄܳöÏÖ¡£ Sigh£¬´óÊåÎÒÒ²²»ÊÇÐÂÊÖ£¬¼òµ¥ÎÊÌ⻹ÐèÒªÀÍ·³ÄúÀÏÈ˼ÒÂð¡£
ÓÚÊÇÔÙ×ÐϸµÄÃèÊöÎÊÌ⣬ÖÕÓÚ³öÁËÒ»¸ö±È½Ï¿¿Æ׵ĸçÃÇ£¬ÈÃÎÒ°ÑÖ´Ðмƻ®·¢¸øËû¿´¿´¡£ÔÙÈ»ºóÁíÍâÒ»¸ö¸çÃÇ˵¿ÉÄÜÊDz¢·¢ÒýÆðµÄÎÊÌ⣬½¨ÒéÎÒÓÃMAXDOPÀ´ÏÞÖƲÎÓëÖ´ÐеÄCPU¸öÊý¡£ÓÚÊÇ£¬ÎÒÔÚÔÀ´µÄSQLºóÃæ¼ÓÁ˸öOPTION MAXDOP=1£¬¹ûÈ»¾¹ý7·ÖÖÓµÄÖ´ÐУ¬Îҵõ½ÁËÕýÈ·µÄ½
Ïà¹ØÎĵµ£º
²éѯËÙ¶ÈÂýµÄÔÒòºÜ¶à£¬³£¼ûÈçϼ¸ÖÖ£º
¡¡¡¡1¡¢Ã»ÓÐË÷Òý»òÕßûÓÐÓõ½Ë÷Òý(ÕâÊDzéѯÂý×î³£¼ûµÄÎÊÌ⣬ÊdzÌÐòÉè¼ÆµÄȱÏÝ)
¡¡¡¡2¡¢I/OÍÌÍÂÁ¿Ð¡£¬ÐγÉÁËÆ¿¾±Ð§Ó¦¡£
¡¡¡¡3¡¢Ã»Óд´½¨¼ÆËãÁе¼Ö²éѯ²»ÓÅ»¯¡£
¡¡¡¡4¡¢ÄÚ´æ²»×ã
¡¡¡¡5¡¢ÍøÂçËÙ¶ÈÂý
¡¡¡¡6¡¢²éѯ³öµÄÊý¾ÝÁ¿¹ý´ó(¿ÉÒÔ²ÉÓöà´Î²éѯ£¬ÆäËûµÄ·½·¨½µµÍÊý¾ÝÁ¿)
¡¡¡¡7¡¢ ......
[code=SQL][/code]
--Óï ¾ä ¹¦ ÄÜ
--Êý¾Ý²Ù×÷
SELECT --´ÓÊý¾Ý¿â±íÖмìË÷Êý¾ÝÐкÍÁÐ
INSERT --ÏòÊý¾Ý¿â±íÌí¼ÓÐÂÊý¾ÝÐÐ
DELETE --´ÓÊý¾Ý¿â±íÖÐɾ³ýÊý¾ÝÐÐ
UPDATE --¸üÐÂÊý¾Ý¿â±íÖеÄÊý¾Ý
--Êý¾Ý¶¨Òå
CREATE TABLE --´´½¨Ò»¸öÊý¾Ý¿â±í
DROP TABLE --´ÓÊý¾Ý ......
List<Class1> li = dc.Table_1.Where(item => item.id > 1).Cast<Class1>().ToList();
Table_1 Ϊ×Ô¶¯Éú³ÉÀà, class1Ϊ×Ô¶¨ÒåÀà,ÊôÐÔÃû³Æ¶¼ÊÇÒ»ÑùµÄ,ÒªÄÜʵÏÖÉÏÃæµÄÓï¾ä,±ØÐëÓм¸¸öÌõ¼þ
Table_1 ÐèÒª¼Ì³Ðclass1,¿ÉÒÔͨ¹ýpartialÀàÀ´ÊµÏÖ
Table_1 ÊôÐÔ¼Óoverride
class1ÀïÃæµÄÊôÐÔÒªÊÇvir ......
1.¶àwhere£¬ÉÙhaving
whereÓÃÀ´¹ýÂËÐУ¬havingÓÃÀ´¹ýÂË×é
2.¶àunion all£¬ÉÙunion
unionɾ³ýÁËÖظ´µÄÐУ¬Òò´Ë»¨·ÑÁËһЩʱ¼ä
3.¶àExists£¬ÉÙin
ExistsÖ»¼ì²é´æÔÚÐÔ£¬ÐÔÄܱÈinÇ¿ºÜ¶à£¬ÓÐЩÅóÓѲ»»áÓÃExists£¬¾Í¾Ù¸öÀý×Ó
Àý£¬ÏëÒªµÃµ½Óе绰ºÅÂëµÄÈ˵Ļù±¾ÐÅÏ¢£¬table2ÓÐÈßÓàÐÅÏ¢
select * from table1;--(id,n ......
USE [sfaecrm]
GO
/****** ¶ÔÏó: StoredProcedure [dbo].[proc_insert] ½Å±¾ÈÕÆÚ: 05/29/2009 06:16:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[proc_insert] (@tablename varchar(256))
as
begin
set ......