Variable Declarations in Teradata Stored Procedures

In Teradata Stored Procedures, variable declarations are limited to the surrounding compound statement (BEGIN…END) and all subsequent compound statements.

An integer variable, x, is declared in the outer and inner compound statements.

The outer compound statement can only access its own declaration of variable x. In contrast, the inner compound statement has access to both the outer x and its own declaration of variable x.

REPLACE   PROCEDURE  SAMPLE_SP ( OUT  n INTEGER )BEGIN  DECLARE x INTEGER DEFAULT 99;BEGIN   DECLARE x INTEGER DEFAULT 100;SET  n = x;END;END;CALL  SAMPLE_SP(x);

This Teradata stored procedure will return the value of 100 to the client.

You must utilize labels to retrieve the variable x declared in the outer compound statement.

REPLACE  PROCEDURE SAMPLE_SP ( OUT  n INTEGER )l1: BEGIN  DECLARE x INTEGER DEFAULT 99;BEGIN    DECLARE x INTEGER DEFAULT 100;SET  n = l1.x;END;END l1;CALL SAMPLE_SP(x);

Prefacing the variable with the outer compound statement label grants access to variable x declared therein. Consequently, the client (BTEQ, SQL Assistant) receives a value of 99 in the second example.

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

2 thoughts on “Variable Declarations in Teradata Stored Procedures”

  1. Hi Roland, Is there any alternative in Teradata to use variables apart from stored procedures, can we declare variables in normal sql scripts.

    Reply

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.