Batch vs SQL statement

but)

The SQL statement is a single SQL statement (for example, SELECT * FROM table1 or SET NOCOUNT ON). A package, on the other hand, is a series of SQL statements sent to the server for execution as a whole unit. Statements in the package are compiled into one execution plan. Parties divided by GO

Thus, the only difference between the SQL statement and Batch is that each SQL statement is sent to the server as a separate unit and, therefore, compiled separately from other SQL statements, and the SQL commands in the package are compiled together?

b) I assume that one of the main differences between a stored procedure and a package is that the stored procedures are precompiled, while the lots arent?

thanks

+6
sql sql-server tsql
source share
1 answer

but. Only if each SQL statement is run individually (for example, in SSMS or on the client).

Two operators = "package" always, even if GO is not involved. GO simply tells the tools, such as SSMS, to break down messages into an engine.

b. not quite right. The stored procedure is pre-analyzed, but not compiled into the execution plan until it is called, and not in the plan cache. A batch is analyzed and compiled at a time and can generate a reusable plan.

Edit, after comment:

  • The terms “operator” and “package” are two different concepts.
  • Any text sent to the database engine is a package
  • the text is literally like this: no processing is performed by client tools: only the text is sent to the database engine
  • text consists of SQL statements

So,

  • A package consists of at least one character of text / one operator (but there may be 2, 20 or 20,000 statements)
  • GO tells SQL tools where the "block of text" / "assembly of instructions" is broken down into individual calls to the database engine (= lots)
+5
source share

All Articles