Article
· Apr 4, 2023 2m read

InterSystems SQL Cheat Sheet

Hi developers!

As you know InterSystems IRIS besides globals, object, document and XML data-models also support relational where SQL is expected as a language to deal with the data.

And as in other relational DBMS InterSystems IRIS has its own dialect.

I start this post to support an SQL cheatsheet and invite you to share your favorites - I'll update the content upon incoming comments.

Here we go!

List all the tables in database:

SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

Credit to @Dmitry Maslennikov 

Create table with current date and current time as defaults:

CREATE TABLE CUSTOMER

(

    ID INTEGER PRIMARY KEY NOT NULL,

    NAME VARCHAR(100) NOT NULL,

    PASSWORD VARCHAR(20) NOT NULL,

    LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,

    REGISTRATION_DATE DATE DEFAULT CURRENT_DATE NOT NULL
);

notice functions CURRENT_TIMESTAMP and CURRENT_DATE are without parenthesis.

Create stored procedure/function that uses ObjectScript inside:

CREATE OR REPLACE FUNCTION IRIS_VERSION()

 RETURNS VARCHAR
 LANGUAGE OBJECTSCRIPT

 {

  Return $ZV

 }

Call Stored procedure/function:

select IRIS_VERSION() as "Version"

Call internal functions.

Return IRIS Version:

Select $version

Return current namespace:

Select $namespace

Run SQL from file (credit @Raj Singh ):

  DO $SYSTEM.SQL.Schema.ImportDDL("c:\InterSystems\mysqlcode.txt",,"IRIS")

Run SQL from file using python terminal(credit @Dmitry Maslennikov): 

irissqlcli iris://_SYSTEM:SYS@localhost:1972/USER < c:\InterSystems\mysqlcode.txt

Open SQL shell in terminal(credit @Chad Severtson):

USER>:sql

Open SQL shell in webterminal (credit @Nikita Savchenko ):

SAMPLES > /sql
SAMPLES:SQL > select top 5 * from Cinema.Film
ID Category Description Length PlayingNow Rating TicketsSold Title
1 1 A post-modern excursion into family dynamics and Thai cuisine. 130 1 PG-13 47000 Her Spicy Brothers
2 1 A gripping true story of honor and discovery 121 1 R 50000 Einstein's Geisha
3 1 A Jungian analysis of pirates and honor 101 1 PG 5000 A Kung Fu Hangman
4 1 A charming diorama about sibling rivalry 124 1 G 7000 Holy Cooking
5 2 An exciting diorama of struggle in Silicon Valley 100 1 PG 48000

The Low Calorie Guide to the Internet

SAMPLES: SQL > /sql
SAMPLES > write "COS!"
cos!

Add yours?

Discussion (26)7
Log in or sign up to continue

here it s in practice for those interested:

USER>d $system.SQL.Shell()
SQL Command Line Shell
----------------------------------------------------
 
The command prefix is currently set to: <<nothing>>.
Enter <command>, 'q' to quit, '?' for help.
[SQL]USER>>select $zversion
1.      select $zversion
 
Expression_1
IRIS for Windows (x86-64) 2022.1.2 (Build 574U) Fri Jan 13 2023 15:08:27 EST
 
1 Rows(s) Affected
statement prepare time(s)/globals/cmds/disk: 0.1884s/34,356/143,526/0ms
          execute time(s)/globals/cmds/disk: 0.0007s/0/394/0ms
                          cached query class: %sqlcq.USER.cls1

Just a note...

    LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, 

This field will only define the LAST_UPDATED field at INSERT, not for an UPDATE.  You probably want something like:

    LAST_UPDATED TIMESTAMP DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) NOT NULL, 

This will cause LAST_UPDATED to be set at INSERT and UPDATE to the current timestamp to microsend precision.