Download presentation
Presentation is loading. Please wait.
Published byOwen Lewis Modified over 8 years ago
1
Caché SQL More than you think Ian Cargill Development Manager Dendrite Clinical Systems
2
%SQL Package - replaces %ResultSet Based primarily on %SQL.Statement and %SQL.StatementResult %SQL.Statement - implements an interface to prepare and execute dynamic SQL statements. %SQL.StatementResult - Every dynamic statement execution returns a result object.
3
Can set DisplayMode as argument to %New() More flexible Prepare of query (e.g. can pass multiline query as array!) %ExecDirect combines %Prepare and %Execute %Display method (for debug) %Print (with optional delimiter) - e.g. to produce CSV files
4
Full Monty - like %ResultSet set sql="SELECT * FROM Demog.Patient" set oST=##class(%SQL.Statement).%New() set sc=oST.%Prepare(sql) set rsSR = oST.%Execute() while rsSR.%Next() { … code here } kill oST,rsSR
5
Simple - %ExecDirect Does Prepare and Execute in single method Set sql="SELECT * FROM Demog.Patient" Set rsSR = ##class(%SQL.Statement).%ExecDirect(,sql) While rsSR.%Next() { … code here } Set rowId=rsSR.%ROWID Kill rsSR NOTE: sql is SECOND argument of %ExecDirect!
6
%ExecDirect %ExecDirect(ByRef oStatement, ByRef StatementText) Don't normally need oStatement
7
Advantages over %ResultSet Multiple Resultsets in a single invoked procedure Class encapsulates useful data as properties StatementResult class: –%ROWID –%ROWCOUNT –%ResultColumnCount –%SQLCODE
8
Which row did I just create? Set sql="INSERT INTO Demog.Patient(Name,Age) "_ " VALUES ('Fred',21)" Set rs=##class(%SQL.Statement).%ExecDirect(,sql) Set NewRowId=rs.%ROWID %ROWID
9
NULL and Empty Many programmers do not understand the difference. NULL is an unknown unknown … no information at all EMPTY is an known unknown … exists, but has no value
10
NULL and Empty Class data stored in ListBlocks $LB(field1,field2,field3) NULL stored as an empty List Item EMPTY stored as zero-length string: $Char(0) $LB("Fred","",$C(0))
11
False Emptys People create Empty, when they really meant NULL. E.g. set sql="UPDATE Table "_ "SET Field='"_variable_"'" What if variable = ""??
12
Use Parameterised Queries Set sql="SELECT * FROM Demog.Patient WHERE Surname=? AND Forename=?" Set rsSR = ##class(%SQL.Statement).%ExecDirect(,.sql,sn,fn)
13
Tune Tables Relative size more important than absolute size. Query Cost is to compare performance within query, not between queries. Selectivity used to determine filtering order. 1 is best, followed by smallest %s Run once, but on truly representative, mature database.
14
%SYS.PTools From 2010, (Portal from 2013) Provide profiling tools for SQL (& COS) –%SYS.PTools.SQLQuery General compile-time details of query –%SYS.PTools.SQLStats Runtime statistics $SYSTEM.SQL.SetSQLStats(flag) $SYSTEM.SQL.SetSQLStatsJob(flag)
15
%EXTERNAL / %INTERNAL %ODBCIN / %ODBCOUT It is possible to open resultsets in either logical or ODBC mode, but every field is in the same format. It is possible to override this on a field-by- field basis using SQL Functions.
16
Pseudo-columns %ID –RowId: Implicit in all tables. –Effectively alias for ID or IdKey field –ID seems to work just as well?? %vidfrom Caché 2011.1 ? –View ID: Implicit in all views –Gives row ID of view based on rows after views WHERE filtering
17
Pseudo-columns - %vid %vid useful for “chunking” data With subqueries (embedded SELECT), it gives “dynamic” row count SELECT %vid VID, * FROM (SELECT * FROM Demog.Patients) WHERE %vid BETWEEN 1 and 49
18
SQL Predicate Conditions 1 %STARTSWITH –%STARTSWITH 'text' –More efficient than LIKE ‘text%’ %INLIST –Especially for testing fields that are ListBlocks
19
SQL Predicate Conditions 2 %PATTERN – literals, character type codes, and wildcards –Like the ? operator in ObjectScript –WHERE Home_State %PATTERN '1U1"C"' %MATCHES - literals, wildcards, and ranges –WHERE Name NOT %MATCHES '*[AaEe]*‘ –WHERE Name %MATCHES '?????,[A-D]*'
20
SQL Predicate Conditions 3 %CONTAINS & %CONTAINSTERM –Only use with %Library.Text fields –Match word-aware strings with complex text analysis –comparison operation is word-aware –also matches stem forms –E.g. “set” also matches “sets” and “setting” %SIMILARITY - related
21
SQL Predicate Conditions 4 %PLUS, %MINUS vrs %EXACT (after 2010 ??) –How to control sorting based on numeric values %EXACT%PLUS 2222222apple 3333333banana 44444440111111 2222222 apple3333333 banana4444444
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.