«á¤@­¶
«e¤@­¶
¦^¥Ø¿ý
¦^­º­¶
²Ä¤Q¤C³¹¡@SQLµ{¦¡³]­p¡]¤@¡^

SQL»y¨¥§@¬°Ãö«Y¸ê®Æ®wºÞ²z¨t²Î¤¤ªº¤@ºØ¤@¯ëªºªºµ²ºc¬d¸ß»y¨¥¡A ¤w¸g³Q²³¦hªº¸ê®Æ®wºÞ²z¨t²Î©Ò±Ä¥Î¡A¦pORACLE¡BSybase¡BInformixµ¥¸ê®Æ®wºÞ²z¨t²Î¡A¥¦­Ì³£¤ä´©SQL »y¨¥¡CDelphi»P¨Ï¥ÎSQL»y¨¥ªº¸ê®Æ®wºÞ²z¨t²Î¬Û®e¡A¦b¨Ï¥ÎDelphi¶}µo¸ê®Æ®wÀ³¥Îµ{¦¡®É¡A§Ú­Ì¥i¥H¨Ï¥ÎSQL»y¨¥µ{¦¡³]­p¡A¤ä´©SQLµ{¦¡³]­p¬ODelphiªº¤@­Ó­«­n¯S©º¡A³o¤]¬OÅé²{Delphi§@¬°¤@­Ó±j¤jªº¸ê®Æ®wÀ³¥Î¶}µo¤u¨ãªº¤@­Ó­«­n¼Ð»x¡C¡@

17.1 SQL»y¨¥Â²¤¶¡@

17.1.1 SQLªº¾ú¥v¡@

¦b70¦~¥Nªì¡AE.E.Codd­º¥ý´£¥X¤FÃö«Y¼Ò«¬¡C70¦~¥N¤¤´Á¡AIBM¤½¥q¦b¬ã¨î SYSTEM RÃö«Y¸ê®Æ®wºÞ²z¨t²Î¤¤¬ã¨î¤FSQL»y¨¥¡A³Ì¦­ªºSQL»y¨¥(¥sSEQUEL2)¬O¦b1976 ¦~ 11 ¤ëªºIBM Journal of R&D¤W¤½§Gªº¡C1979¦~ORACLE¤½¥q­º¥ý´£¨Ñ°Ó¥ÎªºSQL¡AIBM¤½¥q¦bDB2 ©MSQL/DS¸ê®Æ®w¨t²Î¤¤¤]¹ê²{¤FSQL¡C

1986¦~10¤ë¡A¬ü°êANSI±Ä¥ÎSQL§@¬°Ãö«Y¸ê®Æ®wºÞ²z¨t²Îªº¼Ð·Ç»y¨¥(ANSI X3. 135-1986)¡A«á¬°°ê»Ú¼Ð·Ç¤Æ¸s¸s²Õ´(ISO)±Ä¯Ç¬°°ê»Ú¼Ð·Ç¡C1989¦~¡A¬ü°êANSI±Ä¯Ç¦bANSI X3.135-1989³ø§i¤¤©w¸qªºÃö«Y¸ê®Æ®wºÞ²z¨t²ÎªºSQL¼Ð·Ç»y¨¥¡AºÙ¬°ANSI SQL 89¡A ¸Ó¼Ð·Ç´À¥NANSI X3.135-1986ª©¥»¡C¸Ó¼Ð·Ç¬°¤U¦C¸s¸s²Õ´©Ò±Ä¯Ç¡G

¡´ °ê»Ú¼Ð·Ç¤Æ¸s¸s²Õ´(ISO)¡A¬°ISO 9075-1989³ø§i¡§Database Language SQL With Integrity Enhancement¡¨

¡´ ¬ü°êÁp¨¹¬F©²¡Aµo§G¦bThe Federal Information Processing Standard Publication(FIPS PUB)127

¥Ø«e¡A©Ò¦³¥D­nªºÃö«Y¸ê®Æ®wºÞ²z¨t²Î¤ä´©¬Y¨Ç§Î¦¡ªºSQL»y¨¥¡A ¤j³¡¤À¸ê®Æ®w¥´ºâ¿í¦uANSI SQL89¼Ð·Ç¡C¡@

17.1.2 SQLªºÀuÂI

SQL¼sªx¦a³Q±Ä¥Î¥¿±Ô­z¤F¥¦ªºÀuÂI¡C¥¦¨Ï¥þ³¡¥Î¤á¡A¥]¬AÀ³¥Îµ{¦¡­û¡BDBAºÞ²z­û©M²×ºÝ¥Î¤á¨ü¯q«D²L¡C

(1) «D¹Lµ{¤Æ»y¨¥

SQL¬O¤@­Ó«D¹Lµ{¤Æªº»y¨¥¡A¦]¬°¥¦¤@¦¸³B²z¤@­Ó°O¿ý¡A¹ï¼Æ¾Ú´£¨Ñ¦Û°Ê¾É¯è¡CSQL¤¹³\¥Î¤á¦b°ª¼hªº¼Æ¾Úµ²ºc¤W¤u§@¡A¦Ó¤£¹ï³æ­Ó°O¿ý¶i¦æ¾Þ§@¡A¥i¾Þ§@°O¿ý¶°¡C©Ò¦³SQL »y¥y±µ¨ü¶°¦X§@¬°¿é¤J¡Aªð¦^¶°¦X§@¬°¿é¥X¡CSQLªº¶°¦X¯S©Ê¤¹³\¤@±øSQL»y¥yªºµ²ªG§@¬°¥t¤@±øSQL»y¥yªº¿é¤J¡C

SQL¤£­n¨D¥Î¤á«ü©w¹ï¼Æ¾Úªº¦s©ñ¤èªk¡C ³oºØ¯S©Ê¨Ï¥Î¤á§ó©ö¶°¤¤ºë¤O©ó­n±o¨ìªºµ²ªG¡C©Ò¦³SQL»y¥y¨Ï¥Î¬d¸ßÀu¤Æ¾¹¡A¥¦¬ORDBMSªº¤@³¡¤À¡A¥Ñ¥¦¨M©w¹ï«ü©w¼Æ¾Ú¦s¨úªº³Ì§Ö³t«×ªº¤â¬q¡C¬d¸ßÀu¤Æ¾¹ª¾¹D¦s¦b¤°»ò¯Á¤Þ¡A­þ¨à¨Ï¥Î¦X¾A¡A¦Ó¥Î¤á±q¤£»Ý­nª¾¹Dªí¬O§_¦³¯Á¤Þ¡Aªí¦³¤°»ò«¬¦¡ªº¯Á¤Þ¡C

(2) ²Î¤@ªº»y¨¥

SQL¥i¥Î©ó©Ò¦³¥Î¤áªºDB¬¡°Ê¼Ò«¬¡A¥]¬A¨t²ÎºÞ²z­û¡B¸ê®Æ®wºÞ²z­û¡B À³¥Îµ{¦¡­û¡B¨Mµ¦¤ä´©¨t²Î¤H­û¤Î³\¦h¨ä¥¦«¬¦¡ªº²×ºÝ¥Î¤á¡C°ò¥»ªºSQL ©R¥O¥u»Ý«Ü¤Ö®É¶¡´N¯à¾Ç·|¡A³Ì¶i¶¥ªº©R¥O¦b´X¤Ñ¤º«K¥i´x´¤¡C

SQL¬°³\¦h¤u´£¨Ñ¤F©R¥O¡A¥]¬A¡G

¡´ ¬d¸ß¼Æ¾Ú

¡´ ¦bªí¤¤´¡¤J¡B­×§ï©M¸Ñ°£°O¿ý

¡´ «Ø¥ß¡B­×§ï©M¸Ñ°£¼Æ¾Úª«¥ó

¡´ ±±¨î¹ï¼Æ¾Ú©M¼Æ¾Úª«¥óªº¦s¨ú

¡´ «OÃÒ¸ê®Æ®w¤@­P©Ê©M§¹¾ã©Ê

¡@

¥H«eªº¸ê®Æ®wºÞ²z¨t²Î¬°¤W­z¦UÃþ¾Þ§@´£¨Ñ³æ¿Wªº»y¨¥¡A¦ÓSQL ±N¥þ³¡¥ô°È²Î¤@¦b¤@ºØ»y¨¥¤¤¡C

(3) ¬O©Ò¦³Ãö«Y¸ê®Æ®wªº¤½¦@»y¨¥

¥Ñ©ó©Ò¦³¥D­nªºÃö«Y¸ê®Æ®wºÞ²z¨t²Î³£¤ä´©SQL»y¨¥¡A¥Î¤á¥i±N¨Ï¥ÎSQLªº§Þ¯à±q¤@­ÓRDBMSÂà¨ì¥t¤@­Ó¡C©Ò¦³¥ÎSQL½s¼gªºµ{¦¡³£¬O¥i¥H²¾´Óªº¡C

¡@

17.2 TQuery³¡¥ó¦bSQLµ{¦¡³]­p¤¤ªº¹B¥Î

¡@

¦bDelphi¤¤¬O³q¹LTQuery³¡¥ó¨Ó¹ê²{¹ïSQL»y¨¥¤ä´©ªº¡A¤]´N¬O»¡¥ÎDelphi ¶}µo¸ê®Æ®wÀ³¥Îµ{¦¡®É¡A¨Ï¥ÎSQL»y¨¥¾Þ§@¸ê®Æ®w¤¤ªº¼Æ¾Úªº°ß¤@³~®|¬O¸g¹LTQuery³¡¥ó¡C TQuery³¡¥ó¦bDelphi¤¤¨Ï¥ÎSQL»y¨¥µ{¦¡³]­p®É¦û©~µÛµ´¹ï­«­nªº¦a¦ì¡C¦b¨Ï¥ÎDelphi ¶}µoªº¸ê®Æ®wÀ³¥Î¤¤¡A¥i¥H¨Ï¥ÎSQL»y¨¥¦s¨ú¤U¦C¤T­Ó¤è­±ªº¸ê®Æ®w¡G

¡´ Paradox©ÎdBASE¸ê®Æ®w¤¤ªºªí

¦b¦s¨ú³o¨Ç®à­±¸ê®Æ®w¨t²Î¤¤ªº¼Æ¾Ú®É¡A¥u¯à¨Ï¥ÎANSI¼Ð·ÇªºSQL»y¨¥¤¤ªº³¡¤ÀSQL »y¥y¡A¥¦­Ì¥D­n¥]¬A¡GSelect¡BInsert¡BUpdate©MDelete»y¥y¡F§Y¥»¦aSQL»y¥y¡C¦³Ãö¸Ô²Ó±¡ªp½Ð°Ñ¨£ªþ¿ý¡§§½³¡SQL»y¥yªº¨Ï¥Î¡¨¡C

¡´ ¥»¦aInterBase¸ê®Æ®w¦øªA¾¹¤¤ªº¸ê®Æ®w

¦bInterBase¸ê®Æ®w¤¤¤ä´©ªºSQL»y¥y¡A ¦bDelphi¤¤³£¥i¥H¨Ï¥Î¡C¦³ÃöInterBase¤¤SQL»y¥yªº»yªk©M­­¨î¡A½Ð°Ñ¬Ý¡§InterBaseªº»y¨¥°Ñ¦Ò¡¨¡C

¡´ »·ºÝ¸ê®Æ®w¦øªA¾¹¤¤ªº¸ê®Æ®w

·íµM³o­n¨D¦bDelphi¤¤¥²¶·¸Ë³]¬ÛÀ³ªºSQL Link¡C¥u­n¬O¸ê®Æ®w¦øªA¾¹¤WªºDBMS¤ä´©ªºSQL»y¥y¡A¦bDelphi¤¤³£¥i¥H¨Ï¥Î¡C¦³Ãö»yªk ¤Î­­¨î½Ð°Ñ¬Ý¬ÛÃöªº¸ê®Æ®wºÞ²z ¨t²Îªº¤åÀÉ¡C

¡@

­È±o¤@´£ªº¬O¡ADelphiÁ٤䴩²§ºc¬d¸ß¡A§Y¥i¥H¦P®É¬d¸ß¦h­Ó¸ê®Æ®w¦øªA¾¹¤¤¬Û¦Pªº©Î¤£¦P«¬¦¡ªº¸ê®Æ®wªí¡A¨Ò¦p¬d¸ßªº¼Æ¾Ú¥i¥H¬O¨Ó¦ÛORACLE¸ê®Æ®w¤¤ªºªí©MSybase¸ê®Æ®w¤¤ªºªí©ÎªÌ¨ä¥¦¦h­Ó¸ê®Æ®w¤¤ªºªí¡C

¡@

17.2.1 TQuery³¡¥óªº¨Ï¥Î

¡@

TQuery³¡¥ó¬O¤@­Ó¼Æ¾Ú¶°³¡¥ó¡A¥¦¦bDelphi³¡¥ó¿ï¾ÜªO¤Wªº¼Æ¾Ú¦s¨ú­¶(Data Access)¤W¡A¥¦»PTTable³¡¥ó¨ã¦³«Ü¦h¦@¦Pªº¯S©Ê¡A§Ú­Ì¦b²Ä¤Q¤­³¹¡§¼Æ¾Ú¦s¨ú³¡¥óªºÀ³¥Î¤Îµ{¦¡³]­p¡¨¤¤¸û¸Ô²Ó¦a¶i¦æ¤F¤¶²Ð¡C TQuery ³¡¥ó¦b SQL µ{¦¡³]­p¤¤¦û©~¤F¤Q¤À­«­nªº¦a¦ì¡C ¥¦¹ê²{¤FDelphi¹ïSQL»y¨¥ªº¤ä´©¡A¦bDelphi¶}µoªº¸ê®Æ®wÀ³¥Î¤¤¡ASQL»y¥y¬O³q¹LTQuery³¡¥ó¶Ç»¼¨ì­n¦s¨úªº¸ê®Æ®w¨t²Îªº¸ê®Æ®w¤ÞÀº¤¤¡A¥Ñ¸ê®Æ®w¤ÞÀº¨ãÅé°õ¦æSQL»y¥y¡A ¥H¹ê²{¹ï¼Æ¾Úªº¾Þ§@¡A¦Ó¤£¬O¶Ç»¼µ¹Delphi¤¤ªºBDE¡A¥ÑBDE¹ê¬I¨ãÅ骺SQL°Ê§@¡C

¡@

§Ú­Ì¤w¸gª¾¹D¤FTTable³¡¥ó¦b¦s¨ú¸ê®Æ®w®É¤w¸g¨ã³Æ«Ü±j¤jªº¥\¯à¡CTTable³¡¥ó³q¹LDelphi¤º¸mªºBDE¥i¥H¹ê²{¹ï¦UºØ¸ê®Æ®w¨t²Îªº¦s¨ú¡AµM¦ÓTQuery³¡¥ó´£¨Ñ¤F¤@¨Ç TTable³¡¥ó¤£¨ã³Æªº¥\¯à¡A¥¦­Ì¬O¡G

¡´¦hªíÁp±µ¬d¸ß

¡´½ÆÂøªº±_ª¬¬d¸ß(Select¤¤¥]§tµÛSelect¤l¬d¸ß)

¡´©ú½T»Ý­n«öSQL»y¨¥¶i¦æªº¾Þ§@

¡@

¦]¬°TTabel³¡¥ó¤£¯à¨Ï¥ÎSQL»y¨¥¡A¦Ó¦bTQuery³¡¥ó¥i¥H¨Ï¥ÎSQL»y¨¥¡A¦]¦ÓTQuery³¡¥ó¤]´N¨ã³Æ¤F±j¤jªºÃö«Y¬d¸ß¯à¤O¡C·íµM³o¤]¨Ï¸ê®Æ®wÀ³¥Îµ{¦¡¥»¨­Åܱo§ó½ÆÂø¤F¡C

¦bDelphiÀ³¥Îµ{¦¡¤¤½s¼g©M¨Ï¥ÎªºSQL»y¥y¦³¨âºØ¡G§YÀRºASQL»y¥y¡B°ÊºASQL »y¥y¡CÀRºASQL»y¥y¬O¦bµ{¦¡³]­p¶¥¬q¡A±NSQL©R¥O¤å¥»§@¬°TQuery³¡¥óªºSQLÄݩʭȳ]©w¡C ¦Ó°ÊºASQL»y¥yµ{¦¡³]­p¬OSQL»y¥y¤¤¥]§t¤@¨t¦Cªº°Ñ¼Æ¡A¦bµ{¦¡°õ¦æ¹Lµ{¤¤¦U°Ñ¼Æ­È¬O¥iÅܪº¡A§Y¥i¥H°ÊºA¦aµ¹SQL»y¥y¤¤ªº°Ñ¼Æ½á­È¡C

ÀRºA¤è¦¡¬O§âSQL©R¥O¤å§@¬°TQuery³¡¥óªºSQLÄݩʭȶi¦æ³]©w¡A³o¼Ë¡A·í°õ¦æÀ³¥Îµ{¦¡®É¡ADelphi«K°õ¦æTQuery³¡¥óSQLÄݩʤ¤³]©wªºSQL©R¥O¡C¦pªG¬OSQL¤¤ªº¬d¸ß©R¥O¡A §âTQuery³¡¥ó³q¹LTDataSource³¡¥ó»P¼Æ¾Ú±±¨î³¡¥ó¬Û³s¡A¬d¸ßªºµ²ªG±N·|Åã¥Ü¦b»P TQuery³¡¥ó¬Û³s±µªº¼Æ¾ÚÂsÄý³¡¥ó¤¤¡C°ÊºASQL»y¥y¬O«üSQL»y¥y¤¤¥]§t¤@¨Ç°Ñ¼ÆÅܼơA¦bµ{¦¡¤¤¥i¥H¬°³o¨Ç°Ñ¼Æ½á­È¡A¦bµ{¦¡°õ¦æ¹Lµ{¤¤¡A¦U­Ó°Ñ¼Æ­È¬OÅܤƪº¡CTQuery³¡¥óªºSQL Äݩʤ¤ªºSQL»y¥yªº½s¼g¤]¦³¨âºØ¤èªk¡A¤@ºØ¤èªk¬O¦bµ{¦¡³]©w¶¥¬q«K±N¬ÛÀ³ªºSQL»y¥y¼g¤J¨ìTQueryªºSQLÄݩʤ¤¡A¥t¤@ºØ¤èªk¬O¦bDelphi¶}µoªºÀ³¥Îµ{¦¡±NSQL»y¥y¡A¥]§t¦bPascal¥N½X³æ¤¸¤¤¡C

¦bSQLµ{¦¡³]­p¤¤¨Ï¥ÎTQuery³¡¥óªº¨ãÅé¤èªk¨BÆJ¦p¤U¡G

Æ¡¬°TQuery³¡¥ó³]©wDatabaseNameÄÝ©Ê¡A¥¦¥i¥H¬O¥ÎBDE«Ø¥ßªº¸ê®Æ®wªº§O¦W¡A ©Î®à­±¸ê®Æ®w¨t²Î¤¤ªº¥Ø¿ý¦W©Î¸ê®Æ®w¦øªA¾¹¤¤ªºÀÉ®×¼Ðñ¡A¦pªG¦bÀ³¥Î¤¤¨Ï¥Î¤FTDatabase ³¡¥ó¡A¨º»òTQuery³¡¥óªºDatabaseNameÄÝ©Ê¥i¥H¬OTDatabase³¡¥ó¤¤©w¸qªº¤@­Ó¸ê®Æ®w§O¦W¡C¸Ô²Ó±¡ªp½Ð°Ñ¬Ý¡§TDatabase³¡¥óªº¨Ï¥Î¡¨¡F

Æ¢¬°TQuery³¡¥ó³]©wSQLÄÝ©Ê¡ATQuery³¡¥óªºSQLÄÝ©Ê­È´N¬OÀ³¥Îµ{¦¡­n°õ¦æªºSQL ©R¥O¤å¥»¡A³]©wSQLÄݩʦ³¨âºØ¤èªk¡G

¡´ ¦bµ{¦¡³]­p¹Lµ{¤¤¡A§Ú­Ì¥i¥H³q¹Lª«¥óÂsÄý¾¹(Object Inspector)½s¿èSQLÄÝ©Ê ¦bObject Inspector¤¤¿ï¾ÜSQLÄÝ©Ê¡A³o¼Ë·|¥´¶}String List Editorµøµ¡¡A¦b¨ä ¤¤§Ú­Ì«K¥i¥H½s¼gSQL©R¥O¡A§ÚÁÙ¥i¥H¥´¶}Visual Query Builder¨Ó½s¼gSQL©R¥O (¥u¦³Delphiªº¥Î¤á/¦øªA¾¹ª©¥»¤~¨ã¦³³o¤@¤u¨ã)¡C

¡´ ±NSQL©R¥O¥]§t¦bPascal¥N½X³æ¤¸¤¤

¦bµ{¦¡°õ¦æ¹Lµ{¤¤¡A­º¥ý©I¥sTQuery³¡¥óªºClose¤èªkÃö³¬¥Ø«eªºTQuery³¡¥ó¡AµM «á©I¥sClear¤èªk²M°£SQLÄÝ©Ê¡A¨Ã±Ô­z·sªºSQL©R¥O¤å¥»¡AµM«á©I¥sAdd¤èªk¡A±N·sªºSQL©R¥O¤å¥»¥[¤J¨ìSQLÄݩʤ¤¡C

¡@

Æ£³q¹L©I¥sTQuery³¡¥óªºOpen¤èªk©ÎExecSQl¤èªk°õ¦æ SQL ©R¥O¡C Open ¤èªk¥u°õ¦æSelect©R¥O¡AExecSQL¤èªkÁÙ¥i¥H°õ¦æ¨ä¥¦ªºSQL©R¥O¡COpen¤èªk©MExecSQL ¤èªkªº°Ï§O§Ú­Ì¦b«á­±ªº³¹¸`ùØ·|¶i¤@¨B¦a¥[¥H°Q½×ªº¡C

¦pªG¨Ï¥Î°ÊºASQL»y¥y¡A­º¥ý©I¥sprepare¤èªk¡Aµ¹°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ½á­È¡A µM«á¦A©I¥sOpen¤èªk©ÎExecSQL¤èªk¡C©I¥spropare ¤èªk¨Ã¤£¬O¥²¶·ªº¡A ¦ý¬O¹ï©ó­n¦h¦¸°õ¦æTQuery³¡¥ó¤¤SQLÄݩʤ¤ªº°ÊºASQL»y¥y¡A©I¥sPrepare¥i¥H¤j¤j´£°ªTQuery³¡¥ó°õ¦æSQL»y¥yªº©Ê¯à¡C¡@

17.2.2 ¦bTQuery³¡¥ó¤¤½s¼g²³æªºSQL¬d¸ß©R¥O¡@

¦b³o¤@¸`ùاڭ̱N¾Ç²ß¦p¦ó¨Ï¥ÎTQuery³¡¥ó½s¼g²³æªºSQL¬d¸ß©R¥O¡A¨Ã¦bDelphi À³¥Îµ{¦¡¤¤¹ê²{SQL¬d¸ß¡C

¨Ò¦p¡A¦pªG§Ú­Ì·Q¬d¸ß¥XªíCustomer.DB¤¤¥Î¤áªº½s¸¹©M¤½¥q¼Ðñ¡A §Ú­Ì«ö¤U¦C¨BÆJ¨Ó¹ê²{¡G

Æ¡¦bÀ³¥Îµ¡Å餤©ñ¸m¤@­ÓTQuery³¡¥ó¡B¤@­ÓTDataSource³¡¥ó¤@­ÓTDataGrid³¡¥ó¡A¨Ã±N¥¦­Ì³s±µ°_¨Ó¡@

Æ¢³]©wµ¡ÅéTQuery ³¡¥óQuery1ªºDatabaseNameÄݩʭȬ°DBDEMOS

Æ£³sÄò«ö¨â¤UObject Inspectorµøµ¡¤¤Query1ªºSQL ÄÝ©Ê¡A Delphi ±NÅã¥Ü String List Editorµøµ¡¡C

Ƥ¦b¹Ï17.3¤¤ªºµøµ¡¤¤¿é¤JSQL»y¥y¡G

Select CustNo,Company From Custormer;

Æ¥«ö¤@¤UOK«ö¶s¡AÃö³¬String List Editorµøµ¡¡C

Ʀ³]©wQueryªºOpenÄݩʬ°True¡C

17.3 SQL»y¨¥µ{¦¡³]­p·§­z¡@

¦bDelphiÀ³¥Îµ{¦¡¤¤ªºSQL©R¥O»y¥y¬O¥]§t¦bTQuery³¡¥óªºSQLÄݩʤ¤¡ATQuery³¡¥óªºSQLÄݩʬOTString«¬¦¡ªº¡A¤]´N¬O»¡SQLÄݩʭȬO¤@­Ó¦r¤¸¦ê²M³æ¡A ³o­Ó¦r¤¸¦ê²M³æ«D±`Ãþ¦ü©ó¤@­Ó¦r¤¸¦ê«¬¦¡ªº¼Æ¸s¸s²Õ¡A¦³ÃöTString«¬¦¡ªº¸ê°T½Ð°Ñ¬Ý³s½u»²§U±Ô­z¡C ¦b«e¤@¸`ùاڭ̤¶²Ð¤FTQuery³¡¥ó¥i¥H°õ¦æ¨âºØSQL»y¥y¡G

¡´ ÀRºASQL»y¥y

¡´ °ÊºASQL»y¥y¡@

ÀRºASQL»y¥y¦bµ{¦¡³]­p®É«K¤w©T©w¤U¨Ó¡A¥¦¤£¥]§t¥ô¦ó°Ñ¼Æ©MÅܼơA ¨Ò¦p¤U­±ªº»y¥y«K¬O¤@±øÀRºASQL»y¥y¡G¡@

Select * From Cusromer Where CustNo = 1234;¡@

¦Ó°ÊºASQL»y¥y¡A¤]³QºÙ§@°Ñ¼Æ¤Æªº»y¥y¡A¦b¨ä¤¤¶¡¥]§tµÛªí¥ÜÄæ¦ì¦W©Îªí¦Wªº°Ñ¼Æ¡A¨Ò¦p¤U­±ªº»y¥y¬O¤@±ø°ÊºASQL»y¥y¡G¡@

Select * From Customer Where CustNo =: Number;

¡@

¨ä¤¤ªºÅܼÆNumber«K¬O¤@­Ó°Ñ¼ÆÅܼơA¥¦¥Ñ¤@­Ó«_¸¹¤Þ¾É¡A¦bµ{¦¡°õ¦æ¹Lµ{¤¤¡A¥²¶·­n¬°¸Ó°Ñ¼Æ½á­È¡A¸Ó±øSQL»y¥y¤~¯à¥¿½T°õ¦æ¡A ¨C¦¸°õ¦æÀ³¥Îµ{¦¡®É¥i¥H¬°¸Ó°Ñ¼ÆÅܼƽᤩ¤£¦Pªº­È¡C

¡@

17.3.1 SQL©R¥O¤å¥»ªº½s¼g

¡@

1. ¨Ï¥ÎString List Editor½s¼g

§Ú­Ì­n¬°TQuery³¡¥óªºSQLÄݩʳ]©wSQL©R¥O¤å¥»®É¡A¥i¥H¦bÀ³¥Îµ¡Å餤¿ï¾ÜTQuery³¡¥ó¥B³sÄò«ö¨â¤UObject Inspectorµøµ¡¤¤ªºSQLÄÝ©Ê¡A³o¼Ë«K¥´¶}¤FString List Editor µøµ¡¡A¦b¸Óµøµ¡¤¤§Ú­Ì«K¥i¥H½s¼g¦UºØSQL©R¥O¡A¦p¹Ï17.3©Ò¥Ü¡C

¦b½s¼g§¹¾A·íªºSQL»y¥y¤§«á¡A¿ï¾Ü OK «ö¶s«K¥i¥H±N½s¿è¾¹¤¤ªº SQL ©R¥O¤å¸ü¤J¨ìTQuery³¡¥óªºSQLÄݩʤ¤¡A¿ï¾ÜSAVE«ö¶s¥i¥H±N½s¼g¦nªºSQL©R¥O¦sÀɨì¤@­Ó¤å¥ó¤¤¨Ñ¥H«áµ{¦¡³]­p®É¨Ï¥Î¡C§Ú­Ì¦b½s¼gSQL©R¥O¤å¥»®ÉÁÙ¥i¥H¿ï¾ÜLoad«ö¶s±q¤@­Ó SQL ©R¥O¤å¥ó¤¤½Õ¤JSQL©R¥O¡C¦bµ{¦¡°õ¦æ¹Lµ{¤¤¡A­n·Q³]©wTQuery³¡¥óªºSQLÄÝ©Ê¡A¥²¶·­º¥ý©I¥sClose¤èªk¡AÃö³¬TQuery³¡¥ó¡AµM«á¦A©I¥sClear¤èªk²M°£SQLÄݩʤ¤²{¦sªºSQL©R¥O»y¥y¡A ³Ì«á¦A©I¥sAdd¤èªk¬°SQLÄݩʳ]©w·sªºSQL©R¥O»y¥y¡C¨Ò¦p¡G

¡@

Query1.Close {Ãö³¬Query1)

Query1.SQL.Clear {²M°£SQLÄݩʤ¤ªºSQL©R¥O»y¥y}

Query1.SQL.Add('Select * From Country');

Query1.SQL.Add('Where Name ="ARGENTINA" ');

¡@

¦b¬°TQuery³¡¥ó³]©wSQLÄݩʮɩI¥sClose¤èªkÁ`¬O«Ü¦w¥þªº¡A¦pªGTQuery³¡¥ó¤w¸g³QÃö³¬¤F¡A©I¥sClose¤èªk®É¤£·|²£¥Í¥ô¦ó¼vÅT¡C¦bÀ³¥Îµ{¦¡¤¤¬°SQLÄݩʳ]©w·sªºSQL ©R¥O»y¥y®É¡A¥²¶·­n©I¥sClear¤èªk¥H²M°£SQLÄݩʤ¤²{¦sªºSQL©R¥O»y¥y¡A¦pªG¤£©I¥sClear¤èªk¡A«K©I¥sAdd¤èªk¦VSQLÄݩʤ¤³]©wSQL©R¥O»y¥y¡A¨º»ò·s³]©wªºSQL©R¥O»y¥y·|°l¥[¦b²{¦sSQL©R¥O»y¥y«á­±¡A ¦bµ{¦¡°õ¦æ®É±`±`·|¥X²{¥X¥G·N®Æªº¬d¸ßµ²ªG¬Æ¦Üµ{¦¡µLªk°õ¦æ¤U¥h¡C

¦b³oùØ­n¯S§Oª`·Nªº¡A¤@¯ë±¡ªp¤UTQuery³¡¥óªºSQLÄÝ©Ê¥u¯à¥]§t¤@±ø§¹¾ãªºSQL»y¥y¡A¥¦¤£¤¹³\³Q³]©w¦¨¦h±øSQL»y¥y¡C·íµM¦³¨Ç¸ê®Æ®w¦øªA¾¹¤]¤ä´©¦bTQuery³¡¥óªºSQLÄݩʤ¤³]©w¦h±øSQL»y¥y¡A¥u­n¸ê®Æ®w¦øªA¾¹¤¹³\³o¼Ë¡A§Ú­Ì¦bµ{¦¡³]­p®É¥i¥H¬° SQL Äݩʳ]©w¦h±øSQL»y¥y¡C

2. ¨Ï¥ÎVisual Query Builder½s¼g

¥Î¤á/¦øªA¾¹ª©¥»ªºDelphiÁÙ¥]§t¤@­Ó¥iµø¤Æªº¬d¸ßºc³y¾¹Visual Query Builder ¡A¥Î³o­Ó¥iµø¤Æªº¤u¨ã§Ú­Ì¥u¯à½s¼gSelect»y¥y¡C¦bÀ³¥Îµ{¦¡µ¡Å餤¿ï¾ÜTQuery³¡¥ó«á¡A«ö¤@¤U·Æ¹«¥kÁä¡A¼u¥X¤@­Ó¼u¥X¦¡¿ï¾Üªí¡A±q¤¤¿ï¾ÜRun Visual Query Builder«á«K·|¼u¥X¤@¹ï¸Ü¤è¶ô´£¥Ü§A¿ï¾Ü­n¦s¨úªº¸ê®Æ®w¡A¿ï¾Ü·Q­n¦s¨úªº¸ê®Æ®w¤§«á¿ï¾ÜOK«ö¶s¡Aºò±µµÛ·|¥X²{¤@­Ó¼u¥X¦¡¹ï¸Ü¤è¶ô´£¥Ü§A¿ï¾Ü­n¬d¸ßªº¸ê®Æ®wªí¡A¤@¦¸¥i¥H¿ï¾Ü¦h­Ó¸ê®Æ®wªí¡A­Y­n¿ï¾Ü¦h­Ó¸ê®Æ®wªí¡A¨C¿ï¾Ü¤@­Óªí¤§«á«ö¤@¤UAdd«ö¶s¡A±µµÛ¿ï¾Ü¥t¤@­Óªí¡A ¿ï¾Ü§¹­n¬d¸ßªºªí¤§«á«ö¤@¤UClose«ö¶s¡A³o¼Ë¡A¥iµø¤Æªº¬d¸ßºc³y¾¹¤¤±N·|Åã¥Ü¥X¥Î¤á¿ï¾Üªº¸ê®Æ®wªí¡C¡@¡@

¦³Ãö¦p¦ó¨Ï¥Î¥iµø¤Æªº¬d¸ßºc³y¾¹Visual Query Builder ½Ð°Ñ¬Ý³s½u»²§U±Ô­z¸ê°T¡A ¦bVisual Query Builder¤¤ºc³y§¹¤@­Ó¬d¸ß¨Ãµ²§ôVisual Query Builder®É¡A¨ä¤¤ªºSQL ©R¥O»y¥y·|¦Û°Ê¦a¼g¤J¬ÛÀ³ªºTQuery³¡¥óªºSQLÄÝ©Ê¡C¡@

17.3.2 SQLµ{¦¡ªº°õ¦æ¡@

¦b¬°TQuery³¡¥ó³]©w§¹SQLÄݩʪºÄݩʭȤ§«á¡A¤]§Y½s¼g¦n¾A·íªºSQLµ{¦¡¤§«á¡A¥i¥H¦³¦hºØ¤è¦¡¨Ó°õ¦æSQLµ{¦¡¡C

¦b³]­p¹Lµ{¤¤¡A³]©w§¹TQuery³¡¥óªºSQLÄݩʤ§«á±N¨äActiveÄݩʪº­È¸m¬°True¡A ³o¼Ë«K¥i¥H°õ¦æSQLÄݩʤ¤ªºSQLµ{¦¡¡A¦pªGÀ³¥Î¤¤¦³»PTQuery³¡¥ó¬Û³sªº¼Æ¾ÚÂsÄý³¡¥ó( ¦pTDDGrid TDBEditµ¥)¨º»ò¦b³o¨Ç¼Æ¾ÚÂsÄý³¡¥ó¤¤·|Åã¥ÜSQLµ{¦¡ªº°õ¦æµ²ªG¡C

¦bÀ³¥Îµ{¦¡°õ¦æ¹Lµ{¤¤¡A³q¹Lµ{¦¡©I¥sTQuery³¡¥óªºOpen¤èªk©ÎExecSQL ¤èªk¥i¥H°õ¦æ¨äSQLÄݩʤ¤ªºSQLµ{¦¡¡COpen¤èªk©MExecSQL¤èªk¬O¤£¤@¼Ëªº¡C ¤j®a¦bµ{¦¡³]­p¹Lµ{¤¤¤@©w­nª`·N¡COpen¤èªk¥u¯à¥Î¨Ó°õ¦æSQL»y¨¥ªº¬d¸ß»y¥y(Select©R¥O)¡A ¨Ãªð¦^¤@­Ó¬d¸ßµ²ªG¶°¡A¦ÓExecSQL¤èªkÁÙ¥i¥H¥Î¨Ó°õ¦æ¨ä¥¦±`¥ÎªºSQL»y¥y(¦pInsert¡BUPDATE¡B DELETEµ¥©R¥O)¨Ò¦p¡G

¡@

Query1.Open (³o¼Ë·|ªð¦^¤@­Ó¬d¸ßµ²ªG¶°)

¡@

¦pªG©I¥sOpen¤èªk¡A¦Ó¨S¦³¬d¸ßµ²ªG®É¡A·|¥X¿ù¡C¦¹®ÉÀ³¸Ó©I¥sExecSQL ¤èªk¨Ó¥N´ÀOpen¤èªk¡C¦p¡G

¡@

Query1.ExecSQL (¨S¦³ªð¦^µ²ªG)

¡@

·íµM¦b³]­pÀ³¥Îµ{¦¡®É¡Aµ{¦¡³]­p¤H­û¬OµLªk½T©wTQuery³¡¥ó¤¤ªºSQL »y¥y¬O§_·|ªð¦^¤@­Ó¬d¸ßµ²ªGªº¡C¹ï©ó³oºØ±¡ªpÀ³·í¥ÎTry¡KExcept¼Ò¸s²Õ¨Ó³]­pµ{¦¡¡C¦b Try ³¡¤À©I¥sOpen¤èªk¡A¦Ó¦bExcept³¡¤À©I¥sExceSQL¤èªk¡A³o¼Ë¤~¯à«OÃÒµ{¦¡ªº¥¿½T°õ¦æ¡C

¨Ò¦p¡G

¡@

Try

Query1.Open

Except

Query1.ExecSQL

End

¡@

¦bÀ³¥Îµ{¦¡¤¤¨Ï¥ÎTQuery³¡¥ó®É¡AÁÙ¥i¥H³]©w¥¦ªºUniDirectionalÄݩʬ°True¡A³o¼Ë·|¥[§ÖÀ˯Á¸ê®Æ®wªíªº³t«×¡A ¦ý¬O³o¼Ë¥u¯à©¹¤@­Ó¤è¦V²¾°Ê°O¿ý«ü¼Ð¡A ¦b¯Ê¬Ù±¡ªp¤U¡AUniDirectionalÄݩʪº­È¬°False¡C

¡@

17.3.3 ³q¹LTQuery³¡¥ó¦p¦óÀò±o¬¡°Êªº¼Æ¾Ú

¡@

§Ú­Ì¦b«e­±ªº³¹¸`ùؤ¶²ÐTTable³¡¥ó®É¡A§Ú­Ìª¾¹D³q¹LTTable³¡¥ó±q¸ê®Æ®w¤¤Àò±oªº¼Æ¾Ú³£¬O¬¡°Êªº¡A¤]´N¬O»¡¥Î¤á¥i¥Hª½±µ³q¹L¼Æ¾ÚÂsÄý³¡¥ó¹ï³o¨Ç¼Æ¾Ú¶i¦æ½s¿è­×§ï¡C¦Ó³q¹LTQuery³¡¥ó¥i¥HÀò±o¨âºØ«¬¦¡ªº¼Æ¾Ú¡G

¡´ ¡§¬¡°Ê¡¨ªº¼Æ¾Ú

³oºØ¼Æ¾Ú´N¸ò³q¹LTTable³¡¥óÀò±oªº¼Æ¾Ú¤@¼Ë¡A¥Î¤á¥i¥H³q¹L¼Æ¾ÚÂsÄý³¡¥ó¨Ó½s ¿è­×§ï³o¨Ç¼Æ¾Ú¡A¨Ã¥B·í©I¥sPost¤èªk©Î·íµJÂIÂ÷¶}¥Ø«eªº¼Æ¾ÚÂsÄý³¡¥ó®É¡A¥Î¤á¹ï¼Æ¾Úªº­×§ï¦Û°Ê¦a³Q¼g¦^¨ì¸ê®Æ®w¤¤¡A¸Ô²Ó±¡ªp½Ð°Ñ¬Ý²Ä¥|³¹¡§¼Æ¾ÚÂsÄý³¡¥óªºÀ³¥Î¤Îµ{¦¡³]­p¡¨¡C

¡´ «D¬¡°Êªº¼Æ¾Ú(°ßŪ¼Æ¾Ú)

¥Î¤á³q¹L¼Æ¾ÚÂsÄý³¡¥ó¬O¤£¯à­×§ï¨ä¤¤ªº¼Æ¾Ú¡C¦b¯Ê¬Ù±¡ªp¤U¡A³q¹LTQuery³¡¥ó Àò±oªº¬d¸ßµ²ªG¼Æ¾Ú¬O°ßŪ¼Æ¾Ú¡A­n·QÀò±o¡§¬¡°Ê¡¨ªº¼Æ¾Ú¡A¦bÀ³¥Îµ{¦¡¤¤¥²¶·­n³]©wTQuery³¡¥óªºRequestLiveÄݩʭȬ°True¡AµM¦Ó¨Ã¤£¬O¦b¥ô¦ó±¡ªp¤U(³q¹L³]©wRequestLiveªºÄÝ­ÈTrue)³£¥i¥HÀò±o¡§¬¡°Ê¡¨ªº¼Æ¾Úªº¡A­n·QÀò±o¡§¬¡°Ê¡¨ªº¼Æ¾Ú¡A°£¤F±NTQuery³¡¥óªºRequestLiveÄݩʬ°True¥~¡ABDE­n¯à°÷ªð¦^¡§¬¡°Ê¡¨ªº¼Æ¾Ú¡A¬ÛÀ³ªºSQL©R¥O»y¥yÁÙ­nº¡¨¬ªþ¿ýC¤¤ªº»yªk³W«h©M¤U¦Cªº¬ù§ô±ø¥ó¡G

TQuery³¡¥óÀò±o¡§¬¡°Ê¡¨ªº¬d¸ßµ²ªG¼Æ¾Úªº¬ù§ô±ø¥ó¡G

¡@

·í¬d¸ßParadox©ÎdBASE¸ê®Æ®w¤¤ªºªí¡G

¡´ ¬d¸ß¥u¯à¯A¤Î¨ì¤@­Ó³æ¿Wªºªí

¡´ SQL»y¥y¤¤¤£¯à¥]§tORDER BY©R¥O

¡´ SQL»y¥y¤¤¤£¯à§t»E¶°¹Bºâ²ÅSUM©ÎAVG

¡´ ¦bSelect«áªºÄæ¦ì²M³æ¤¤¤£¯à¦³­pºâÄæ¦ì

¡´ ¦bSelect»y¥yWHERE³¡¤À¥u¯à¥]§tÄæ¦ì­È»P±`¼Æªº¤ñ¸û¹Bºâ¡A³o¨Ç¤ñ¸û¹Bºâ²Å¬O¡G Like,>,<,>=,<=¡A¦U¤ñ¸û¹Bºâ¤§¶¡¥i¥H¦³¨Ã©M¥æ¹Bºâ¡GAND©MOR¡C

¡@

·í³q¹LSQL»y¥y¬d¸ß¸ê®Æ®w¦øªA¾¹¤¤ªº¸ê®Æ®wªí¡G

¡´ ¬d¸ß¥u¯à¯A¤Î¨ì¤@­Ó³æ¿Wªºªí

¡´ SQL»y¥y¤¤¤£¯à¥]§tORDER BY©R¥O

¡´ SQL»y¥y¤¤¤£¯à§t»E¶°¹Bºâ²ÅSUM©ÎAVG¹Bºâ

¡@

¥t¥~¡A¦pªG¬O¬d¸ßSybase¸ê®Æ®w¤¤ªºªí¡A¨º»ò³Q¬d¸ßªºªí¤¤¥u¯à¦³¤@­Ó¯Á¤Þ¡C

¦pªG¦bÀ³¥Îµ{¦¡¤¤­n¨DTQuery³¡¥óªð¦^¤@­Ó¡§¬¡°Ê¡¨ªº¬d¸ßµ²ªG¼Æ¾Ú¶°¡A¦ý¬OSQL ©R¥O»y¥y¤£º¡¨¬¤W­z¬ù§ô±ø¥ó®É¡A¹ï©ó¥»¦a¸ê®Æ®wªºSQL¬d¸ß¡ABDE¥u¯àªð¦^°ßŪªº¼Æ¾Ú¶°¡C¹ï©ó¸ê®Æ®w¦øªA¾¹¤¤ªºSQL¬d¸ß¡A¥u¯àªð¦^¿ù»~ªº¥N½X¡C·íTQuery ³¡¥óªð¦^¤@­Ó¡§¬¡°Ê¡¨ªº¬d¸ßµ²ªG¼Æ¾Ú¶°®É¡A¥¦ªºCanModifyÄݩʪº­È·|³Q³]©w¦¨True¡C

¡@

ªí17.1 TQuery³¡¥óªð¦^¬d¸ßµ²ªG¼Æ¾Úªº«¬¦¡

¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

RequestLiveÄÝ©Ê­È CanModifyÄÝ©Ê­È ¬d¸ßµ²ªGªº«¬¦¡

¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X¡X

False False °ßŪ¼Æ¾Ú

True(SQL»y¥yº¡¨¬¬ù§ô±ø¥ó) True ¡§¬¡°Ê¡¨¼Æ¾Ú

True(SQL»y¥y¤£º¡¨¬¬ù§ô±ø¥ó) False °ßŪ¼Æ¾Ú

¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w

¡@

·íTQuery³¡¥óªð¦^°ßŪªº¬d¸ßµ²ªG¼Æ¾Ú¶°¡A¦Ó¥Î¤á¤S§Æ±æ­×§ï³o°ßŪªº¼Æ¾Ú¶°®É¡A¤@¯ë³o¼Ë¨Ó³B²z¡A ¦bÀ³¥Îµ{¦¡¤¤¥t¥~¼W¥[¤@­Ó TQuery ³¡¥ó Query2( °²³]Àò±o°ßŪµ²ªGªºTQuery³¡¥óªº¦W¦r¬OQuery1)¡A¦bQuery2¤¤³]©w­×§ï»y¥yUpDATE¹ïQuery1 ¤¤ªº¼Æ¾Ú¶i¦æ­×§ï¾Þ§@¡A³o¼Ë·|¹ê²{¹ï°ßŪ¼Æ¾Úªº­×§ï¡C

¡@

17.4 °ÊºASQL»y¥yªºµ{¦¡³]­p

¡@

¦b17.3¸`¤¤¡A§Ú­Ì¤w¸g¤¶²Ð¤F°ÊºASQL»y¥y¡]¤S³QºÙ¬°°Ñ¼Æ¤ÆªºSQL»y¥y¡^¡A¦b¨ä¤¤¥]§t¦bµ{¦¡¹Lµ{¤¤¥i¥HÅܤƪº°Ñ¼Æ¡A¦b¹ê»Úªºµ{¦¡³]­p¤¤¨Ï¥Î±o§ó¦hªº¬O°ÊºASQL»y¥y¡A ¦]¦Ó¦b³o¤@¸`ùاڭ̭«ÂI¤¶²Ð¦p¦óµ¹°ÊºASQL»y¥yªº°Ñ¼Æ½á­È¡A ¥H¦bÀ³¥Îµ{¦¡¤¤ÆF¬¡¦a¨Ï¥ÎSQL»y¥y¡C°ÊºASQL»y¥yªº½s¼g¡B°õ¦æµ¥µ¥»P17.3¸`¤¤¤¶²ÐªºSQL»y¥yªº½s¼g¡B °õ¦æ¬O¤@¼Ëªº¡C

°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ¡A§Ú­Ì¥i¥H³q¹L¨âºØ³~®|¨Ó¬°¥¦½á­È¡G

1. §Q¥Î°Ñ¼Æ½s¿è¾¹(Parameter Editor)¨Ó¬°°Ñ¼Æ½á­È

¨ãÅé¤èªk¬O¡G¿ï¤¤TQuery³¡¥ó¡A«ö¤@¤U·Æ¹«¥kÁä¡AµM«á±q¤¤¿ï¾ÜDefine Parameters «K¥i¥H¥´¶}°Ñ¼Æ½s¿è¾¹¡C¡@

¨Ò¦p¡A¦bTQuery³¡¥óªºSQLÄݩʤ¤§Ú­Ì³]©w¦p¤UªºSQL»y¥y¡G¡@

Setect * From Customer Where CustNO=:Number;

¡@

TQueryªºDatabaseNameÄݩʬ°DBDEMOS¡A¨ä¤¤Number¬°°Ñ¼ÆÅܼơC§Ú­Ì«K¥i¥H¬°°Ñ¼ÆNumber½á­È¡A¦bDatetype¸s¸s²Õ¦X®Ø¤¤¿ï¾Ü¸Ó°Ñ¼Æªº¼Æ¾Ú«¬¦¡¬°¾ã¼ÆInteger¡A¦bValue½s¿è®Ø¤¤¥i¥H¬°°Ñ¼ÆNumber½á¤@­Ó­È¡A¤]¥i¥H«ö¤@¤UNull ValueÀˬd®Ø¬°°Ñ¼ÆNumber½á¤@­ÓªÅ­ÈNull¡Cµ¹°Ñ¼Æ½á­È¤§«á¡A«ö¤@¤UOK«ö¶s¡A³o¼ËTQuery³¡¥ó¤¤ªºSQL ¬d¸ß«K·Ç³Æ¦n¤F¡A¦Ó¥B°Ñ¼Æ­È¤]³Q½áµ¹¤F°ÊºASQL»y¥y¤¤¬ÛÀ³ªº°Ñ¼Æ¡A¦¹®É·í§âTQuery ³¡¥óªºActiveÄݩʳ]©w¦¨True®É¡A¦b»PTQuery³¡¥ó¬Û³sªº¼Æ¾ÚÂsÄý³¡¥ó¤¤·|Åã¥Ü¥X¬d¸ßµ²ªG¡A³q¹L°Ñ¼Æ½s¿è¾¹¬°°Ñ¼Æ½á­È¡A³oºØ¤è¦¡¯Ê¥FÀ³¦³ªºÆF¬¡©Ê¡A¦b¹ê»ÚÀ³¥Î¤¤¥Î±o¸û¤Ö¡A¦b¹ê»ÚÀ³¥Î¤¤µ{¦¡³]­p¤H­û§Æ±æ¥Î§óÆF¬¡¤è«Kªº¤è¦¡¬°°Ñ¼Æ½á­È¡A¨º´N¬O§Ú­Ì±µ¤U¨Ó­n¤¶²Ðªº¥t¤@ºØ³~®|¡G

2. ¦b°õ¦æ¹Lµ{¤¤¡A³q¹Lµ{¦¡¬°°Ñ¼Æ½á­È

¥Î³oºØ¤è¦¡¬°°Ñ¼Æ½á­È¦³¤TºØ¤èªk¡G

Æ¡®Ú¾Ú°Ñ¼Æ¦bSQL»y¥y¤¤¥X²{ªº¶¶§Ç¡A³]©wTQuery³¡¥óªºParamsÄݩʭȬ°°Ñ¼Æ½á­È¡C

Æ¢ª½±µ®Ú¾ÚSQL»y¥y¤¤¦U°Ñ¼Æªº¦W¦r¡A©I¥sParamByName¤èªk¨Ó¬°¦U°Ñ¼Æ½á­È¡C

Æ£±NTQuery³¡¥óªºDataSourceÄݩʳ]©w¬°¥t¤@­Ó¼Æ¾Ú·½¡A³o¼Ë±N¥t¤@­Ó¼Æ¾Ú·½¤¤»P¥Ø«eTQuery³¡¥óªºSQL»y¥y¤¤ªº°Ñ¼Æ¦W¬Û¤Ç°tªºÄæ¦ì­È½áµ¹¨ä¹ïÀ³ªº°Ñ¼Æ¡C

³o¤TºØ¤èªk§Ú­Ì±N¦b¤U­±ªº¤T¤p¸`¤¤¨ãÅé¦a¤¶²Ð

¡@

17.4.1 ¨Ï¥ÎParamsÄݩʬ°°Ñ¼Æ½á­È

¡@

TQuery³¡¥ó¨ã¦³¤@­ÓParamsÄÝ©Ê¡A¥¦­Ì¦b³]­p®É¤£¥i¥Î¡A¦bµ{¦¡°õ¦æ¹Lµ{¤¤¥i¥Î¡A¨Ã¥B¬O°ÊºA«Ø¥ßªº¡A·í¬°TQuery³¡¥ó½s¼g°ÊºASQL »y¥y®É¡A Delphi ·|¦Û°Ê¦a«Ø¥ß¤@­Ó¼Æ¸s¸s²ÕParams¡A¼Æ¸s¸s²ÕParams¬O¥H0¤U¼Ð¶}©lªº¡A¨Ì¦¸¹ïÀ³°ÊºASQL »y¥y¤¤ªº°Ñ¼Æ¡A ¤]´N¬O»¡°ÊºASQL»y¥y¤¤²Ä¤@­Ó°Ñ¼Æ¹ïÀ³Params[0],²Ä¤G­Ó°Ñ¼Æ¹ïÀ³params[1]¡A¨Ì¦¹Ãþ±À¡C

¨Ò¦p¡G¤@­ÓTQuery³¡¥óQuery1¡A§Ú­Ì¬°¥¦½s¼gªº°ÊºASQL»y¥y¬O¡G

¡@

Insert Into Customer(CustNo,Name,Country)

Values(:CustNo,:Name, : Country)

¡@

¹ï©ó¤W­z³o±ø°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ¡A§Ú­Ì¥i¥H§Q¥ÎTQuery³¡¥óªºparams Äݩʬ°°Ñ¼Æ½á­È¡G

¡@

Query1.params[0].AsString := "1988";

Query1.params[1].AsString := "Lichtenstein";

Query1.params[2].AsString := "USA";

¡@

¤W­z»y¥y±N§â"1988"½áµ¹°Ñ¼Æ:Cuse_No¡A"Lichtenstein"½áµ¹°Ñ¼Æ:Name¡A"USA"½áµ¹°Ñ¼Æ:Country¡C

¡@

17.4.2 ¨Ï¥ÎParamByName¤èªk¬°°Ñ¼Æ½á­È

¡@

ParamByName¬O¤@­Ó¨ç¼Æ¡A¥Î°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ§@¬°©I¥sParamByName¨ç¼Æªº°Ñ¼Æ¡A³o¼Ë«K¥i¥H¬°¥¦­Ì½á­È¡A¨Ï¥Î³oºØ½á­È¤èªk¡A¥²¶·­nª¾¹D°ÊºASQL»y¥y°Ñ¼Æªº¦W¦r¡C

¨Ò¦p¦b17.4.1¸`¤¤ªº¨Ò¤l¤¤¡A¤]¥i¥H¥Î¤U­z¤èªkµ¹°Ñ¼Æ½á­È¡G

¡@

Query1.ParamByName('CustNo').AsString := "1988";

Query1.ParamByName('Name').AsString := "Lichtenstein";

Query1.ParamByName('Country').AsString := "USA";

¡@

¨Ï¥Î³oºØ¤èªk¦P¼Ë¥i¥H¬°¦U°Ñ¼Æ½á­È¡A¦Ó¥B§ó¥[ª½Æ[¤@¨Ç¡C

¡@

17.4.3 ¨Ï¥ÎDatasourceÄݩʬ°°Ñ¼Æ½á­È

¡@

¤W­z¨âºØ¤èªkªº¦@¦P¯SÂI¬O¡G§Ú­Ì¦b¬°¦U°Ñ¼Æ½á­È®É¡A§Ú­Ì¬Oª¾¹D¦U°Ñ¼Æ¹ïÀ³ªº¨ãÅé°Ñ¼Æ­Èªº¡C¦Ó¦b¨ãÅ骺À³¥Îµ{¦¡¤¤¡A¦³¨Ç°Ñ¼Æ­È±`±`¬OµLªk½T©wªº¡A¨Ò¦p°Ñ¼Æ­È¨Ó¦Û©ó¥t¤@­Ó¬d¸ßµ²ªG¡A¹ï©ó³oºØ±¡ªp¡ADelphi´£¨Ñ¤F¨Ï¥ÎDatasourceÄݩʬ°°ÊºASQL »y¥y¤¤©|¦s¦b¨S¦³½á­Èªº°Ñ¼Æ®É¡A Delphi ·|¦Û°ÊÀˬd TQuery ³¡¥óªº Datasource ÄÝ©Ê¡A ¦pªG¬°DatasourceÄݩʳ]©w¤FÄÝ©Ê­È(¸ÓÄݩʪº­È¬O¥t¤@­ÓTDatasource³¡¥óªº¦W¦r)¡ADelphi ·|§â¨S¦³½á­Èªº°Ñ¼Æ»PTDatasource³¡¥ó¤¤ªº¦UÄæ¦ì¤ñ¸û¡ADelphi ·|±N¬ÛÀ³ªºÄæ¦ì­È½áµ¹»P¨ä¬Û¤Ç°tªº°Ñ¼Æ¡A§Q¥Î³oºØ¤èªk¤]¯à¹ê²{©Ò¿×ªº³s±µ¬d¸ß¡A§Ú­Ì¦b¾Ç²ß¨Ï¥ÎTTable³¡¥ó®É¡A«K·|«Ø¥ß¥D­n¡Ð¡Ð©ú²Ó«¬¸ê®Æ®wÀ³¥Î¡A¥ÎTQuery³¡¥ó«Ø¥ßªº³s±µ¬d¸ß»P¥D­n- ¡Ð©ú²Ó«¬À³¥Î¬O¬Û¦üªº¡C

¨Ò¦p¡G¦b¦p¹Ï17.7©Ò¥ÜªºÀ³¥Î¤¤¡A³]©w¤F¤U¦C³¡¥ó¡G

¡´ ¤@­ÓTTable³¡¥ó

¦W¦r¬°Cust¡A¥¦ªºDatabaseNameÄݩʬ°DEMOS¡ATableNameÄݩʬ°Customer¡C

¡´ ¤@­ÓTDatasource³¡¥ó

¦W¦r¬°Custsource¡A¨äDatasetÄݩʳQ³]©w¬°Cust¡C

¡´ ¤@­ÓTQuery³¡¥ó

¦W¦r¬°ORDERS¡A¨äDatabaseName³Q³]©w¬°DEMOS¡ASQLÄݩʭȬ°¡G

¡@

Select Orders.CustNo,Orders.OrderNo,Orders.SaleDate FROM Orders

WHERE Orders.CustNo =: CustNo

¡@

ORDERSªºDataSouceÄݩʳQ³]©w¬°CustSource

¡´ ¤@­ÓTDatasource³¡¥ó

¦W¦r¬°OrderSource¡A¨äDataSetÄݩʳQ³]©w¬°Orders¡C

¡´ ¨â­ÓTDBGrid³¡¥ó

¥¦­Ì¤À§O³s±µCustSource©MOrderSource¡C

TQuery³¡¥óOrders¤¤ªº°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ:CustNo¦bµ{¦¡³]­p¹Lµ{¤¤¨S¦³µ¹¥¦½á­È¡A·í¸ÓÀ³¥Îµ{¦¡°õ¦æ®ÉDelphi·|¦Û°Ê¦a¨ì¨äDatasourceÄݩʤ¤±Ô­zªº¼Æ¾Ú·½CustSource¤¤§ä¨ì»P°Ñ¼Æ:CustNo¤Ç°tªºÄæ¦ì¡A¦ÓCustSource¤¤¥¿¦n¦³¤@­Ó¦W¦r¬° CustNo ªºÄæ¦ì»P°Ñ¼Æ:CustNo¤Ç°t¡A³o¼ËCustomerªí¤¤ªºCustNoÄæ¦ì­È³Q½áµ¹¤F°Ñ¼Æ : CustNo ¡A ¦Ó·í¨C²¾°ÊCustomerªí¤¤ªº°O¿ý«ü¼Ð¡A°Ñ¼Æ:CustNoªº­È·|ÀH¤§§ïÅÜ¡A¦Ó°Ñ¼Æ:CustNoªº­Èµo¥Í§ïÅܮɡAOrders¤¤ªº°ÊºASQL»y¥y·|®Ú¾Ú·sªº°Ñ¼Æ­È­«·s¬d¸ß¡A±q¸ê®Æ®wªí¤¤Àò¨ú¬ÛÀ³ªº­q³æ¼Æ¾Ú¡A³o¼Ë¤]Åܹê²{¤FÃþ¦ü©ó¥D­n¡Ð¡Ð©ú²Ó«¬À³¥Î¡C§Y³s±µ¬d¸ß¡C¡@

17.4.4 Prepare¤èªkªº¨Ï¥Î¡@

¦b¨Ï¥Î°ÊºASQL»y¥yµ{¦¡³]­p®É¡A±`±`¥Î¨ì¤@­Ó«Ü­«­nªº¤èªkprepare¡A©I¥sprepare ¤èªk¤§«á¡ADelphi·|±N±a°Ñ¼ÆªºSQL»y¥yÂà°eµ¹»P¨ä¹ïÀ³ªº¸ê®Æ®w¤ÞÀº¡A¹ï°ÊºASQL»y¥y¶i¦æ»yªk¤ÀªR©MÀu¤Æ¡CÁöµM¦b¥Î°ÊºASQL»y¥yµ{¦¡³]­p®É¡A©I¥sprepare¤èªk¨Ã¤£¬O¥²¶·ªº¡A¦ý¬O³oùاڭ̭n·¥¤O±ÀÂË©I¥sprepare¤èªk¡A¦]¬°©I¥sprepare¤èªk«á¡A·|¤j¤j´£°ª°ÊºASQL »y¥yªº°õ¦æ©Ê¯à¡A¯S§O¬O·í­n¤Ï´_¦h¦¸°õ¦æ¦P¤@±ø°ÊºASQL»y¥y®É¡A¨äÀu¶V©Ê·|§ó¥[©úÅã¡C ¦pªG¦bÀ³¥Îµ{¦¡¤¤°õ¦æ¤@±øSQL»y¥y¤§«e¨Ã¨S¦³Å㦡¦a©I¥sprepare¤èªk¡A¨C¦¸¦b°õ¦æSQL »y¥y®É¡ADelphi·|Áô§t¦a©I¥spropare¤èªk¥H·Ç³Æ³o­Ó¬d¸ß¡C

TQuery³¡¥óÁÙ¦³¤@­ÓprepareÄÝ©Ê¡A³o¬O¤@­Ó¥¬º¸«¬ÄÝ©Ê¡A·í¨äÄݩʭȬ°True®É¡A ªí©ú¸Ó¬d¸ß¤w³Q·Ç³Æ¦n¤F( SQL »y¥y¤w³QÂà°e¨ì¸ê®Æ®w¤ÞÀº¤¤ ) ¡A ·í§Ú­Ì¨Ï¥Î°Ñ¼Æ½s¿è¾¹Parameters Editor¨Ó¬°°ÊºASQL»y¥y¤¤ªº°Ñ¼Æ½á­È®É¡A·í³]©w§¹¬ÛÀ³ªº°Ñ¼Æ­È¨Ãµ²§ô°Ñ¼Æ½s¿è¾¹®É¡ADelphi·|Áô§t¦a©I¥sprepare¤èªk¥H·Ç³Æ¦n¬d¸ß¡C

·íSQL»y¥y°õ¦æ§¹¤§«á¡A­n·Q·Ç³Æ¤U¤@­Ó¬d¸ß¡A­º¥ý¥²¶·©I¥sclose¤èªk¡AµM«á¤~¯à©I¥sprepare¤èªk·Ç³Æ¤U¤@­Ó¬d¸ß¡C¤@¯ë¨Ó»¡¡A¦b¤@­ÓÀ³¥Îµ{¦¡¤¤À³¸Ó©I¥s¤@¦¸prepare¤èªk¡A±`±`¦bµ¡Å骺OnCreate¨Æ¥ó³B²z¹Lµ{¤¤©I¥sprepare¤èªk¡A µM«á¥Î¤W­z¤¶²Ðªº¤èªk¬°°Ñ¼Æ½á­È¡A³Ì«á©I¥sOpen¤èªk©ÎExecSQL¤èªk°õ¦æSQL»y¥y¡A¥H§¹¦¨¬d¸ß¡C

·íµM¦b©I¥sprepare¤èªk·Ç³Æ¦n¤@­Ó¬d¸ß®É¡A·|®ø¯Ó¤@¨Ç¸ê®Æ®w¸ê·½¡A ¦]¦Ó¨C·í¤@­Ó¬d¸ß°õ¦æ§¹²¦¤§«á¡A­n¾i¦¨©I¥sUnprepare¤èªk¥H´_­ì¬d¸ßªº¦n²ßºD¡C¦b°õ¦æµ{¦¡¹Lµ{¤¤¡A³q¹Lµ{¦¡§ïÅÜTQuery³¡¥óªºSQLÄݩʭȮɡADelphi·|¦Û°Ê¦a©I¥sClose¤èªk©MUnprepare ¤èªk¡A¥H´_­ì¬d¸ß¡C

¡@


«á¤@­¶
«e¤@­¶
¦^¥Ø¿ý
¦^­º­¶