SQL ¶¯Ì¬ÅÅÐòµÄ·½·¨
ÔÚ¹«¹²ÐÂÎÅ×éÖУ¬Ò»¸ö¾³£³öÏÖµÄÎÊÌâÊÇ“ÔõÑù²ÅÄܸù¾Ý´«µÝ¸ø´æ´¢¹ý³ÌµÄ²ÎÊý·µ»ØÒ»¸öÅÅÐòµÄÊä³ö£¿”¡£ÔÚһЩ¸ßˮƽר¼ÒµÄ°ïÖú֮ϣ¬ÎÒÕûÀí³öÁËÕâ¸öÎÊÌâµÄ¼¸ÖÖ½â¾ö·½°¸¡£
Ò»¡¢ÓÃIF...ELSEÖ´ÐÐÔ¤ÏȱàдºÃµÄ²éѯ
¡¡¡¡¶ÔÓÚ´ó¶àÊýÈËÀ´Ëµ£¬Ê×ÏÈÏëµ½µÄ×ö·¨Ò²ÐíÊÇ£ºÍ¨¹ýIF...ELSEÓï¾ä£¬Ö´Ðм¸¸öÔ¤ÏȱàдºÃµÄ²éѯÖеÄÒ»¸ö¡£ÀýÈ磬¼ÙÉèÒª´ÓNorthwindÊý¾Ý¿â²éѯµÃµ½Ò»¸ö»õÖ÷£¨Shipper£©µÄÅÅÐòÁÐ±í£¬·¢³öµ÷ÓõĴúÂëÒÔ´æ´¢¹ý³Ì²ÎÊýµÄÐÎʽָ¶¨Ò»¸öÁУ¬´æ´¢¹ý³Ì¸ù¾ÝÕâ¸öÁÐÅÅÐòÊä³ö½á¹û¡£Listing 1ÏÔʾÁËÕâÖÖ´æ´¢¹ý³ÌµÄÒ»¸ö¿ÉÄܵÄʵÏÖ£¨GetSortedShippers´æ´¢¹ý³Ì£©¡£
¡¾Listing 1: ÓÃIF...ELSEÖ´Ðжà¸öÔ¤ÏȱàдºÃµÄ²éѯÖеÄÒ»¸ö¡¿
CREATE PROC GetSortedShippers
@OrdSeq AS int
AS
IF @OrdSeq = 1
SELECT * from Shippers ORDER BY ShipperID
ELSE IF @OrdSeq = 2
SELECT * from Shippers ORDER BY CompanyName
ELSE IF @OrdSeq = 3
SELECT * from Shippers ORDER BY Phone
¡¡¡¡ÕâÖÖ·½·¨µÄÓŵãÊÇ´úÂëºÜ¼òµ¥¡¢ºÜÈÝÒ×Àí½â£¬SQL ServerµÄ²éѯÓÅ»¯Æ÷Äܹ»ÎªÃ¿Ò»¸öSELECT²éѯ´´½¨Ò»¸ö²éѯÓÅ»¯¼Æ»®£¬È·±£´úÂë¾ßÓÐ×îÓŵÄÐÔÄÜ¡£ÕâÖÖ·½·¨×îÖ÷ÒªµÄȱµãÊÇ£¬Èç¹û²éѯµÄÒªÇó·¢ÉúÁ˸ı䣬Äã±ØÐëÐ޸Ķà¸ö¶ÀÁ¢µÄSELECT²éѯ——ÔÚÕâÀïÊÇÈý¸ö¡£
¶þ¡¢ÓÃÁÐÃû×Ö×÷Ϊ²ÎÊý
¡¡¡¡ÁíÍâÒ»¸öÑ¡ÔñÊÇÈòéѯÒÔ²ÎÊýµÄÐÎʽ½ÓÊÕÒ»¸öÁÐÃû×Ö¡£Listing 2ÏÔʾÁËÐ޸ĺóµÄGetSortedShippers´æ´¢¹ý³Ì¡£CASE±í´ïʽ¸ù¾Ý½ÓÊÕµ½µÄ²ÎÊý£¬È·¶¨SQL ServerÔÚORDER BY×Ó¾äÖÐʹÓÃÄÄÒ»¸öÁÐÖµ¡£×¢Ò⣬ORDER BY×Ó¾äÖеıí´ïʽ²¢Î´ÔÚSELECTÇåµ¥ÖгöÏÖ¡£ÔÚANSI SQL-92±ê×¼ÖУ¬ORDER BY×Ó¾äÖв»ÔÊÐí³öÏÖûÓÐÔÚSELECTÇåµ¥ÖÐÖ¸¶¨µÄ±í´ïʽ£¬µ«ANSI SQL-99±ê×¼ÔÊÐí¡£SQL ServerÒ»Ö±ÔÊÐíÕâÖÖÓ÷¨¡£
¡¾Listing 2£ºÓÃÁÐÃû×Ö×÷Ϊ²ÎÊý£¬µÚÒ»´Î³¢ÊÔ¡¿
CREATE PROC GetSortedShippers
@ColName AS sysname
AS
SELECT *
from Shippers
ORDER BY
CASE @ColName
WHEN 'ShipperID' THEN ShipperID
WHEN 'CompanyName' THEN CompanyName
WHEN 'Phone' THEN Phone
ELSE NULL
END
¡¡¡¡ÏÖÔÚ£¬ÎÒÃÇÀ´ÊÔÒ»ÏÂеĴ洢¹ý³Ì£¬ÒÔ²ÎÊýµÄÐÎʽָ¶¨ShipperIDÁУº
EXEC GetSortedShippers 'ShipperID'
¡¡¡¡´ËʱһÇÐÕý³£¡£µ«ÊÇ£¬µ±ÎÒÃÇÊÓͼ°ÑCompanyNameÁÐ×÷Ϊ²ÎÊýµ÷Óô洢¹ý³Ìʱ£¬Ëü²»ÔÙÓÐЧ£º
EXEC GetSortedShippers 'CompanyName'
¡¡¡¡×Ðϸ¿´Ò»Ï´íÎóÐÅÏ¢£º
Server
Ïà¹ØÎĵµ£º
http://www.umgr.com/blog/PostView.aspx?bpId=36294
1. Ö´ÐÐsqlÓï¾ä
int sqlite3_exec(sqlite3*£¬ const char *sql£¬ sqlite3_callbacksql Óï·¨
£¬ void *£¬ char **errmsg );
Õâ¾ÍÊÇÖ´ÐÐÒ»Ìõ sql Óï¾äµÄº¯Êý¡£
µÚ1¸ö²ÎÊý²»ÔÙ˵ÁË£¬ÊÇÇ°Ãæopenº¯ÊýµÃµ½µÄÖ¸Õ롣˵ÁËÊǹؼüÊý¾Ý½á¹¹¡£
µÚ2¸ö²ÎÊýconst char ......
1.Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)¡¡¡¡
¡¡¡¡ SQLSERVERµÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬Òò´Ëfrom×Ó¾äÖÐдÔÚ×îºóµÄ±í£¨»ù´¡±ídriving table£©½«±»×îÏÈ´¦Àí£¬ÔÚfrom×Ó¾äÖаüº¬¶à¸ö±íµÄÇé¿öÏ£¬±ØÐëÑ¡Ôñ¼Ç¼ÌõÊý×îÉٵıí×÷Ϊ»ù´¡±í£¬µ±SQLSERVER´¦Àí¶à¸ö±íʱ£¬»áÔËÓÃÅÅÐò¼°ºÏ²¢µÄ·½Ê½Á ......
--sql structured query language
--DML--Data Manipulation Language--Êý¾Ý²Ù×÷ÓïÑÔ
query information (SELECT),
add new rows (INSERT),
modify existing rows (UPDATE),
delete existing rows (DELETE),
perform a conditional update or insert operation (MERGE),
see an execution plan of SQL (EXPLA ......
ÕâÁ½Ì춼ûÓкúÃѧϰ£¬½ñÌìÖÕÓÚËãͶÈëÁËЩ£¬ÓÉÓڿγ̵ĹØÏµ£¬ÎÒµÄsqlÒ²ÊÇͬvbÒ»ÆðѧϰµÄ¡£ËäȻ֪µÀ̰¶à½À²»À㬿ÉÊǰ´ÕÕʵ¼ÊÇé¿ö£¬ÎÒÍêȫûÓÐÀíÓÉÅׯúsqlµÄ¡£
×î½ü¶¼°Ñʱ¼äͶÈëµ½vbºÍÃæÏò¶ÔÏóÖУ¬sql½ñÌìºÃºÃ¸´Ï°ÁËÏ£¬´Ócreate database ¿ªÊ¼£¬create table£¬alter table £¬ add constraint …&h ......
ÒÔÏÂΪ±¾ÈËÕ⼸ÌìÖØÐÂѧϰSQLSERVER2000µÄѧϰ±Ê¼Ç£¬Ä¿µÄÊÇÖØÐÂÈÏʶ¼°¹®¹Ì֪ʶµã£º
Ò»¡¢¼òµ¥µÄSELECTÓï¾äµÄÓï·¨ÈçÏ£º
SELECT |ALL|DISTINCT| <Ä¿±ê±í´ïʽ>|,<Ä¿±ê±í´ïʽ>|...
from<±í»òÊÓͼÃû>|,<±í»òÊÓͼÃû>|
[WHERE<Ìõ¼þ±í´ïʽ>]
[GROUP BY <ÁÐÃû1>|HAVING<Ìõ¼þ±í´ïʽ>| ......