Download presentation
Presentation is loading. Please wait.
Published byAnnice Sanders Modified over 9 years ago
1
InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer
2
Agenda What is Dynamic SQL?What is Dynamic SQL? Why use Dynamic SQL?Why use Dynamic SQL? The Reason for the New interfaceThe Reason for the New interface %SQL.Statement with Examples%SQL.Statement with Examples %SQL.StatementResult with Examples%SQL.StatementResult with Examples Interactive SQL Shell with ExamplesInteractive SQL Shell with Examples What’s NextWhat’s Next
3
What is Dynamic SQL? From the Documentation:From the Documentation: –“ Dynamic SQL refers to SQL statements that are prepared and executed at runtime. Dynamic SQL lets you program within Caché in a manner similar to an ODBC or JDBC application (except that you are executing the SQL statement within the same process context as the database engine)”. (The Dynamic SQL Documentation is very good.)(The Dynamic SQL Documentation is very good.)
4
Why use Dynamic SQL? The ability to instantiate one or many statement objects, prepare statements once and execute them many times.The ability to instantiate one or many statement objects, prepare statements once and execute them many times. Results can be retained in local variables, arrays or any other structure available for immediate or later processing.Results can be retained in local variables, arrays or any other structure available for immediate or later processing. The ability to prepare once outside of a loop and to execute possibly many times inside a loop can be key to performance.The ability to prepare once outside of a loop and to execute possibly many times inside a loop can be key to performance.
5
Why use Dynamic SQL? Cont. Dynamic SQL is Dynamic. Changes to table/view definitions or data selectivity changes don’t require an application recompile.Dynamic SQL is Dynamic. Changes to table/view definitions or data selectivity changes don’t require an application recompile. The application can generate the statement to be executed. Generated statements can be prepared and executed using inputs from any source.The application can generate the statement to be executed. Generated statements can be prepared and executed using inputs from any source.
6
Why Create a New Interface? A consistent interface to the various dynamic SQL features InterSystems provides was needed, as well as a way to recommend “Best Practices”.A consistent interface to the various dynamic SQL features InterSystems provides was needed, as well as a way to recommend “Best Practices”. We already had %Library.ResultSet and %ResultSet.SQL, but both had limitations. %Library.ResultSet is restricted in the number of parameters and %ResultSet.SQL was restricted to select statements only. We also had a variety of %XSQL statement interfaces implemented to support SQL MigrationsWe already had %Library.ResultSet and %ResultSet.SQL, but both had limitations. %Library.ResultSet is restricted in the number of parameters and %ResultSet.SQL was restricted to select statements only. We also had a variety of %XSQL statement interfaces implemented to support SQL Migrations %SQL.Statement is a consolidation and provides a single consistent interface for all dynamic statements.%SQL.Statement is a consolidation and provides a single consistent interface for all dynamic statements.
7
The %SQL.Statement class The %SQL.Statement class implements an interface for managing dynamic SQL statements. The interface consists of the following methods and properties.The %SQL.Statement class implements an interface for managing dynamic SQL statements. The interface consists of the following methods and properties. –%Prepare – Prepare a SQL statement –%Execute – Execute the earlier prepared statement –%ExecDirect – Prepare and execute in one step –%Metadata – Returns Meta-data on prepared statement –%Selectmode – Set the select mode for the statement –%SchemaPath – Set the default Schema(s) –%ObjectSelectMode – Determine column types
8
The %ObjectSelectMode property The %ObjectSelectMode property - Allows you to swizzle objects, modify the object, and save it back, all without having to open anything. Example:The %ObjectSelectMode property - Allows you to swizzle objects, modify the object, and save it back, all without having to open anything. Example: –set result.Spouse.Name=“LastName,First” Avoid “underwriting” SQL applied updates to swizzled objects. Using Dynamic SQL with %ObjectSelectMode can help to avoid this issue.Avoid “underwriting” SQL applied updates to swizzled objects. Using Dynamic SQL with %ObjectSelectMode can help to avoid this issue. Polymorphic dispatch – objects swizzled through a result set are fully polymorphic.Polymorphic dispatch – objects swizzled through a result set are fully polymorphic. Let’s look at some examples:Let’s look at some examples:
9
Statement Results Executing a statement with either %Execute or %ExecDirect returns an object that implements the %SQL.StatementResult interface.Executing a statement with either %Execute or %ExecDirect returns an object that implements the %SQL.StatementResult interface. This object can be a unitary value, a result set, or a context object that is returned from a CALL statement.This object can be a unitary value, a result set, or a context object that is returned from a CALL statement. Serial binding of dynamic results. (2010.2)Serial binding of dynamic results. (2010.2) –A context object that holds multiple result sets can be used in the same way as a single result set object. What you get when doing this is the automatic binding of the first dynamic result set and invoking %Next() iteratively will move through the rows of that result set until it reaches the end. At any time during this process, the user can invoke %MoreResults and the currently bound result set is closed and the next result set is bound.
10
Interactive SQL Shell The interactive SQL shell allows the user to execute SQL statements dynamically.The interactive SQL shell allows the user to execute SQL statements dynamically. There are two execution modes - immediate and deferred.There are two execution modes - immediate and deferred. The execution mode can be set by executing a set executemode command with immediate or deferred as the executemode value. If the execution mode is immediate then SQL statements are prepared and executed immediately. If execute mode is deferred then a statement is only prepared and not executed until either a # or go command is entered.The execution mode can be set by executing a set executemode command with immediate or deferred as the executemode value. If the execution mode is immediate then SQL statements are prepared and executed immediately. If execute mode is deferred then a statement is only prepared and not executed until either a # or go command is entered.
11
Interactive SQL Shell, cont. The result of executing the statement is displayed on the current device after the statement is executed.The result of executing the statement is displayed on the current device after the statement is executed. SQL statements can span multiple lines (2010.2+).SQL statements can span multiple lines (2010.2+). The Shell supports statement recall. Any statement that is prepared may be recalled from the statement buffer.The Shell supports statement recall. Any statement that is prepared may be recalled from the statement buffer. DISPLAYMODE – In 2010.2 this feature exists but is primarily a convenience. It does allude to the ability to produce an XML document from a result set. This will be developed more in 2011.1DISPLAYMODE – In 2010.2 this feature exists but is primarily a convenience. It does allude to the ability to produce an XML document from a result set. This will be developed more in 2011.1 Let’s see some examples:Let’s see some examples:
12
What’s Next? Closer integration between Dynamic SQL and Zen/CSP.Closer integration between Dynamic SQL and Zen/CSP. SQL Manager in System Management Portal updated to use Dynamic SQL.SQL Manager in System Management Portal updated to use Dynamic SQL. TSQL and other shells may be integrated.TSQL and other shells may be integrated.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.