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:
CREATETABLE CUSTOMER
(
IDINTEGER PRIMARY KEYNOTNULL,
NAMEVARCHAR(100) NOTNULL,
PASSWORDVARCHAR(20) NOTNULL,
LAST_UPDATED TIMESTAMPDEFAULTCURRENT_TIMESTAMPNOTNULL,
REGISTRATION_DATE DATEDEFAULTCURRENT_DATENOTNULL
);notice functions CURRENT_TIMESTAMP and CURRENT_DATE are without parenthesis.
Create stored procedure/function that uses ObjectScript inside:
CREATEORREPLACEFUNCTION IRIS_VERSION()
RETURNSVARCHAR
LANGUAGE OBJECTSCRIPT
{
Return $ZV
}Call Stored procedure/function:
select IRIS_VERSION() as"Version"Call internal functions.
Return IRIS Version:
Select $versionReturn current namespace:
Select $namespaceRun 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.txtOpen 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
nice! Thanks ... I didn't know you could do things like Select $zversion :)
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
Try ":sql" instead of
d $system.SQL.Shell()
Holy cow @Chad Severtson, that's really cool! Do you know when that was added / where I can find more information? I don't see it in the docs on SQL Shell (https://docs.intersystems.com/iris20221/csp/docbook/Doc.View.cls?KEY=GS…).
Also, what is the ":" shorthand for and where else can it be used?
:py for embedded python
.png)
@Dmitry Maslennikov - thank you! Docs reference? Do you know what the ":" syntax is?
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 aliasesHTH
wow. Didn't know we have that! And aliases can be setup and transferred too? E.g. like a package (IPM)?
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
Another very cool aspect of the feature :) So it looks like this is just a built-in alias which ships with InterSystems IRIS.
holy cow #2 :-) so much precious info on this thread!
seriously! it's amazing how much knowledge there is to be gleaned within this community :)
Hey @Ben Spead,
You can find out more about the alias capability of iris session here: Using the Terminal Interactively | Using the Terminal | InterSystems IRIS Data Platform 2022.3
It's quite useful. You can even provide parameter substitution with $1, $2, etc.
Thank you @Jeffrey Drumm !!!
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!
For those that posted "I never knew that! Wow!" about the aliases: didn't you read this2 years ago?
😞
is there no quicker way to list all database tables? Like the postgresql \t?
in irissqlcli, you can use \dt or .tables
Would be nice to have
DO $SYSTEM.SQL.Schema.ImportDDL("c:\InterSystems\mysqlcode.txt",,"IRIS")or
irissqlcli iris://_SYSTEM:SYS@localhost:1972/USER < c:\InterSystems\mysqlcode.txtor
[SQL]USER>> run filename.sqlYou can also save the current query
[SQL]USER>>save filename.sqlNice work @Evgeny Shvarov those typical things you want to do quickly and never remember and have to read the docs :-)
Thanks, Luca!
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.