Question
· Jul 29

WHat is the difference between tsql method and querry

Hi, I'm wondering what are the diferences between a method or classmethod with the language set at tsql and a querry writen in sql.
If anyone know if there is some difference and what they are, I would be glad to learn. 😊

Product version: IRIS 2025.2
Discussion (3)3
Log in or sign up to continue

To compare methods (or class methods) marked with the language "tsql" and queries written in SQL, there are notable distinctions in use and behavior:

  1. Object-Oriented Interfaces vs Plain SQL Execution:

    • A class query defines predefined lookups for dynamic SQL queries use. It is often used to incorporate SQL into object-oriented paradigms, as part of a class definition. For instance, you can create a query inside a class that specifies structure, parameters, and row formats [1][2].
    • A method or classmethod with "tsql" marked can embed SQL logic, but it still leverages the broader capabilities of the class, such as property reference. These methods can be projected as stored procedures (using keywords like SqlProc) to be executed in SQL [3][2].
  2. Instance Context:

    • A regular SQL query operates on the database schema directly and doesn't inherently interact with object properties or instances unless explicitly bound, whereas tsql-methods/classmethods can directly handle object properties or interact programmatically with instances [3][2].
  3. Encapsulation and Reusability:

    • TSQL methods can encapsulate logic in an object-oriented way, reusing database interaction across different application parts with structured input parameters and exception handling. SQL queries remain purely a database-level interaction and do not inherently support programmatic encapsulation [3][2].
  4. Projection to Stored Procedures:

    • Classmethods marked with "tsql" use the SqlProc keyword can be projected as SQL stored procedures within the database. These stored procedures offer direct interaction as callable entities within the database via SQL statements [3][2].
  5. Runtime and Compilation Contexts:

    • SQL queries are typically dynamic and work at runtime using an SQL execution engine or shell. Methods marked as "tsql" might provide additional programming capabilities such as runtime configuration, object manipulation, and pre-defined error responses within the ObjectScript or T-SQL context [3][2].
  6. Avoidance of Memory Conflicts:

    • When using classmethods with embedded SQL (&sql syntax), care must be taken with memory instances to avoid overwriting certain updates, depending on where the object data resides (in memory or on disk). This potential issue does not directly apply to standalone SQL [4].

These distinctions highlight advantages for TSQL methods/classmethods when an object-oriented integration or additional programming logic is demanded, compared to SQL queries for pure relational data interactions alone. [4][3][2][1]

Sources:

the language=tsql feature allows you to write SQL statements that are supported by transact SQL.

https://docs.intersystems.com/iris20252/csp/docbook/Doc.View.cls?KEY=GTS...

InterSystems TSQL is an implementation of the Transact-SQL procedural language which supports many of the features of both the Sybase and Microsoft implementations. Transact-SQL is used with Sybase Adaptive Server, Microsoft SQL Server (MSSQL), and other platforms.

On the one hand it means if you are familiar with tsql then you can continue to write using it's supported syntax and

When you run TSQL code on the InterSystems IRIS platform, the code is compiled into the corresponding InterSystems SQL and ObjectScript code. ObjectScript is InterSystems object-oriented programming language. The compiled code is run on InterSystems IRIS and available for debugging purposes, if needed. This compilation step is fully automated and not a one-time migration task. The migration effort consists of loading the schema, data and TSQL code from the old environment to InterSystems IRIS. From then on you can continue to use and modify the TSQL application code, simply recompiling it after making any changes.

On the other hand if you use language =tsql you may not be able to use the extensions that InterSystems SQL supports such as implicit join syntax.

InterSystems SQL provides a special –> operator as a shorthand for getting values from a related table without the complexity of specifying explicit JOINs in certain common cases. This arrow syntax can be used instead of explicit join syntax, or in combination with explicit join syntax. Arrow syntax performs a left outer join.

While you can write a method or trigger using language=tsql I do not believe you can write a class query with language = tsql.  In this case you are kind of mixing apples and oranages.  

A class Query is a defined/callable query statement that can optionally be defined as a SQLProc.  When a Query is defined it can be defined as %Query in which case you write the Execute/Fetch/Close and within there I imagine you could use language = tsql but this is the less common approach and is a special case.  If Query is based on %SQLQuery you would simply provide an InterSystems SQL statement and not a tsql statement.

A class method can define language =tsql and this means the sql statements in the method are using the tsql dialect.  While a classmethod can also be defined as a SQLProc that can either act as a function returning a value or can use ReturnResultSets to 

Specifies whether this method returns result sets (so that ODBC and JDBC clients can retrieve them).