zoukankan      html  css  js  c++  java
  • SQL SERVER – Difference Between EXEC and EXECUTE vs EXEC() – Use EXEC/EXECUTE for SP always

    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

    What 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.

    做个快乐的自己。
  • 相关阅读:
    freeCAD定制界面
    freeCAD预选项编辑器
    freeCAD文档结构
    FreeCAD鼠标操作指南
    freeCAD下载与安装
    freeCAD特性列表
    关于freeCAD
    html 试题试卷(包含latex)下载成word
    latex转word公式 java (latextoword,latex_word,latex2word,latex_omml)
    oracle 行列转换
  • 原文地址:https://www.cnblogs.com/Jessy/p/2119594.html
Copyright © 2011-2022 走看看