SlideShare una empresa de Scribd logo
1 de 4
Anchored data type
An anchored data type is a data type that is defined to be the same as that of
another object. If the underlying object data type changes, the anchored data type
also changes.

    An anchored type defines a data type based on another SQL object such as a
         Column
         Global variable,
         SQL variable,
         SQL parameter
         Row of a table or view.

Features of the anchored data type
An anchored type defines a data type based on another SQL object such as a
column, global variable, SQL variable, SQL parameter, or the row of a table or view.

A data type defined using an anchored type definition maintains a dependency on
the object to which it is anchored. Any change in the data type of the anchor object
will impact the anchored data type. If anchored to the row of a table or view, the
anchored data type is ROW with the fields defined by the columns of the anchor
table or anchor view.

This data type is useful when declaring variables in cases where you require that the
variable have the same data type as another object, for example a column in a table,
but you do not know exactly what is the data type.

An anchored data type can be of the same type as one of:
      a row in a table
      a row in a view
      a cursor variable row definition
      a column in a table
      a column in a view
      a local variable, including a local cursor variable or row variable
      a global variable

Anchored data types can only be specified when declaring or creating one of the
following:
       a local variable in an SQL procedure, including a row variable
       a local variable in a compiled SQL function, including a row variable
       a routine parameter
       a user-defined cursor data type using the CREATE TYPE statement.
       It cannot be referenced in a DECLARE CURSOR statement.
       a function return data type
       a global variable
To define an anchored data type specify the ANCHOR DATA TYPE TO clause (or
the shorter form ANCHOR clause) to specify what the data type will be. If the
anchored data type is a row data type, the ANCHOR ROW OF clause, or one of its
synonyms, must be specified. These clauses are supported within the following
statements:
      DECLARE
      CREATE TYPE
      CREATE VARIABLE
      In this version, global variables can only be anchored to other global
      variables, a column in a table, or a column in a view.
Restrictions on the anchored data type
Review the restrictions on the use of the anchored data type before declaring
variables of this type or when troubleshooting problems related to their use.

The following restrictions apply to the use of anchored data types, including types
specified using the PL/SQL %TYPE attribute:
       Anchored data types are not supported in inline SQL functions.
       Anchored data types cannot reference nicknames or columns in nicknames.
       Anchored data types cannot reference typed tables, columns of typed tables,
       typed views, or columns of typed views.
       Anchored data types cannot reference declared temporary tables, or columns
       of declared temporary tables.
       Anchored data types cannot reference row definitions associated with a
       weakly typed cursor.
       Anchored data types cannot reference objects with a code page or collation
       that is different from the database code page or database collation.
Anchored data type variables
An anchored variable is a local variable or parameter with a data type that is an
anchored data type.

Anchored variables are supported in the following contexts:
     SQL procedures
         o In SQL procedures, parameters and local variables can be specified to
            be of an anchored data type.
     Compiled SQL functions
         o SQL functions created using the CREATE FUNCTION statement that
            specify the BEGIN clause instead of the BEGIN ATOMIC clause can
            include parameter or local variable specification that are of the
            anchored data type.
     Module variables
         o Anchored variables can be specified as published or unpublished
            variables defined within a module.
     Global variables
         o Global variables can be created of the anchored data type.
Declaring local variables of the anchored data type
Declaring local variables or parameters of the anchored data type is a task that you would
perform whenever it is necessary that the data type of the variable or parameter remain
consistent with the data type of the object to which it is anchored.
Before you begin

The object of the data type that the variable will be anchored to must be defined.

About this task

Procedure

   1. Formulate a DECLARE statement

           a. Specify the name of the variable.
           b. Specify the ANCHOR DATA TYPE TO clause.
           c. Specify the name of the object that is of the data type that the variable is to be
              anchored.

   2. Execute the DECLARE statement from a supported DB2® interface.

Results

If the DECLARE statement executes successfully, the variable is defined in the database
with the specified anchor data type.

Example
The following is an example of an anchored data type declaration in which a variable
named v1 is anchored to the data type of a column named c1 in a table named emp:

DECLARE v1 ANCHOR DATA TYPE TO emp.c1;

What to do next

Once the variable is defined it can be assigned a value, be referenced, or passed as a
parameter to routines.

Example: Variable declarations of anchored data types
Examples of anchored data type declarations can be useful references when declaring
variables.

The following is an example of a declaration of a variable named v1 that has the same data
type as the column name in table staff:
DECLARE v1 ANCHOR DATA TYPE TO staff.name;
The following is an example of a CREATE TYPE statement that defines a type
named empRow1 that is anchored to a row defined in a table namedemployee:
CREATE TYPE empRow1 AS ROW ANCHOR DATA TYPE TO ROW OF employee;
For variables declared of type empRow1, the field names are the same as the table column
names.

If the data type of the column name is VARCHAR(128), then the variable v1 will also be of
data type VARCHAR(128).

Examples: Anchored data type use in SQL routines
Examples of anchored data type use in SQL routines are useful to reference when creating
your own SQL routines.

The set of examples below demonstrate various features and uses of anchored data types in
SQL routines. The anchored data type features are demonstrated more so than the features
of the SQL routines that contain them.

The following is an example that demonstrates a declared variable that is anchored to the
data type of a column in a table:
CREATE TABLE tab1(col1 INT, col2 CHAR)@

INSERT INTO tab1 VALUES (1,2)@

INSERT INTO tab1 VALUES (3,4)@

CREATE TABLE tab2 (col1a INT, col2a CHAR)@

CREATE PROCEDURE p1()
BEGIN
  DECLARE var1 ANCHOR tab1.col1;
  SELECT col1 INTO var1 FROM tab1 WHERE col2 = 2;
  INSERT INTO tab2 VALUES (var1, 'a');
END@

Más contenido relacionado

La actualidad más candente

Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
Yogiji Creations
 

La actualidad más candente (20)

PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step Guide
 
Database users
Database usersDatabase users
Database users
 
Lecture2 oracle ppt
Lecture2 oracle pptLecture2 oracle ppt
Lecture2 oracle ppt
 
Relational databases
Relational databasesRelational databases
Relational databases
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Database fundamentals
Database fundamentalsDatabase fundamentals
Database fundamentals
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
 
Database System Architectures
Database System ArchitecturesDatabase System Architectures
Database System Architectures
 
Database Keys
Database KeysDatabase Keys
Database Keys
 
Oracle database introduction
Oracle database introductionOracle database introduction
Oracle database introduction
 
Oracle sql joins
Oracle sql joinsOracle sql joins
Oracle sql joins
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
 
Triggers and active database
Triggers and active databaseTriggers and active database
Triggers and active database
 
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
Table Partitioning in SQL Server: A Magic Solution for Better Performance? (P...
 

Destacado (15)

V.social campaign
V.social campaignV.social campaign
V.social campaign
 
More than yea or nay day 1 0
More than yea or nay day 1 0More than yea or nay day 1 0
More than yea or nay day 1 0
 
Taking The Plunge In Blogging 2
Taking The Plunge In Blogging 2Taking The Plunge In Blogging 2
Taking The Plunge In Blogging 2
 
Tech pp
Tech ppTech pp
Tech pp
 
More than yea or nay day 2 0
More than yea or nay day 2 0More than yea or nay day 2 0
More than yea or nay day 2 0
 
Assignment 1(web)
Assignment 1(web)Assignment 1(web)
Assignment 1(web)
 
Codd rules
Codd rulesCodd rules
Codd rules
 
Acampada
AcampadaAcampada
Acampada
 
Codd rules
Codd rulesCodd rules
Codd rules
 
Images
ImagesImages
Images
 
Presentation1
Presentation1Presentation1
Presentation1
 
Mírame a los ojos
Mírame a los ojosMírame a los ojos
Mírame a los ojos
 
Inwork-johdanto 3.12.2013
Inwork-johdanto 3.12.2013Inwork-johdanto 3.12.2013
Inwork-johdanto 3.12.2013
 
Multimedia software tools
Multimedia software toolsMultimedia software tools
Multimedia software tools
 
Selection sort
Selection sortSelection sort
Selection sort
 

Similar a Anchored data type

Anchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data typeAnchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data type
dhruv patel
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)
Nalina Kumari
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 

Similar a Anchored data type (20)

Unit1
Unit1Unit1
Unit1
 
Anchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data typeAnchor data type,cursor data type,array data type
Anchor data type,cursor data type,array data type
 
Pl sql
Pl sqlPl sql
Pl sql
 
Pl sql
Pl sqlPl sql
Pl sql
 
Pl sql
Pl sqlPl sql
Pl sql
 
Structure query language (sql)
Structure query language (sql)Structure query language (sql)
Structure query language (sql)
 
Oracle 11G Development Training noida Delhi NCR
Oracle 11G Development Training noida Delhi NCROracle 11G Development Training noida Delhi NCR
Oracle 11G Development Training noida Delhi NCR
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
 
Structure Query Language (SQL).pptx
Structure Query Language (SQL).pptxStructure Query Language (SQL).pptx
Structure Query Language (SQL).pptx
 
DBMS MODULE 3 NOTES ENGINEERING CSE .pdf
DBMS MODULE 3 NOTES ENGINEERING CSE .pdfDBMS MODULE 3 NOTES ENGINEERING CSE .pdf
DBMS MODULE 3 NOTES ENGINEERING CSE .pdf
 
Oracle11gdevtrainingindelhincr
Oracle11gdevtrainingindelhincrOracle11gdevtrainingindelhincr
Oracle11gdevtrainingindelhincr
 
chapter9-SQL.pptx
chapter9-SQL.pptxchapter9-SQL.pptx
chapter9-SQL.pptx
 
Oracle report from ppt
Oracle report from pptOracle report from ppt
Oracle report from ppt
 
Ankit
AnkitAnkit
Ankit
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Plsqlobj
PlsqlobjPlsqlobj
Plsqlobj
 
11i&r12 difference
11i&r12 difference11i&r12 difference
11i&r12 difference
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 

Más de Jay Patel

Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
Jay Patel
 
Assignment 2(web)
Assignment 2(web)Assignment 2(web)
Assignment 2(web)
Jay Patel
 
Assignment 2(web)
Assignment 2(web)Assignment 2(web)
Assignment 2(web)
Jay Patel
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
Jay Patel
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
Jay Patel
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
Jay Patel
 
Mutlimedia authoring tools
Mutlimedia authoring toolsMutlimedia authoring tools
Mutlimedia authoring tools
Jay Patel
 
Lecture6 text
Lecture6   textLecture6   text
Lecture6 text
Jay Patel
 
Lecture6 text
Lecture6   textLecture6   text
Lecture6 text
Jay Patel
 
Hypertext and hypermedia
Hypertext and hypermediaHypertext and hypermedia
Hypertext and hypermedia
Jay Patel
 
Multimedia software tools
Multimedia software toolsMultimedia software tools
Multimedia software tools
Jay Patel
 
Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02
Jay Patel
 
Introductionofdatastructure 110731092019-phpapp01
Introductionofdatastructure 110731092019-phpapp01Introductionofdatastructure 110731092019-phpapp01
Introductionofdatastructure 110731092019-phpapp01
Jay Patel
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
Jay Patel
 
Chapter19 multimedia-091006115642-phpapp02 (1)
Chapter19 multimedia-091006115642-phpapp02 (1)Chapter19 multimedia-091006115642-phpapp02 (1)
Chapter19 multimedia-091006115642-phpapp02 (1)
Jay Patel
 

Más de Jay Patel (20)

Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Assignment 2(web)
Assignment 2(web)Assignment 2(web)
Assignment 2(web)
 
Quiz(web)
Quiz(web)Quiz(web)
Quiz(web)
 
Assignment 2(web)
Assignment 2(web)Assignment 2(web)
Assignment 2(web)
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Inline function(oops)
Inline function(oops)Inline function(oops)
Inline function(oops)
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
 
Cursor
CursorCursor
Cursor
 
Mutlimedia authoring tools
Mutlimedia authoring toolsMutlimedia authoring tools
Mutlimedia authoring tools
 
Lecture6 text
Lecture6   textLecture6   text
Lecture6 text
 
Sound
SoundSound
Sound
 
Lecture6 text
Lecture6   textLecture6   text
Lecture6 text
 
Hypertext and hypermedia
Hypertext and hypermediaHypertext and hypermedia
Hypertext and hypermedia
 
Multimedia software tools
Multimedia software toolsMultimedia software tools
Multimedia software tools
 
Cursor
CursorCursor
Cursor
 
Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02Lecturestacks 110802115132-phpapp02
Lecturestacks 110802115132-phpapp02
 
Introductionofdatastructure 110731092019-phpapp01
Introductionofdatastructure 110731092019-phpapp01Introductionofdatastructure 110731092019-phpapp01
Introductionofdatastructure 110731092019-phpapp01
 
Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01Applicationsofstack 110805072322-phpapp01
Applicationsofstack 110805072322-phpapp01
 
Chapter19 multimedia-091006115642-phpapp02 (1)
Chapter19 multimedia-091006115642-phpapp02 (1)Chapter19 multimedia-091006115642-phpapp02 (1)
Chapter19 multimedia-091006115642-phpapp02 (1)
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Anchored data type

  • 1. Anchored data type An anchored data type is a data type that is defined to be the same as that of another object. If the underlying object data type changes, the anchored data type also changes.  An anchored type defines a data type based on another SQL object such as a  Column  Global variable,  SQL variable,  SQL parameter  Row of a table or view. Features of the anchored data type An anchored type defines a data type based on another SQL object such as a column, global variable, SQL variable, SQL parameter, or the row of a table or view. A data type defined using an anchored type definition maintains a dependency on the object to which it is anchored. Any change in the data type of the anchor object will impact the anchored data type. If anchored to the row of a table or view, the anchored data type is ROW with the fields defined by the columns of the anchor table or anchor view. This data type is useful when declaring variables in cases where you require that the variable have the same data type as another object, for example a column in a table, but you do not know exactly what is the data type. An anchored data type can be of the same type as one of: a row in a table a row in a view a cursor variable row definition a column in a table a column in a view a local variable, including a local cursor variable or row variable a global variable Anchored data types can only be specified when declaring or creating one of the following: a local variable in an SQL procedure, including a row variable a local variable in a compiled SQL function, including a row variable a routine parameter a user-defined cursor data type using the CREATE TYPE statement. It cannot be referenced in a DECLARE CURSOR statement. a function return data type a global variable To define an anchored data type specify the ANCHOR DATA TYPE TO clause (or the shorter form ANCHOR clause) to specify what the data type will be. If the anchored data type is a row data type, the ANCHOR ROW OF clause, or one of its
  • 2. synonyms, must be specified. These clauses are supported within the following statements: DECLARE CREATE TYPE CREATE VARIABLE In this version, global variables can only be anchored to other global variables, a column in a table, or a column in a view. Restrictions on the anchored data type Review the restrictions on the use of the anchored data type before declaring variables of this type or when troubleshooting problems related to their use. The following restrictions apply to the use of anchored data types, including types specified using the PL/SQL %TYPE attribute: Anchored data types are not supported in inline SQL functions. Anchored data types cannot reference nicknames or columns in nicknames. Anchored data types cannot reference typed tables, columns of typed tables, typed views, or columns of typed views. Anchored data types cannot reference declared temporary tables, or columns of declared temporary tables. Anchored data types cannot reference row definitions associated with a weakly typed cursor. Anchored data types cannot reference objects with a code page or collation that is different from the database code page or database collation. Anchored data type variables An anchored variable is a local variable or parameter with a data type that is an anchored data type. Anchored variables are supported in the following contexts: SQL procedures o In SQL procedures, parameters and local variables can be specified to be of an anchored data type. Compiled SQL functions o SQL functions created using the CREATE FUNCTION statement that specify the BEGIN clause instead of the BEGIN ATOMIC clause can include parameter or local variable specification that are of the anchored data type. Module variables o Anchored variables can be specified as published or unpublished variables defined within a module. Global variables o Global variables can be created of the anchored data type. Declaring local variables of the anchored data type Declaring local variables or parameters of the anchored data type is a task that you would perform whenever it is necessary that the data type of the variable or parameter remain consistent with the data type of the object to which it is anchored.
  • 3. Before you begin The object of the data type that the variable will be anchored to must be defined. About this task Procedure 1. Formulate a DECLARE statement a. Specify the name of the variable. b. Specify the ANCHOR DATA TYPE TO clause. c. Specify the name of the object that is of the data type that the variable is to be anchored. 2. Execute the DECLARE statement from a supported DB2® interface. Results If the DECLARE statement executes successfully, the variable is defined in the database with the specified anchor data type. Example The following is an example of an anchored data type declaration in which a variable named v1 is anchored to the data type of a column named c1 in a table named emp: DECLARE v1 ANCHOR DATA TYPE TO emp.c1; What to do next Once the variable is defined it can be assigned a value, be referenced, or passed as a parameter to routines. Example: Variable declarations of anchored data types Examples of anchored data type declarations can be useful references when declaring variables. The following is an example of a declaration of a variable named v1 that has the same data type as the column name in table staff: DECLARE v1 ANCHOR DATA TYPE TO staff.name; The following is an example of a CREATE TYPE statement that defines a type named empRow1 that is anchored to a row defined in a table namedemployee: CREATE TYPE empRow1 AS ROW ANCHOR DATA TYPE TO ROW OF employee; For variables declared of type empRow1, the field names are the same as the table column names. If the data type of the column name is VARCHAR(128), then the variable v1 will also be of data type VARCHAR(128). Examples: Anchored data type use in SQL routines
  • 4. Examples of anchored data type use in SQL routines are useful to reference when creating your own SQL routines. The set of examples below demonstrate various features and uses of anchored data types in SQL routines. The anchored data type features are demonstrated more so than the features of the SQL routines that contain them. The following is an example that demonstrates a declared variable that is anchored to the data type of a column in a table: CREATE TABLE tab1(col1 INT, col2 CHAR)@ INSERT INTO tab1 VALUES (1,2)@ INSERT INTO tab1 VALUES (3,4)@ CREATE TABLE tab2 (col1a INT, col2a CHAR)@ CREATE PROCEDURE p1() BEGIN DECLARE var1 ANCHOR tab1.col1; SELECT col1 INTO var1 FROM tab1 WHERE col2 = 2; INSERT INTO tab2 VALUES (var1, 'a'); END@