Escrito por

Senior Startups and Community Programs Manager at InterSystems Corporation
Artículo Evgeny Shvarov · abr 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?

Comments

Ben Spead · abr 4, 2023

nice!  Thanks ... I didn't know you could do things like Select $zversion :)

0
Ben Spead  abr 4, 2023 to Ben Spead

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
0
Chad Severtson  abr 6, 2023 to Ben Spead

Try ":sql" instead of 

d $system.SQL.Shell()
0
Robert Cemper  abr 8, 2023 to Ben Spead
USER>:?
 :<number>    Recall command # <number>
 :?           Display help
 :py          Do $system.Python.Shell()
 :mdx         Do $system.DeepSee.Shell()
 :sql         Do $system.SQL.Shell()
 :tsql        Do $system.SQL.TSQLShell()
 :alias       Create/display aliases
 :clear       Clear history buffer
 :history     Display command history
 :unalias     Remove aliases

HTH

0
Evgeny Shvarov  abr 9, 2023 to Robert Cemper

wow. Didn't know we have that! And aliases can be setup and transferred too? E.g. like a package (IPM)?

0
Robert Cemper  abr 9, 2023 to Evgeny Shvarov

from docs:
If you are using a UNIX® or Linux system, you can provide a list of alias definitions which the Terminal will set automatically at the start of every session. Define these aliases (one per line) in a file named .iris_init in the home directory.
never tried this. I rarely work on *X

0
Ben Spead  abr 10, 2023 to Robert Cemper

Another very cool aspect of the feature :)  So it looks like this is just a built-in alias which ships with InterSystems IRIS.  

0
Luca Ravazzolo  abr 11, 2023 to Robert Cemper

holy cow #2 :-) so much precious info on this thread!

0
Ben Spead  abr 11, 2023 to Luca Ravazzolo

seriously!  it's amazing how much knowledge there is to be gleaned within this community :)

0
Chad Severtson  abr 12, 2023 to Ben Spead

It has been around for a few years.
Personally, I hesitate to use the alias capability lest I find myself in an environment without them! 

0
Joel Solon  ene 8, 2024 to Ben Spead

For those that posted "I never knew that! Wow!" about the aliases: didn't you read this 2 years ago?
😞

0
Raj Singh · abr 5, 2023

is there no quicker way to list all database tables? Like the postgresql \t?

0
Dmitry Maslennikov  abr 7, 2023 to Raj Singh

or 

irissqlcli iris://_SYSTEM:SYS@localhost:1972/USER < c:\InterSystems\mysqlcode.txt
0
Jeffrey Drumm  abr 9, 2023 to Dmitry Maslennikov

or

[SQL]USER>> run filename.sql

You can also save the current query

[SQL]USER>>save filename.sql
0
Luca Ravazzolo · abr 11, 2023

Nice work @Evgeny Shvarov those typical things you want to do quickly and never remember and have to read the docs :-)

0
David Van De Griek · abr 14, 2023

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.

0