SQL»ù±¾Ê¹ÓúʹúÂëÓÅ»¯
EXECºÍsp_executesqlµÄÇø±ð£¿
DBCC FREEPROCCACHE --¡·Çå¿Õ»º´æÖеÄÖ´Ðмƻ®
SELECT cacheobjtype,objtype,usecounts,sql from sys.syscacheobjects WHERE sql NOT LIKE '%cach%' AND sql NOT LIKE '%sys.%' --¡·²éÕÒ¶ÔÓ¦µÄ»º´æÖеĶÔÓ¦¼Æ»®
SQL ServerΪÿһ¸öµÄ²éѯ×Ö·û´®´´½¨ÐµÄÖ´Ðмƻ®£¬¼´Ê¹²éѯģʽÏàͬҲÊÇÕâÑù
EXEC³ýÁ˲»Ö§³Ö¶¯Ì¬Åú´¦ÀíÖеÄÊäÈë²ÎÊýÍ⣬ËûÒ²²»Ö§³ÖÊä³ö²ÎÊý
Ó÷¨£º
Exec('select * from CustInfo')
Exec sp_executesql N'select * from CustInfo'
declare @paraName varchar(20)
set @paraName='CustName'
Exec('select '+@paraName+' from CustInfo')--¼ÓºÅÇ°ºó¼ÓÉÏ¿Õ¸ñ
Èç¹ûÄãÒª°ÑÊä³ö·µ»Ø¸øµ÷ÓÃÅú´¦ÀíÖеıäÁ¿£º
DECLARE @sql NVARCHAR(MAX),@RecordCount INT
SET @sql = 'SELECT COUNT(ORDERID) from Orders';
CREATE TABLE #T(TID INT);
INSERT INTO #T EXEC(@sql);
SET @RecordCount = (SELECT TID from #T)
SELECT @RecordCount
DROP TABLE #T
sp_executesqlÃüÁîÔÚSQL ServerÖÐÒýÈëµÄ±ÈEXECÃüÁîÍíһЩ£¬ËüÖ÷ҪΪÖØÓÃÖ´Ðмƻ®Ìṩ¸üºÃµÄÖ§³Ö¡£
ËüµÄ¹¹³É°üÀ¨£º´úÂë¿ì£¬²ÎÊýÉùÃ÷²¿·Ö£¬²ÎÊý¸³Öµ²¿·Ö
EXEC sp_executesql
@stmt = <statement>,--ÀàËÆ´æ´¢¹ý³ÌÖ÷Ìå
@params = <params>, --ÀàËÆ´æ´¢¹ý³Ì²ÎÊý²¿·Ö
<params assignment> --ÀàËÆ´æ´¢¹ý³Ìµ÷ÓÃ
@stmt²ÎÊýÊÇÊäÈëµÄ¶¯Ì¬Åú´¦Àí£¬Ëü¿ÉÒÔÒýÈëÊäÈë²ÎÊý»òÊä³ö²ÎÊý£¬ºÍ´æ´¢¹ý³ÌµÄÖ÷ÌåÓï¾äÒ»Ñù£¬Ö»²»¹ýËüÊǶ¯Ì¬µÄ£¬¶ø´æ´¢¹ý³ÌÊǾ²Ì¬µÄ£¬²»¹ýÄãÒ²¿ÉÒÔÔÚ´æ´¢¹ý³ÌÖÐʹÓÃsp_executesql;
@params²ÎÊýÓ붨ÒåÊäÈë/Êä³ö²ÎÊýµÄ´æ´¢¹ý³ÌÍ·ÀàËÆ£¬Êµ¼ÊÉϺʹ洢¹ý³ÌÍ·µÄÓï·¨ÍêÈ«Ò»Ñù£»
@<params assignment> Óëµ÷Óô洢¹ý³ÌµÄEXEC²¿·ÖÀàËÆ¡£
Ó÷¨£º
declare @paraName varchar(20)
set @paraName='CustName'
declare @temp nvarchar(1000)
set @temp='select ' +@paraName+ ' from CustInfo'
Exec(@temp)
exec sp_executesql @temp--ÒòΪ sp_executesqlÖ»ÄÜÖ´ÐеÄÀàÐÍΪntext/nchar/nvarchar£¬ËùÒÔ@tempÐèÒª´Óvarchar¸ÄΪnvarchar
DECLARE @TableName VARCHAR(50),@sql NVARCHAR(MAX),@OrderID INT;
SET @TableName = 'CustInfo ';
SET @OrderID = 10;
SET @sql = 'SELECT * from '+QUOTENAME(@TableName) + ' WHER
Ïà¹ØÎĵµ£º
ÏÈÕ¾ÔÚÓ¦ÓóÌÐòµÄ½Ç¶È˵˵ËüÃǵIJ»Í¬¡£
1¡¢ Ö±½ÓÆ´ SQL
¾ÍÏñ´ó¼ÒÁ˽âµÄÄÇÑù£¬Ö±½ÓÆ´ SQL ´øÀ´ÁË SQL ×¢Èë¹¥»÷£¬´øÀ´ÁËƴʱЩÐíµÄÐÔÄÜËðʧ£¬µ«ÊÇÆ´²»ÓÃÌí¼Ó SqlParameter £¬»áÉÙдºÜ¶à´úÂë——ºÜ¶àÈËϲ»¶Ö±½ÓÆ´£¬Ò²Ðí¾ÍÒòΪÕâµã¡£ÕâÖÖ×ö·¨»á°ÑÄãÆ´ºÃµÄ SQL ÔÑùÖ±½Ó·¢Ë͵½ DB ·þÎñÆ÷È¥Ö´ÐС££¨×¢ÒâÀàËÆ ......
½ñÌì×öSQL ÓÅ»¯£¬²éÕÒÖ´Ðмƻ®Ê±£¬Ö´Ðмƻ®£¬·¢ÏÖ´ËÖ´Ðмƻ®ÓëÒÔÍùµÄ¼Æ»®ÓÐËùÇø±ð£»ÕÒ¼»¥ÁªÍø£¬ÖÕÓÚÕÒһƪÓйØÑо¿±È½ÏÉîÈëµÄÎÄÕ£»
ÔÖ´Ðмƻ®Ê¹ÓõÄÊÇË÷ÒýɨÃ裬ͻȻһÏ»áʹÓÃË÷Òý¸²¸Ç¼¼Êõ£¬Ð§ÂÊ´óÔö£»
SELECT * µÄÕæÏࣺË÷Òý¸²¸Ç(index coverage)
SELECT *µÄЧÂʺÜÔã¸âÂ𣿵±È»£¬ËùÓÐÈ˶¼ÖªµÀÕâÒ»µã£¬µ«ÊÇΪʲôÄØ£ ......
ËäÈ»²éѯËÙ¶ÈÂýµÄÔÒòºÜ¶à£¬µ«ÊÇÈç¹ûͨ¹ýÒ»¶¨µÄÓÅ»¯£¬Ò²¿ÉÒÔʹ²éѯÎÊÌâµÃµ½Ò»¶¨³Ì¶ÈµÄ½â¾ö¡£ ²éѯËÙ¶ÈÂýµÄÔÒòºÜ¶à£¬³£¼ûÈçϼ¸ÖÖ£º
1¡¢Ã»ÓÐË÷Òý»òÕßûÓÐÓõ½Ë÷Òý(ÕâÊDzéѯÂý×î³£¼ûµÄÎÊÌ⣬ÊdzÌÐòÉè¼ÆµÄȱÏÝ)
2¡¢i/oÍÌÍÂÁ¿Ð¡£¬ÐγÉÁËÆ¿¾±Ð§Ó¦¡£
&n ......
ÏÖÏó:
ÓÃVS 2008´ò¿ªVS 2005¿ª·¢µÄ±¨±íºó,»á°Ñ±¨±íµ¥ÏòÉý¼¶µ½2008µÄshcema,²»ÄÜÔÙת»Ø2005µÄschema.ÔÙ·¢²¼µ½SQL Server 2005ÉÏʱ»á±¨´í:"ÎÞ·¨Éý¼¶µÄÃû³Æ¿Õ¼ä".
ÕÒµ½µÄ½â¾ö°ì·¨:
ÔÚSQL Server 2005ËùÔڵĻúÆ÷ÉÏ¿ª·¢±¨±í.
°ÑSQL ServerÉý¼¶µ½ SQL Server 2008.
¾Ý˵ÓÐReporting Service 2008µÄµ¥¶À°²×°°æ,¿ÉÒÔ ......
¶¨Ò壺ºÎΪ´¥·¢Æ÷£¿ÔÚSQL ServerÀïÃæÒ²¾ÍÊǶÔijһ¸ö±íµÄÒ»¶¨µÄ²Ù×÷£¬´¥·¢Ä³ÖÖÌõ¼þ£¬´Ó¶øÖ´ÐеÄÒ»¶Î³ÌÐò¡£´¥·¢Æ÷ÊÇÒ»¸öÌØÊâµÄ´æ´¢¹ý³Ì¡£
³£¼ûµÄ´¥·¢Æ÷ÓÐÈýÖÖ£º·Ö±ðÓ¦ÓÃÓÚInsert , Update , Delete ʼþ¡£
ÎÒΪʲôҪʹÓô¥·¢Æ÷£¿±ÈÈ磬ÕâôÁ½¸ö±í£º
&n ......