¶¯Ì¬sqlÓï¾ä»ù±¾Óï·¨
SQL code
¶¯Ì¬sqlÓï¾ä»ù±¾Óï·¨
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃExecÖ´ÐÐ
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- ´íÎó,²»»áÌáʾ´íÎ󣬵«½á¹ûΪ¹Ì¶¨ÖµFiledName,²¢·ÇËùÒª¡£
Exec('select ' + @fname + ' from tableName') -- Çë×¢Òâ ¼ÓºÅǰºóµÄ µ¥ÒýºÅµÄ±ßÉϼӿոñ
µ±È»½«×Ö·û´®¸Ä³É±äÁ¿µÄÐÎʽҲ¿É
declare @fname varchar(20)
set @fname = 'FiledName' --ÉèÖÃ×Ö¶ÎÃû
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- ³É¹¦
exec sp_executesql @s -- ´Ë¾ä»á±¨´í
declare @s Nvarchar(1000) -- ×¢Òâ´Ë´¦¸ÄΪnvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- ³É¹¦
exec sp_executesql @s -- ´Ë¾äÕýÈ·
3. Êä³ö²ÎÊý
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--ÈçºÎ½«execÖ´Ðнá¹û·ÅÈë±äÁ¿ÖУ¿
declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
Ïà¹ØÎĵµ£º
¡¾Ñ§Ï°µãµãµÎµÎ¡¿ ÔںܶàÇé¿öÓë»·¾³ÖУ¬ÎÒÃǶ¼Òª¶ÔÒÑÓеÄÊý¾Ý¿â½øÐб¸·Ý£¬ÕâÑùÒÔ·ÀÖ¹µ±ÎÒÃÇÒòÈËΪ»òÕß·þÎñÆ÷µÈÖî¶àÔÒò¶øµ¼ÖµÄÊý¾Ý¶ªÊ§£¬
´Ó¶ø¸øÎÒÃÇ´øÀ´ºÜ´óµÄÂé·³£¬ÎÒÒ²ÊǽӴ¥SQLû¶à³¤Ê±¼ä£¬¶ÔÓëËüÇ¿´óµÄÊý¾Ý¿É»Ö¸´¹¦ÄÜ Ò²Ö»ÊÇÂÔÖªÒ»¶þ£¬µ±È»ÔÚÕâÀïÒ²Ö»ÊÇ×ö¶ÔËü×öÂÔÂԵļǼ£¬
´¿´âÊÇ ......
·Ï»°ÉÙ˵,(ÒѾÓоä·Ï»°ÁË.)
¿´±í
Óï¾ä: SELECT * from C
//---------------------------------------
Cno Cname Teacher
---- ---------- --------
1 ÓïÎÄ Æî¾²
2 &n ......
1) ʱ¼äת»»º¯ÊýÖÐÈç¹ûÓÐʱ¼ä±äÁ¿yyyy-mm-ddÁ½±ßÐèÁ½''
to_date(''' || to_char(a_valid_date_end, 'yyyy-mm-dd') ||''', ''yyyy-mm-dd'')
2) select distinct(e.itemnum) bulk collect into v_itemnum
½«×Ö·û´®Êý×éÒÔ·Ö¸ô·û·Ö¸ôµÄ×Ö·û´®µ÷ÓÃ
pkg_maximo_common.get_string(v_itemnum, ',', '')
3) unionÖ»Êǽ«Á½¸ö½á¹ ......
LIKEºÍͨÅä·û
©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥©¥
È·¶¨¸ø¶¨µÄ×Ö·û´®ÊÇ·ñÓëÖ¸¶¨µÄģʽƥÅ䡣ģʽ¿ÉÒÔ°üº¬³£¹æ×Ö·ûºÍͨÅä·û×Ö·û¡£Ä£Ê½Æ¥Åä¹ý³ÌÖУ¬³£¹æ×Ö·û±ØÐëÓë×Ö·û´®ÖÐÖ¸¶¨µÄ×Ö·ûÍêȫƥÅ䡣Ȼ¶ø£¬¿ÉʹÓÃ×Ö·û´®µÄÈÎÒâÆ¬¶ÎÆ¥ÅäͨÅä·û¡£ÓëʹÓÃ
= ºÍ != ×Ö·û´®±È½ÏÔËËã·ûÏà±È£¬Ê¹ÓÃͨÅä·û¿Éʹ LIKE ÔËË ......
----------Dbf µ¼Èë Sql Server±í----------
ÒÔϾùÒÔSQL2000¡¢VFP6¼°ÒÔÉϵıíΪÀý
´úÂëµ¼È룺²éѯ·ÖÎöÆ÷ÖÐÖ´ÐÐÈçÏÂÓï¾ä(ÏÈÑ¡Ôñ¶ÔÓ¦µÄÊý¾Ý¿â)
-------------Èç¹û½ÓÊܵ¼ÈëÊý¾ÝµÄSQL±íÒÑ´æÔÚ
--Èç¹û½ÓÊܵ¼ÈëÊý¾ÝµÄSQL±íÒѾ´æÔÚ
Insert Into ÒѾ´æÔÚµÄSQL±íÃû Select * from openrowset('MSDASQL','Driver=Micros ......