InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

12
InterSystems’ New InterSystems’ New Dynamic SQL Dynamic SQL Features Features Louise Parberry – Senior Louise Parberry – Senior Sales Engineer Sales Engineer

description

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.)

Transcript of InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Page 1: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

InterSystems’ New Dynamic InterSystems’ New Dynamic SQL FeaturesSQL Features

Louise Parberry – Senior Sales EngineerLouise Parberry – Senior Sales Engineer

Page 2: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

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

Page 3: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

What is Dynamic SQL?

• From the Documentation:From the Documentation:– ““Dynamic SQL Dynamic SQL refers to SQL statements that are refers to SQL statements that are

prepared and executed at runtime. Dynamic SQL lets prepared and executed at runtime. Dynamic SQL lets you program within Caché in a manner similar to an you program within Caché in a manner similar to an ODBC or JDBC application (except that you are ODBC or JDBC application (except that you are executing the SQL statement within the same process executing the SQL statement within the same process context as the database engine)”.context as the database engine)”.

• (The Dynamic SQL Documentation is very good.)(The Dynamic SQL Documentation is very good.)

Page 4: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Why use Dynamic SQL?

• The ability to instantiate one or many statement objects, The ability to instantiate one or many statement objects, prepare statements once and execute them many times.prepare statements once and execute them many times.

• Results can be retained in local variables, arrays or any Results can be retained in local variables, arrays or any other structure available for immediate or later processing.other structure available for immediate or later processing.

• The ability to prepare once outside of a loop and to execute The ability to prepare once outside of a loop and to execute possibly many times inside a loop can be key to possibly many times inside a loop can be key to performance.performance.

Page 5: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Why use Dynamic SQL? Cont.

• Dynamic SQL is Dynamic. Changes to table/view definitions Dynamic SQL is Dynamic. Changes to table/view definitions or data selectivity changes don’t require an application or data selectivity changes don’t require an application recompile.recompile.

• The application can generate the statement to be executed. The application can generate the statement to be executed. Generated statements can be prepared and executed using Generated statements can be prepared and executed using inputs from any source.inputs from any source.

Page 6: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Why Create a New Interface?

• A consistent interface to the various dynamic SQL features A consistent interface to the various dynamic SQL features InterSystems provides was needed, as well as a way to InterSystems provides was needed, as well as a way to recommend “Best Practices”.recommend “Best Practices”.

• We already had %Library.ResultSet and %ResultSet.SQL, We already had %Library.ResultSet and %ResultSet.SQL, but both had limitations. %Library.ResultSet is restricted in but both had limitations. %Library.ResultSet is restricted in the number of parameters and %ResultSet.SQL was the number of parameters and %ResultSet.SQL was restricted to select statements only. We also had a variety of restricted to select statements only. We also had a variety of %XSQL statement interfaces implemented to support SQL %XSQL statement interfaces implemented to support SQL MigrationsMigrations

• %SQL.Statement is a consolidation and provides a single %SQL.Statement is a consolidation and provides a single consistent interface for all dynamic statements.consistent interface for all dynamic statements.

Page 7: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

The %SQL.Statement class

• The %SQL.Statement class implements an interface for The %SQL.Statement class implements an interface for managing dynamic SQL statements. The interface consists managing dynamic SQL statements. The interface consists of the following methods and properties. of the following methods and properties.

– %Prepare – Prepare a SQL statement%Prepare – Prepare a SQL statement– %Execute – Execute the earlier prepared statement%Execute – Execute the earlier prepared statement– %ExecDirect – Prepare and execute in one step%ExecDirect – Prepare and execute in one step– %Metadata – Returns Meta-data on prepared statement%Metadata – Returns Meta-data on prepared statement– %Selectmode – Set the select mode for the statement%Selectmode – Set the select mode for the statement– %SchemaPath – Set the default Schema(s)%SchemaPath – Set the default Schema(s)– %ObjectSelectMode – Determine column types%ObjectSelectMode – Determine column types

Page 8: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

The %ObjectSelectMode property

• The %ObjectSelectMode property - Allows you to swizzle The %ObjectSelectMode property - Allows you to swizzle objects, modify the object, and save it back, all without objects, modify the object, and save it back, all without having to open anything. Example:having to open anything. Example:

– set result.Spouse.Name=“LastName,First”set result.Spouse.Name=“LastName,First”

• Avoid “underwriting” SQL applied updates to swizzled Avoid “underwriting” SQL applied updates to swizzled objects. Using Dynamic SQL with %ObjectSelectMode can objects. Using Dynamic SQL with %ObjectSelectMode can help to avoid this issue.help to avoid this issue.

• Polymorphic dispatch – objects swizzled through a result Polymorphic dispatch – objects swizzled through a result set are fully polymorphic.set are fully polymorphic.

• Let’s look at some examples:Let’s look at some examples:

Page 9: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Statement Results

• Executing a statement with either Executing a statement with either %Execute%Execute or or %ExecDirect%ExecDirect returns returns an object that implements the an object that implements the %SQL.StatementResult %SQL.StatementResult interface.interface.

• This object can be a unitary value, a result set, or a context object This object can be a unitary value, a result set, or a context object that is returned from a that is returned from a CALLCALL statement. 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 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 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 this is the automatic binding of the first dynamic result set and invoking %Next() iteratively will move through the rows of that invoking %Next() iteratively will move through the rows of that result set until it reaches the end. At any time during this process, result set until it reaches the end. At any time during this process, the user can invoke %MoreResults and the currently bound the user can invoke %MoreResults and the currently bound result set is closed and the next result set is bound.result set is closed and the next result set is bound.

Page 10: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Interactive SQL Shell

• The interactive SQL shell allows the user to execute SQL The interactive SQL shell allows the user to execute SQL statements dynamically.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 The execution mode can be set by executing a set set executemodeexecutemode command with command with immediateimmediate or or deferreddeferred as the as the executemode value. If the execution mode is immediate executemode value. If the execution mode is immediate then SQL statements are prepared and executed then SQL statements are prepared and executed immediately. If execute mode is deferred then a statement immediately. If execute mode is deferred then a statement is only prepared and not executed until either a is only prepared and not executed until either a ## or or gogo command is entered.command is entered.

Page 11: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

Interactive SQL Shell, cont.

• The result of executing the statement is displayed on the The result of executing the statement is displayed on the current device after the statement is executed. 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 The Shell supports statement recall. Any statement that is prepared may be recalled from the statement buffer.prepared may be recalled from the statement buffer.

• DISPLAYMODE – In 2010.2 this feature exists but is DISPLAYMODE – In 2010.2 this feature exists but is primarily a convenience. It does allude to the ability to primarily a convenience. It does allude to the ability to produce an XML document from a result set. This will be produce an XML document from a result set. This will be developed more in 2011.1developed more in 2011.1

• Let’s see some examples:Let’s see some examples:

Page 12: InterSystems’ New Dynamic SQL Features Louise Parberry – Senior Sales Engineer.

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 SQL Manager in System Management Portal updated to use Dynamic SQL.use Dynamic SQL.

• TSQL and other shells may be integrated.TSQL and other shells may be integrated.