¶¯Ì¬SQL(Dynamic SQL)
----start
¶¯Ì¬SQLÊÇÔÚ³ÌÐòÔËÐÐʱ¹¹ÔìµÄ£¬ÒªÖ´Ðе¥ÌõSQL£¬Ê¹ÓÃEXECUTE IMMEDATE Óï¾ä£»µ±ÅúÁ¿Ö´ÐÐSQLʱ£¬ÏÈʹÓÃPREPARE Óï¾ä¹¹ÔìSQL£¬È»ºóʹÓÃEXECUTE Óï¾äÖ´ÐС£
Ò»£ºPrepareÓï¾ä£ºÓÃÀ´¹¹ÔìÅúÁ¿SQL
Óï·¨£º
PREPARE <sql-statement> [OUTPUT] INTO <result> [INPUT INTO] <input> from <variable>
¶þ£ºDESCRIBEÓï¾ä£º»ñÈ¡±í¡¢SQLµÈÊý¾Ý¿â¶ÔÏóµÄÃèÊöÐÅÏ¢
Óï·¨£º
>>-DESCRIBE----------------------------------------------------->
.-OUTPUT-.
>--+-+--------+--+-select-statement---------+--------------------------------+-><
| +-call-statement-----------+ |
| '-XQUERY--XQuery-statement-' |
'-+-TABLE----------------------------------+--table-name--+-------------+-'
+-+-----------------+--INDEXES FOR TABLE-+ '-SHOW DETAIL-'
| +-RELATIONAL DATA-+ |
| +-XML DATA--------+ |
| '-TEXT SEARCH-----' |
'-DATA PARTITIONS FOR TABLE--------------'
Èý£ºExecuteÓï¾ä£ºÓÃÀ´Ö´ÐÐÅúÁ¿SQL£¬²»ÄÜÖ´ÐÐselectÓï¾ä
Óï·¨£º
EXECUTE <statement-name> [INTO <result-variable>] [USING <input-variable> [,<input-variable>,...] ]
ËÄ£ºExecute ImmediateÓï¾ä£ºÓÃÀ´Ö´Ðе¥ÌõSQLÓï¾ä£¬²»ÄÜÖ´ÐÐselectÓï¾ä
Óï·¨£º
EXECUTE IMMEDIATE <sql-statement>
Î壺ʾÀý
--Á¬½ÓÊý¾Ý¿â
CONNECT TO SAMPLE!
--´´½¨±í
CREATE TABLE TESTTB(A INT, B INT)!
--ɾ³ý´æ´¢¹ý³Ì
DROP PROCEDURE TEST!
--´´½¨´æ´¢¹ý³Ì
CREATE PROCEDURE TEST (IN V1 INT, IN V2 INT)
BEGIN
DECLARE STMT VARCHAR(50);
DECLARE ST STATEMENT;
--¾²Ì¬SQL
INSERT INTO TESTTB VALUES (1,1);
--¶¯Ì¬SQL(µ¥ÌõÓï¾ä)
SET STMT='INSERT INTO TESTTB VALUES (' ||
Ïà¹ØÎĵµ£º
1£©PL/SQLÊý¾ÝÀàÐÍ
Ãû³Æ
ÀàÐÍ
˵Ã÷
NUMBER
Êý×ÖÐÍ
ÄÜ´æ·ÅÕûÊýÖµºÍʵÊýÖµ£¬²¢ÇÒ¿ÉÒÔ¶¨Ò徫¶ÈºÍÈ¡Öµ·¶Î§
BINARY_INTEGER
Êý×ÖÐÍ
¿É´æ´¢´ø·ûºÅÕûÊý£¬ÎªÕûÊý¼ÆËãÓÅ»¯ÐÔÄÜ
DEC
Êý×ÖÐÍ
NUMBERµÄ×ÓÀàÐÍ£¬Ð¡Êý
DOUBLE PRECISION
Êý×ÖÐÍ
NUMBERµÄ×ÓÀàÐÍ£¬¸ß¾«¶ÈʵÊý
INTEGER
Êý×ÖÐÍ
NUMBERµÄ×ÓÀàÐÍ£¬ÕûÊý
INT
Êý ......
---------Êýѧº¯Êý
1.¾ø¶ÔÖµ
S:select abs(-1) value
O:select abs(-1) value from dual
2.È¡Õû(´ó)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.È¡Õû£¨Ð¡£©
S:select floor(-1.001) value
O:select floor(-1.001) value from dual
4.È¡Õû£¨½ØÈ¡£©
S:select cast(-1.002 as int) v ......
select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = 'ÏàÓ¦±íÃû')
ÓÃÒÔÉÏsqlÓï¾äÊäÈëÏàÓ¦±íÃû¾Í¿ÉÒԲ鵽±íµÄ×Ö¶ÎÃû£¬¶ÔÓ¦ºÃÊý¾Ý¿â ²éѯÊÇ·ñ´æÔڸñíÓï¾ä
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tb_cost]') and OBJECTPROPER ......
Ó°ÏìÊý¾Ý¿â»¹ÔËٶȵÄÒòËغÍÓ°ÏìÊý¾Ý¿â±¸·ÝËٶȵÄÒòËØÏàͬ¡£³ý´ËÖ®Í⣬¼ÙÈçÄãʹÓÃSQL Server 2005µÄ»°£¬Ä㻹¿ÉÒÔÆô¶¯ÁíÍâÒ»¸öÓÅ»¯ÈÎÎñÀ´»¹Ôµ±Ç°²»´æÔÚµÄÊý¾Ý¿â£¬ÔËÐл·¾³ÎªWindows XP£¬Windows 2003 Server »ò¸üа汾¡£
Perform Volume Maintenance Tasks
µ±Ä㻹ÔÒ»¸öеÄÍêÕûÊý¾Ý¿âÊÇ£¬SQL Server¶Á±¸·ÝÎļþÍ·£¬È»ºó´ ......
¡¡--Óï ¾ä ¹¦ ÄÜ¡¡¡¡--Êý¾Ý²Ù×÷¡¡¡¡SELECT --´ÓÊý¾Ý¿â±íÖмìË÷Êý¾ÝÐкÍÁС¡¡¡INSERT --ÏòÊý¾Ý¿â±íÌí¼ÓÐÂÊý¾ÝÐС¡¡¡DELETE --´ÓÊý¾Ý¿â±íÖÐɾ³ýÊý¾ÝÐС¡¡¡UPDATE --¸üÐÂÊý¾Ý¿â±íÖеÄÊý¾Ý¡¡¡¡--Êý¾Ý¶¨Òå¡¡¡¡CREATE TABLE --´´½¨Ò»¸öÊý¾Ý¿â±í¡¡¡¡DROP TABLE --´ÓÊý¾Ý¿âÖÐɾ³ý±í¡¡¡¡ALTER TABLE --ÐÞ¸ÄÊý¾Ý¿â±í½á¹¹¡¡¡¡CREATE VI ......