1

This is like a little existential doubt...

Which is the right way to say that you run a script in SQL?

  1. I compiled the script xxxx

  2. I executed the script xxxx

I know is a little weird and noob to ask this, but I need to know.

I'm very sure the second way is the correct one, but I need more opinions.

Thank you very much.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Diego Arias
  • 171
  • 2
  • 14
  • 10
    One "runs" or "executes" scripts. The compilation is behind the scenes. I don't really like "executes", sounds like someone is dying. – Gordon Linoff Oct 12 '18 at 20:50
  • Thank you. This is because where I work almost everyone says "to compile scripts" and I feel like that's not correct. – Diego Arias Oct 12 '18 at 20:54
  • 1
    As a former programmer, one never compiles scripts, that's why they are called scripts because they are run by an interpreter program/engine. A program source code is compiled to build machine code which is executed. As for SQL I typically say executed, ran, or the change that it was associated with was deployed. – Chad Estes Oct 12 '18 at 21:02
  • @ChadEstes, scripts get compiled too. It might be on their first or each run but they get compiled. With T-SQL, you might even sometimes want it to be recompiled explicitly. – Cetin Basoz Oct 12 '18 at 21:14
  • @Cetin Basoz, perhaps it is symantics, but scripts are interpreted and converted to machine executable instructions, executed and dropped from memory. They never result in a packet of executable code which can be called repetitively without some mechanism in the script to keep the process alive. Source code is compiled into an executable file (*.exe, *.dll, etc.) which is not human readable, but contains a set of binary instructions for the machine to run whenever the executable file is executed. – Chad Estes Oct 12 '18 at 21:19
  • @Cetin Basoz Yes, Java and .net are both intepreted and compiled because they make use of JIT compilation; compiling source code into an intermediate binary form (MSL or JVM byte code). This compiled piece is then interpreted into machine code to be executed. This allows these languages to be deployed to hundreds of different machines because they have a write once, deploy anywhere architecture. – Chad Estes Oct 12 '18 at 21:42
  • Way back in the day, programmers did not have this luxury; if you wanted to write a COBOL program to run on a Mainframe, UNIX, an iSeries box and in Windows you had to write four slightly different versions of the program for each environment, maybe more depending on differences in each platforms' various supported versions, but this protected the intellectual property of the code. Scripts don't allow for that as they have no intermediary compiled piece to redistribute. JIT compilation is the best of both worlds. – Chad Estes Oct 12 '18 at 21:49
  • https://stackoverflow.com/questions/2657268/whats-the-difference-between-compiled-and-interpreted-language – Chad Estes Oct 12 '18 at 21:51
  • 3
    It's fair to say that in almost any programming context, compiling is _not_ executing. It's just getting the source in a state to execute. – Nick.Mc Oct 13 '18 at 01:01
  • @ChadEstes, yes. For that reason maybe, Go is one of my favorites that cross compiles into native code for different platforms from a single code base and on a single machine - say Mac - (no intermediate - and also somewhat used as a scripting language for its fast compilation). – Cetin Basoz Oct 13 '18 at 12:11
  • 1
    @ChadEstes I think exactly like you. For me, scripts are run or executed, but not compiled. I appreciate all your comments guys. Thank you very much for your help. – Diego Arias Oct 16 '18 at 16:34

1 Answers1

2

It depends on what you actually want to say.

All queries in SQL Server follow the same process before they are executed.

  • Parse - Statement is broken down into individual words. Syntax errors and misspellings are detected on this step.
  • Validate (aka Resolve) – Ensures that the names of the objects are present as well as correct ownership permissions.
  • Optimise – SQL Server explores different ways to execute the query.
  • Compile – SQL Server generates execution plan (binary representation of the execution tree)

and finally

  • Execute
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pavel Nefyodov
  • 876
  • 2
  • 11
  • 29
  • I want to add that stored procedures are handled differently from views and statements. In most software houses you see them using views to store and build a query. But stored procedures are really compiled and saved so they are much faster. A view or statement gets interpreted every time, a stored procedure gets faster and faster after each execute. – Tarabass Oct 13 '18 at 07:10
  • @Tarabass Again, this depends (physical design changes since last execute, use of options in stored procedure definition, state of the SQL Server since last execute). Please see https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/recompile-a-stored-procedure?view=sql-server-2017 – Pavel Nefyodov Oct 13 '18 at 09:56
  • Thank you very much for that information. I will have it in mind to explain it to my team mates. – Diego Arias Oct 16 '18 at 16:35