¶¯Ì¬SQL»ù±¾Óï·¨
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
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
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃExecÖ´ÐÐ Àý: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
´íÎó: declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName  
Ïà¹ØÎĵµ£º
ÉÏÃæÎÒÃÇÌáµ½µÄÊÇһЩ»ù±¾µÄÌá¸ß²éѯËٶȵÄ×¢ÒâÊÂÏî,µ«ÊÇÔÚ¸ü¶àµÄÇé¿öÏÂ,ÍùÍùÐèÒª·´¸´ÊÔÑé±È½Ï²»Í¬µÄÓï¾äÒԵõ½×î¼Ñ·½°¸¡£×îºÃµÄ·½·¨µ±È»ÊDzâÊÔ£¬¿´ÊµÏÖÏàͬ¹¦ÄܵÄSQLÓï¾äÄĸöÖ´ÐÐʱ¼ä×îÉÙ£¬µ«ÊÇÊý¾Ý¿âÖÐÈç¹ûÊý¾ÝÁ¿ºÜÉÙ£¬ÊDZȽϲ»³öÀ´µÄ£¬Õâʱ¿ÉÒÔÓò鿴ִÐмƻ®£¬¼´£º°ÑʵÏÖÏàͬ¹¦ÄܵĶàÌõSQLÓï¾ä¿¼µ½²éѯ·ÖÎöÆ÷£¬°´ ......
ÈÃÎÒÃÇ´ÓÕâÑùÒ»¸öʾÀý¿ªÊ¼ËµÆð£¬ËüÔÚ SQL Server 2000 ºÍ 2005 Öж¼ÄÜÒýÆðËÀËø¡£ÔÚ±¾ÎÄÖУ¬ÎÒʹÓà SQL Server 2005 µÄ×îРCTP£¨ÉçÇø¼¼ÊõÔ¤ÀÀ£¬Community Technology Preview£©°æ±¾£¬SQL Server 2005 Beta 2£¨7 Ô·¢²¼£©Ò²Í¬ÑùÊÊÓá£Èç¹ûÄúûÓÐ Beta 2 »ò×îÐ嵀 CTP °æ±¾£¬ÇëÏÂÔØ SQL Serve ......
·½·¨Ò»£º
1.Èç¹û¸½¼ÓÊý¾Ý¿âʱ£¬Ã»ÓÐÖ¸¶¨Âß¼Ãû£¬ÔòÔÚSQL2005ÉÏÏÔʾΪ“ȫ·¾¶£¬ÇÒΪ´óд”¡£±ÈÈ磬Êý¾Ý¿âÎļþD:MyTest.mdfºÍD:MyTest.ldf£¬¸½¼Óµ½SQL·þÎñÆ÷ÉÏʱ£¬Èç¹ûûÓÐÖ¸¶¨Âß¼Ãû£¬ÔòÔÚSQL2005ÉÏÏÔʾΪ“D:MYTEST.MDF”¡£´Ëʱ£¬C#µÄÁ¬½Ó×Ö·û´®Îª“DataSource=dsName;AttatchDBFilename=&rdquo ......
×î½üÒ»Ö±ÔÚѧϰSQL serverµÄÄÚÈÝ¡£×òÌ쿼ÁËÒ»ÏÂÊÔ¡£¸Ð¾õÕæµÄÊDz»ÈÝÒ×°¡¡£ÌرðÊÇһЩ¸´ÔӵIJéѯ¡£¸ãµÃÎÒÍ·»èÄÔÕ͵ġ£²»¹ýÒ²ÊÇÓÉÓÚ×Ô¼ºµÄ֪ʶÕÆÎյĻ¹²»¹»Ôúʵ°¡¡£ËùÒÔ½ñÌ츴ϰÁËÒ»ÏÂT-SQlÓï¾äµÄÔöɾ¸Ä²é¡£·¢ÏÖµÄÈ·ÊÇÓкܶ඼Íü¼ÇÁË¡£ÏÖÔڰѽá¹ûд³öÀ´¡£ÒÔºó¿É²»ÒªÍüÁËѽ¡£
--SQLÓï¾ä¸´Ï° --Ò»,²åÈëinsertÓï¾ä --1,ins ......
1¡¢¹«Óñí±í´ïʽ (CTE) ¿ÉÒÔÈÏΪÊÇÔÚµ¥¸ö SELECT¡¢INSERT¡¢UPDATE¡¢DELETE »ò CREATE VIEW Óï¾äµÄÖ´Ðз¶Î§ÄÚ¶¨ÒåµÄÁÙʱ½á¹û¼¯¡£CTE ÓëÅÉÉú±íÀàËÆ£¬¾ßÌå±íÏÖÔÚ²»´æ´¢Îª¶ÔÏ󣬲¢ÇÒÖ»ÔÚ²éѯÆÚ¼äÓÐЧ¡£ÓëÅÉÉú±íµÄ²»Í¬Ö®´¦ÔÚÓÚ£¬CTE ¿É×ÔÒýÓ㬻¹¿ÉÔÚͬһ²éѯÖÐÒýÓöà´Î¡£
¡¡¡¡CTE ¿ÉÓÃÓÚ£º
¡¡¡¡´´½¨µÝ¹é²éѯ¡£ÓйØÏêϸР......