ʹÓù«Óñí±í´ïʽ£¨CTE£©¼ò»¯Ç¶Ì×SQL ºÍ½øÐеݹéµ÷ÓÃ
1.ʹÓÃCTE¼ò»¯Ç¶Ì×sql
ÏÈ¿´ÏÂÃæÒ»¸öǶÌ׵IJéѯÓï¾ä£º
select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')
ÉÏÃæµÄ²éѯÓï¾äʹÓÃÁËÒ»¸ö×Ó²éѯ¡£ËäÈ»ÕâÌõSQLÓï¾ä²¢²»¸´ÔÓ£¬µ«Èç¹ûǶÌ׵IJã´Î¹ý¶à£¬»áʹSQLÓï¾ä·Ç³£ÄÑÒÔÔĶÁºÍά»¤¡£Òò´Ë£¬Ò²¿ÉÒÔʹÓñí±äÁ¿µÄ·½Ê½À´½â¾öÕâ¸öÎÊÌ⣬SQLÓï¾äÈçÏ£º
declare @t table(CountryRegionCode nvarchar(3))
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')
select * from person.StateProvince where CountryRegionCode
in (select * from @t)
ËäÈ»ÉÏÃæµÄSQLÓï¾äÒª±ÈµÚÒ»ÖÖ·½Ê½¸ü¸´ÔÓ£¬µ«È´½«×Ó²éѯ·ÅÔÚÁ˱í±äÁ¿@tÖУ¬ÕâÑù×ö½«Ê¹SQLÓï¾ä¸üÈÝÒ×ά»¤£¬µ«ÓÖ»á´øÀ´ÁíÒ»¸öÎÊÌ⣬¾ÍÊÇÐÔÄܵÄËðʧ¡£ÓÉÓÚ±í±äÁ¿Êµ¼ÊÉÏʹÓÃÁËÁÙʱ±í£¬´Ó¶øÔö¼ÓÁ˶îÍâµÄI/O¿ªÏú£¬Òò´Ë£¬±í±äÁ¿µÄ·½Ê½²¢²»Ì«ÊʺÏÊý¾ÝÁ¿´óÇÒƵ·±²éѯµÄÇé¿ö¡£Îª´Ë£¬ÔÚSQL Server 2005ÖÐÌṩÁËÁíÍâÒ»ÖÖ½â¾ö·½°¸£¬Õâ¾ÍÊǹ«Óñí±í´ïʽ£¨CTE£©£¬Ê¹ÓÃCTE£¬¿ÉÒÔʹSQLÓï¾äµÄ¿Éά»¤ÐÔ£¬Í¬Ê±£¬CTEÒª±È±í±äÁ¿µÄЧÂʸߵöࡣ
ÏÂÃæÊÇCTEµÄÓï·¨£º
[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,n ] ) ]
AS
( CTE_query_definition )
ÏÖÔÚʹÓÃCTEÀ´½â¾öÉÏÃæµÄÎÊÌ⣬SQLÓï¾äÈçÏ£º
with
cr as
(
select CountryRegionCode from person.CountryRegion where 
Ïà¹ØÎĵµ£º
Äã´´½¨µÄÿһ¸ö±¸·Ý¶¼ÊÇÒ»¸ö±¸·ÝÉ豸£¬¹ØÓÚËüµÄϸ½ÚÐÅÏ¢¶¼´æ´¢ÔÚmsdb..backupset±íÖС£Ò»¸ö±¸·ÝÉ豸¿ÉÒÔ±»´æ´¢ÔÚµ¥Ò»Îļþ£¬»òÊǶà¸öÎļþÖС£Í¬Ñù£¬Ò»¸öÎļþÒ²¿ÉÒÔ´æ´¢¶à¸ö±¸·ÝÉ豸¡£
ËùÒÔ£¬¼ÙÈçÄãÿ´Î±¸·Ý¶¼Ê¹ÓÃÏàͬµÄÎļþÃû£¬Îļþ¾Í»áÒ»Ö±Ôö³¤¡£Ò»¸öÆÕ±éµÄÎó½âÊÇ£ºÈç¹ûÄãÿ´ÎʹÓÃÏàͬµÄÎļþÃû£¬ÄǾɵı¸·ÝÉ豸¾Í»á±»¸²¸Ç¡ ......
1.Union
UNION Ö¸ÁîµÄÄ¿µÄÊǽ«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÆðÀ´¡£´ÓÕâ¸ö½Ç¶ÈÀ´¿´£¬ UNION ¸ú JOIN ÓÐЩÐíÀàËÆ£¬ÒòΪÕâÁ½¸öÖ¸Á¿ÉÒÔÓɶà¸ö±í¸ñÖÐߢȡ×ÊÁÏ¡£ UNION µÄÒ»¸öÏÞÖÆÊÇÁ½¸ö SQL Óï¾äËù²úÉúµÄÀ¸Î»ÐèÒªÊÇͬÑùµÄ×ÊÁÏÖÖÀà¡£ÁíÍ⣬µ±ÎÒÃÇÓà UNIONÕâ¸öÖ¸Áîʱ£¬ÎÒÃÇÖ»»á¿´µ½²»Í¬µÄ×ÊÁÏÖµ (ÀàËÆ SELECT DISTINCT)¡£
UNION µ ......
inner join(µÈÖµÁ¬½Ó) Ö»·µ»ØÁ½¸ö±íÖÐÁª½á×Ö¶ÎÏàµÈµÄÐÐ
left join(×óÁª½Ó) ·µ»Ø°üÀ¨×ó±íÖеÄËùÓмǼºÍÓÒ±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
right join(ÓÒÁª½Ó) ·µ»Ø°üÀ¨ÓÒ±íÖеÄËùÓмǼºÍ×ó±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
INNER JOIN Óï·¨£º
INNER JOIN Á¬½ÓÁ½¸öÊý¾Ý±íµÄÓ÷¨£º
SELECT * from ±í1 INNER JOIN ±í2 ON ±í1.×ֶκÅ=±í2 ......
DECLARE @sql varchar(8000)
--ÏÂÃæÊÇɾ³ý15ÌìÇ°±¸·ÝÎļþµÄ´¦Àí
set @sql='Del E:\Databasebackup\'
@sql=@sql +rtrim(convert(varchar,getdate()-15,112))+'.bak'
Exec master..xp_cmdshell @sql
DECLARE @DelFile Varchar(256)
set ......
Ò»¡¢PowerDesignerÉú³ÉsqlÎÊÌâ
Éú³ÉsqlµÄ·½·¨ÊÇ Database -->Generate Database (Ctrl + G ) µ«ÊÇÌáʾ
Generation aborted due to errors detected during the verification of the model.
½â¾ö·½·¨: ½«check model È¥µô¾Í¿ÉÒÔÁË.ÆäÖУ¬one file onÊÇ·ñÐèÒª°´ÕÕ±íÉú³ÉÐí¶à¸ösql£¨Ä¬ÈÏÑ¡ÉÏ£¬¼´²»ÐèÒª£©
& ......