![]() |
Learn more about Toad for SQL Server Find solutions and downloads at the |
< To bookmark a page, right-click the Page Title and select Add to Favorites / Bookmark This Page |
Toad for SQL Server 5.7 |
You can use Toad Scripts to improve flow control in your SQL scripts. Toad Scripts support script functions and statement functions.
Caution: Toad Script functions automatically terminate a batch. To avoid terminating before a batch completes, make sure that any Toad Script functions are placed between batches.
Script functions are formatted as comments using "--TOAD:" as shown in the following example:
-- TOAD: SET a = 10
In this example, the function sets the predefined parameter "a" to the value 10. "SET" is the actual function and "a = 10" are the parameters to the SET function.
Statement functions are used inside of statements and unlike script functions, they cannot be used in products outside of Toad. (Because script functions are comments they are ignored by other products). The following displays an example of a statement function:
SELECT {{Unquote(:a)}}
In this example, the statement function is in bold inserts the value of the bind variable as a literal value before the statement is sent to the server. This is usefule when you need to use a bind variable in places where they are not allowed.
Here is a basic example of how Toad Scripts can be used.
-- TOAD: SET a = 1
-- TOAD: WHILE a < 10
CREATE TABLE toadscripttest{{Unquote(:a)}} (id INT);
-- TOAD: SET a = a + 1
-- TOAD: ENDWHILE
This example creates 10 tables called toadscripttest1 to toadscripttest10. The first line sets a predefined bind variable a to 1. The second line checks whether the value of a is less than 10, and as long as it is, it repeats everything between this row and the corresponding ENDWHILE line. The third line executes a create table statement and inserts the predefined value of :a unquoted. The fourth line increments a with 1 and the last line ends the while statement.
See Example Script: Populate Tables and Data for a more detailed example of creating a Toad script.