If a variable assignment is given as a file operand on the command line of an awk script, that assignment will be carried out when awk gets to that point in using the file operands.
Example:
awk -f script.awk a=1 file1 file2 a=2 file3 a=3 file4
Here, the awk variable a will be set to 1 before reading file1. The variable will retain that value (unless it's changed by the awk program) until the file file2 has been read. It is then set to 2 etc.
This means that your command line properly sets the variable ID to 4 before reading text.sql.
The difference between -v ID=4 and this is that ID will not have been set in any BEGIN block. Using -v ID=4 sets ID to 4 and it will be available in BEGIN blocks.
Another issue with your code is that you use ID as a literal string in
if ($1 ~/--/ && $2 ~/^ID/){
That is, you test whether the second field starts with the string ID.
It also seems as if you expect your while loop to loop over the lines of the file. That line-by-line reading loop is already built into the way awk operates already and it very seldom needs to be explicit.
Instead,
$1 == "--" && $2 == ID { getline; print }
This will use string comparisons (not regular expression matches) to check whether the first whitespace-delimited field on the current line is the string -- and whether the second such field is the value of the variable ID. If so, the next line of input is read and printed.
To use regular expressions instead,
$0 ~ "^-- " ID "$" { getline; print }
This would match every line that starts with a double dash and a single space, followed by the value of ID and the end of the line ($ matches the end of the line). The line after the matching line would be printed.