SQL SERVER – Difference Between EXEC and EXECUTE vs EXEC() – Use EXEC/EXECUTE for SP always
2011-07-28 15:17
766 查看
http://blog.sqlauthority.com/2007/09/13/sql-server-difference-between-exec-and-execute-vs-exec-use-execexecute-for-sp-always/
SQL SERVER – Difference Between EXEC and EXECUTE vs EXEC() – Use EXEC/EXECUTE for SP always
September 13, 2007 by pinaldaveWhat is the difference between EXEC and EXECUTE?
They are the same. Both of them executes stored procedure when called as
EXEC sp_help GO EXECUTE sp_help GO
I have seen enough times developer getting confused between EXEC and EXEC(). EXEC command executes stored procedure where as EXEC() function takes dynamic string as input and executes them.
EXEC('EXEC sp_help') GO
Another common mistakes I have seen is not using EXEC before stored procedure. It is always good practice to use EXEC before stored procedure name even though SQL Server assumes any command as stored procedure when it does not recognize the first statement. Developer learns while working with Query Editor in SSMS that EXEC is not necessary before running any stored procedure. However, consider following two test and you will see why EXEC or EXECUTE is necessary in many cases and good practice to use it.
TEST 1 : No Errors
USE AdventureWorks; GO ----Try this first independently this will throw an error sp_helptext 'dbo.uspPrintError' GO ----Try this second independently this will work fine EXEC sp_helptext 'dbo.uspPrintError' GO
TEST 2 : EXEC prevents error
USE AdventureWorks; GO ----Try this first independently this will throw an error SELECT * FROM Sales.Individual sp_helptext 'dbo.uspPrintError' GO ----Try this second independently this will work fine SELECT * FROM Sales.Individual EXEC sp_helptext 'dbo.uspPrintError' GO
Test 2 indicates that using EXEC or EXECUTE is good practice as it always executes the stored procedure, when not using EXEC can confuse SQL SERVER to misinterpret commands and may create errors.
相关文章推荐
- [Ramda] Difference between R.converge and R.useWith
- SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison
- Difference Between ZIP and GZIP | Difference Between | ZIP vs GZIP
- Difference between href and reload for refreshing in javascript
- SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?
- Difference to between COMPUTE STATISTICS and VALIDATE STRUCTURE for ANALYZE INDEX
- Difference between ksh a.sh and exec a.sh
- 【iOS】Difference Between objectForKey and valueForKey in NSDictionary
- Difference between Class.forName() and ClassLoader.loadClass()
- SQL Server: Difference Between Locking, Blocking and Dead Locking
- Difference between px, dp, dip and sp in Android?
- Difference between executor.submit and executor.execute
- What is the difference between objectForKey and valueForKey?
- Difference Between Class.forName() and ClassLoader.loadClass()
- What is the difference between HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR
- Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class
- What is the difference between max-device-width and max-width for mobile web?
- [Forward]: SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison
- What’s the difference between Catia, SolidWorks and Inventor? Which one for me?
- Difference between maven scope compile and provided for JAR packaging