Introduction to Netezza Bank of America
Jan 04, 2016
Introduction to Netezza
Bank of America
Topics
• Netezza Architecture
• SQL Differences
• Stored Procedure Differences
2Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Netezza Architecture – TwinFin 12
3Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Topics
System Architecture
Snippet Processing Unit (SPU)
System Capacity
4Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Distribution in a shared-nothing architecture
Zone maps & data organization
Database Hardware Architecture Terminology
• Symmetrical Multi Process Architecture (SMP)
• Multiple processors sharing access to disk and memory
• Processors operate asynchronously
• Examples: Sybase, Sybase IQ, Oracle, SQL Server, DB2
• Massive Parallel Process Architecture (MPP)
5Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Massive Parallel Process Architecture (MPP)
• Multiple processors, each with separate, dedicated memory and disk
• No hardware is shared between processors
• Processors are slaved to a controller
• Processors operate synchronously
• Implemented many different ways
• Netezza approach is unique
• Examples: Netezza, Teradata, Green Plum, Aster, Vertica
Netezza Architecture
Host compiles SQL,develops execution
plan and sends code Host collects and returns result sets
Result sent to
user
Host
SQL command sent to host from user
6Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
snippets to SPUs based on plan
SPU executes code snippet. All SPUs execute same code synchronously.
SPUs pass data between each other as needed
returns result sets Host
Snippet Processing Unit (SPU)
Each SPU has a dedicated 1TB disk
Disk controlled by FPGA (Field Programmable Gate Array)
CPU loads query code into FPGA
7Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
CPU loads query code into FPGA
FPGA executes code using memory and cache
CPU performs additional processing to result set
CPU communicates to controller and other SPUs via NIC
System Capacity – TwinFin 12
• 12 S-Blades, each have 8 SPUs
• 4 SPUs are spares for fail over – 92 usable SPUs
• Each SPU has a 1TB disk drive
8Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Disk divided into 3 330GB partitions:
• Active data area
• Workspace
• Redundant copy of another SPU’s active data area
• Nominal 330GB capacity per SPU (uncompressed)
• Nominal 30TB capacity per system (uncompressed)
MPP Shared Nothing Architecture
• A SPU only has visibility to the data on its own disk.
• A SPU will broadcast data to other SPUs as needed (based on instructions from the controller) via the NIC.
• Broadcast data is received and held in memory or work area by
9Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Broadcast data is received and held in memory or work area by the SPU for use in resolving the query.
• Each SPU operates synchronously with all other SPUs. Each execute the same snippet at the same time.
• The controller’s execution plan ensures minimal data transfer between SPUs.
Data Distribution
• Data distribution to a SPU is defined by the DISTRIBUTE ON clause of the CREATE TABLE command
• Each table is distributed across all 92 SPUs
• You can distribute RANDOM or specify one or more columns in
10Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• You can distribute RANDOM or specify one or more columns in the table as a distribution key
• Controller uses distribution key information when deciding the execution plan
• Joins between tables with the same distribution key that are joined using that key will execute entirely on that SPU without the need to broadcast data (table co-location)
• Joins to tables with RANDOM distribution will require data broadcast. The smaller of the two sets is broadcast
Effects of Poor Data Distribution
• System Capacity
• The system is ‘full’ when any one SPU is full
• A badly skewed distribution of a large table may fill up a SPU prematurely
• Try to keep skew under 10% (5% off average) for very large tables
• System Performance
11Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• System Performance
• SQL code snippets are executed synchronously among all SPUs
• The total execution time for a snippet is the longest time among the SPUs
• The total execution time for a query is the sum of the longest times for each snippet
• Snippet execution time is proportionally related to the amount of data a SPU needs to process
• A query will only run as fast as the slowest SPU
Data Distribution – Co-Location
• Co-Location is the physical placement of related data across multiple tables onto the same SPU
• Allows a SPU to join co-located data without interaction with other SPUs
• Speeds queries by eliminating data broadcast steps
• Tables must have identical distribution key columns
12Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Tables must have identical distribution key columns
• RANDOM distribution will not co-locate
• Any difference in columns will result in a different, unrelated SPU assignment
• Join expression must include all distribution key columns
• Query compiler cannot assume co-location unless the join between two co-located tables includes equi-joins between all columns specified in the distribution key
• Ensure use by defining as few columns as possible as a distribution key
• Don’t bother with small tables
• No real advantage co-locating small tables
Data Distribution - Example
13Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• All 3 tables share ID_TS as a common key
• ID_TS is always used when joining these tables
• ID_TS is the appropriate column to use as a distribution key
CREATE TABLE TSVALUES(
ID_TSDATE INTEGER NOT NULL,ID_TS INTEGER NOT NULL,VALUE DOUBLE PRECISION NOT NULL,ID_SRC INTEGER NOT NULL,ID_STATUS INTEGER NOT NULL,TS_BEGIN TIMESTAMP NOT NULL,TS_END TIMESTAMP NOT NULL,SIGMA DOUBLE PRECISION NOT NULL
)DISTRIBUTE ON (ID_TS)ORGANIZE ON (TS_END, TS_BEGIN);
Data Distribution – Example
• Aginity Workbench
• Provides ability to redistribute existing tables
• Allows you to view a table’s distribution
• Distribution display for TSValues:
14Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Co-Located Queries
• The query will execute in parallel across all SPUs without data sharing because:
SELECT N.ID_TS, N.TICKER, N.NAME, V.VALUEFROM TSNAMES N, TSVALUES VWHERE N.ID_TS = V.ID_TSAND V.TS_BEGIN BETWEEN N.TS_BEGIN AND N.TS_ENDAND V.TS_END = ‘1/1/3000’;
15Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
sharing because:
• Both tables are distributed on the same key (ID_TS)
• ID_TS is used to join the tables
• Identical distribution key definitions across tables ensure rows with the same distribution key values in those tables reside on the same disk and SPU.
• Run times (493M row result set):
• Co-located: 22.1 seconds
• TSNames distributed on RANDOM: 74.3 seconds
Distribution Key Advice
• Keep distribution key as small as possible
• One column is best
• Ensure full use in joins
• Tables with some keys in common will not result in co-location
• Use a key that provides even distribution
16Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Use a key that provides even distribution
• Avoid skews > 10% in very large tables (> 5% nominal system capacity)
• Avoid skews > 5% in multiple same key tables with total size > 5% nominal system capacity
• Avoid skews > 20% in other tables
• May be necessary to add a column to the distribution key to reduce skew
• Don’t worry about skew in small tables (< 0.1 % nominal system capacity)
• Poor Distribution Keys
• Dates – cause query ‘hotspots’ when queries based on date range
• Low cardinality columns
• Never use a column with cardinality less than the number of SPUs
• Columns with cardinality < 20 x #of SPUs may produce very bad skews
Zone Maps
• A zone map records the high and low values of columns for rows in a data block on disk
• Netezza uses zone maps to skip data blocks that do not satisfy query predicates, speeding query execution
17Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Zone maps are automatically created for Integer, Date and Timestamp columns
• You can specify additional zone map columns in the ORGANIZE ON clause of the CREATE TABLE statement
ORGANIZE ON Clause
• Defines a Clustered Base Table
• Data is arranged (sorted) by the ORGANIZE ON columns during GROOM
• Improves effectiveness of zone maps
18Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Improves effectiveness of zone maps
• Specify up to 4 columns
• Allowable data types:
• INTEGER, DATE, TIMESTAMP
• CHAR, VARCHAR, NCHAR, NVARCHAR – Only first 8 bytes used
• NUMERIC – up to NUMERIC(18)
• FLOAT, DOUBLE
• BOOL
• TIME, TIME with Time zone
• INTERVAL
Using ORGANIZE ON
• Select columns most likely used in WHERE clause
• Zone map efficiency diminishes for 3rd and 4th columns
• Greater chance a wide range of values appearing in a data block
• Arrange lower cardinality or “clumpier” columns first
19Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Arrange lower cardinality or “clumpier” columns first
• Groups larger numbers of rows first, spread over more data blocks
• Increases likelihood subsequent columns spread over multiple blocks
• “Clumpy” – large portion of rows have few unique values
• GROOM table periodically
• Reorganizes data according to ORGANIZE ON specification
• Removes deleted rows
Using ORGANIZE ON - TSValues
• All current rows have ts_end = 1/1/3000 (very clumpy)
• Current rows are of greatest interest (most often selected)
• ts_begin commonly used in most queries
20Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Specify ts_end first. Clumps all current rows into contiguous blocks. CREATE TABLE TSVALUES
(ID_TSDATE INTEGER NOT NULL,ID_TS INTEGER NOT NULL,VALUE DOUBLE PRECISION NOT NULL,ID_SRC INTEGER NOT NULL,ID_STATUS INTEGER NOT NULL,TS_BEGIN TIMESTAMP NOT NULL,TS_END TIMESTAMP NOT NULL,SIGMA DOUBLE PRECISION NOT NULL
)DISTRIBUTE ON (ID_TS)ORGANIZE ON (TS_END, TS_BEGIN);
GROOM Command
• Organizes rows based on ORGANIZE ON clause
GROOM TABLE TSVALUESRECORDS ALL RECLAIM BACKUPSET DEFAULT;
21Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Organizes rows based on ORGANIZE ON clause
• Reclaims space from deleted rows
• Netezza performs updates by deleting old row and inserting new row
• Deleted rows consume space until table is GROOMed
• Fast – 15 minutes for 4.6 billion rows in TSValues
• Table remains available for query/update
SQL Differences – Netezza/Sybase
22Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Topics
• General Language Characteristics
• Identifiers
• Data Types
23Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Functions
• Command differences
General Language Characteristics
• Similar to Oracle syntax and interpretation
• Commands terminated with semi-colon (;)
• Terminator is required at all times
• Uses Oracle’s interpretation of NULL
24Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Uses Oracle’s interpretation of NULL
• A zero length string (‘’) is considered NULL
• Assigning ‘’ to a character column defined as NOT NULL will generate an error
Identifiers
• An identifier names a database object
• There are 2 types of Identifiers: regular and delimited
• Regular Identifier
• Is case insensitive
25Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Is case insensitive
• Must begin with a letter
• Contain letters, digits, underscores, dollar sign ($)
• Delimited Identifier
• Enclosed in double quotes
• Is case sensitive
• May include spaces, other special symbols and reserved words
• May begin with any allowable character
Data Types
• Does not support LOB types (CLOB, BLOB)
• Character types
• CHAR, VARCHAR: ASCII data, 64,000 maximum length
• NCHAR, NVARCHAR: Unicode data, 16,000 maximum length
26Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Exact Numeric Types
• BYTEINT 8 bit signed integer
• SMALLINT 16 bit signed integer
• INTEGER 32 bit signed integer
• BIGINT 64 bit signed integer
• NUMERIC(p,s), NUMERIC(p) Up to 38 digits precision (p), scale (s) from 0 to p
• NUMERIC Same as NUMERIC(18, 0)
Data Types (continued)
• Approximate Numeric Types
• FLOAT(p) precision can range from 1 to 15
• REAL same as FLOAT(6) – 4 bytes
• DOUBLE PRECISION same as FLOAT(15) – 8 bytes
• Logical Types
27Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Logical Types
• BOOLEAN (or BOOL) True/false value, 1 byte
• Temporal Types
• Time maintained to microsecond (6 decimal places)
• DATE Date with no time – 4 bytes
• TIME Time with no date – 8 bytes
• TIME WITH TIME ZONE Time with time zone information – 12 bytes
• TIMESTAMP Date and time – 8 bytes
• INTERVAL Time interval, non-standard implementation
• Maintained in seconds
• Ignores unit declarations, literals require explicit units
• Month assumed to be 30 days
Internal Data Types (pseudo-columns)
• ROWID
• Unique row identifier, assigned when row is inserted
• Not sequential in table
• Value range 100,000 - 9,223,372,036,854,775,807
• Will numbers ever repeat?
• If a table contains 2 billion rows and takes 3 minutes to copy
28Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• If a table contains 2 billion rows and takes 3 minutes to copy
• And if the table is copied repeatedly and continuously (add 2 billion rows every 3 minutes)
• It will take over 24,000 years to run out of numbers
• Netezza hopes you would have upgraded before then
• CREATEXID, DELETEXID
• Transaction ID that created and deleted the row.
• If DELETEXID > 0, row has been deleted
• You can not see deleted rows using SQL
• DATASLICEID
• Identifies the SPU holding the row
Operators
• Differences from Sybase
• Concatenation ||
• Not Equal <> or !=
• Null test ISNULL or IS NULLNOTNULL or IS NOT NULL
29Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Conditional Column Expression Functions
• CASE
Searched form:CASEWHEN <search-condition-1> THEN <result-1>WHEN <search-condition-2> THEN <result-2>...WHEN <search-condition-n> THEN <result-n>ELSE <default-result>
Value form:CASE <test-value>WHEN <comparand-value-1> THEN <result-1>WHEN <comparand-value-2> THEN <result-2>...WHEN <comparand-value-n> THEN <result-n>ELSE <default-result>
30Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• DECODE
• Same as Oracle DECODE
• decode(<expr>,<search1>,<result1>, …<search N>,<result N>,<default>)
ELSE <default-result>ENDSearch conditions can be arbitrarily complex and results can be expressions.
ELSE <default-result>ENDTest values, comparand values, and results can be expressions.
Commonly Used Functions
• Current time
• Use NOW() or CURRENT_TIMESTAMP
• TIMEOFDAY() returns a verbose date string. Example: ‘Mon Jan 24 16:12:05 2011 EST’
• Date Conversion
• Date literal is in MM/DD/YYYY format (default). Example: ‘1/1/3000’
31Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Date literal is in MM/DD/YYYY format (default). Example: ‘1/1/3000’
• TO_DATE(<text>, <template>) Converts string to DATE data typeTemplate describes format of date string.Example: TO_DATE(‘24 Jan 2011’, ‘DD Mon YYYY’)
• TO_TIMESTAMP (<text>, <template>) Converts string to TIMESTAMP data type
• TO_CHAR(date or timestamp, <template>) Converts date or timestamp to string
• See User Guide pp 3-26 – 3-28 for template information
SQL Differences - Update
Sybase Netezza
update tempRaw aSet Sigma = b.Sigma
from TSValues bwhere a.ID_TS = b.ID_TS
and a.ID_TSDate = b.ID_TSDate
update #tempRawSet a.Sigma = b.Sigma
from #tempRaw a,TSValues bwhere a.ID_TS = b.ID_TS
and a.ID_TSDate = b.ID_TSDate
32Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Special characters not used for temporary tables
• Table only referenced once
and a.ID_TSDate = b.ID_TSDateand now() < b.ts_endand a.ID_Status >= 1and a.ID_StatusSigma <= -1and a.isdelete = 0;
and a.ID_TSDate = b.ID_TSDateand getdate() < b.ts_endand a.ID_Status >= 1and a.ID_StatusSigma <= -1and IsDelete = 0
SQL Differences - DELETE
• DELETE may only reference one table
• DELETE FROM <table> WHERE <condition>
• Cannot directly convert:
DELETE #tempRaw FROM TSValues B , #tempRaw A
where A.ID_TS = B.ID_TS and A.ID_TSDate = B.ID_TSDate
33Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
where A.ID_TS = B.ID_TS and A.ID_TSDate = B.ID_TSDateand ( ( A.Value = B.Value and A.ID_Status = B.ID_Status ) or ( B.ID_Status >= 1 and A.ID_Status <= -1 ))AND ( abs(A.Sigma - B.Sigma) < 0.00000001 or A.ID_StatusSigma <= -1)and getdate() between B.ts_begin and B.ts_end AND A.Comments IS NULL
• Use UPDATE then DELETE:
Add a boolean column ‘deleteme’ to tempRaw table. Initialize to FALSE.
UPDATE tempRaw a set deleteme = TRUE
FROM TSValues B WHERE A.ID_TS = B.ID_TS AND A.ID_TSDate = B.ID_TSDateAND ( ( A.Value = B.Value and A.ID_Status = B.ID_Status ) OR ( B.ID_Status >= 1 and A.ID_Status <= -1 ))AND ( abs(A.Sigma - B.Sigma) < 0.00000001 OR A.ID_StatusSigma <= -1)AND NOW() BETWEEN B.ts_begin AND B.ts_end AND A.Comments IS NULL;
DELETE FROM tempRaw WHERE deleteme;
SQL Differences
• Materialized Views
• View may have only one source table
• Limited to 64 columns
• May include an ORDER BY clause
• Optimize zone maps
• Cannot include an ORGANIZE ON clause
34Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Cannot include an ORGANIZE ON clause
• Sequences
• You may specify data type (byteint, smallint, int, bigint)
• Default is bigint (64 bit signed integer)
• Blocks of numbers are allocated to each SPU during execution
• Sequential number assignment cannot be assured
Stored Procedure Differences
35Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
Topics
• General Differences
• CREATE PROCEDURE statement
• Iterating through rows (cursors)
36Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• Returning result sets
• System Variables & Special Functions
General Differences
• Statements are terminated with semi-colons (;)
• Variables are not prefixed with special characters
• := symbol used for variable assignment
a := 1;
37Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
a := 1;
• Syntax similar to Oracle PL/SQL
CREATE PROCEDURE
Sybase Netezza
CREATE PROCEDURE sp_TS_ProcessFillValues(@LoaderID int, @ID_SRC int)ASDECLARE @err_no int, @err_msg varchar(250), @CurrentTime datetime
<...procedure code ...>
CREATE PROCEDURE sp_TS_ProcessFillValues(INTEGER, INTEGER)RETURNS INTEGER EXECUTE AS CALLERLANGUAGE NZPLSQLASbegin_procDECLARE
38Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
<...procedure code ...>
GO
DECLARE LoaderID alias for $1;ID_SRC ALIAS FOR $2;CurrentTime datetime;EndDate datetime;StartDate datetime;
begin<... procedure code ...>end;end_proc;
Parameters are unnamed, referenced by position: $1, $2…
Alias may be used to name parameters
Procedure enclosed in a BEGIN_PROC -END_PROC block
Executable code enclosed in a BEGIN – END block following declarations.
Iterating Through Rows
• Netezza does not support cursor data type
• Use looping to iterate through a result set:
FOR record | row IN select_clause LOOP
statements
END LOOP;
39Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
END LOOP;
Alternate form:
FOR record | row IN EXECUTE text_expression LOOP
statements
END LOOP;
• Example
DECLARE myrec RECORD;mytotal DOUBLE PRECISION;
BEGINFOR myrec IN SELECT * FROM tsvalues LOOP
mytotal := mytotal + myrec.value;END LOOP;
END;
Returning Sets from Stored Procedures
• Use RETURNS REFTABLE (<table name>) clause
• <table name> refers to a table defined in the schema
• Used to define the format of the result set being returned (metadata only)
• Use REFTABLENAME to refer to the result set
• REFTABLENAME is a system provided variable
40Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• REFTABLENAME is a system provided variable
• System generates a unique table name
• Essentially a CREATE TEMP TABLE command
• Generate result set using EXECUTE IMMEDIATE
• Example:EXECUTE IMMEDIATE ‘INSERT INTO ‘ || REFTABLENAME || ‘ VALUES (…);’
• Use RETURN REFTABLE to return result set
System Variables and Special Functions
• SQLERRM
• Error message relating to last SQL error
• ROW_COUNT
• Number of rows affected by last executed SQL statement
41Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC
• QUOTE_LITERAL(<expression>)
• Used when building EXECUTE IMMEDIATE strings
• Converts the value of the expression into a properly formatted literal string
• Escapes any special characters as necessary.
• QUOTE_IDENT(<expression>)
• Used when building EXECUTE IMMEDIATE strings
• Converts expression into properly formatted database object name string
• Mainly used for delimited identifiers
Netezza Manuals
Full documentation can be found at:
\\crpnycnaf00n2\ReconDQ\Netezza\Manuals
42Ipsos, January 2007
Know Your Customers. Grow Your Business.
Ipsos Loyalty |Introduction to Netezza – Prepared for Bank of America © 2011 Aginity LLC