# Defining Variables

**URL:** <https://forums.toadworld.com/t/defining-variables/34887>\
**Category:** Toad for SQL Server\
**Created:** [January 21, 2016, 9:08am UTC](https://forums.toadworld.com/t/defining-variables/34887 "2016-01-21T09:08:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gavin73](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@gavin73](https://forums.toadworld.com/u/gavin73)\
**Post date:** [January 21, 2016, 9:08am UTC](https://forums.toadworld.com/t/defining-variables/34887/1 "2016-01-21T09:08:10Z")

</div>

Hi

I’m new to Toad and fairly new to SQL, I’d like to define my Variables at the top of my code, I’ve used Define, but I’m still getting a popup asking for my Variables again ?, additionally do I need to Undefine them ?, how long will Toad hold variables in Memory ?, is there a way to add a Watch to a variable in Toad, so that I can see what has been assigned ?, thanks for any help…

DEFINE StartDate = 20150101, EndDate = 20150131;  
SELECT \*  
FROM DAILY\_SALES  
WHERE PROCESSED\_DATE\_KEY BETWEEN &StartDate AND &EndDate;

Thanks

Gavin

---

<div class="post-metadata">

**Author:** ![s.bertolotti](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@s.bertolotti](https://forums.toadworld.com/u/s.bertolotti)\
**Post date:** [January 21, 2016, 9:55am UTC](https://forums.toadworld.com/t/defining-variables/34887/2 "2016-01-21T09:55:03Z")

</div>

Hi Gavin,

to define a variable in TOAD SS you can write :

SELECT \*

FROM TABLE\_NAME

WHERE FIELD\_NAME BETWEEN :StartDate AND :EndDate;

where TABLE\_NAME was a valid name of your table , and FIELD\_NAME is a valid name of the field of your table.

In your case :

SELECT \*

FROM DAILY\_SALES

WHERE PROCESSED\_DATE\_KEY BETWEEN :StartDate AND :EndDate;

At this point if you execute your statement TOAD prompt to you a dialog, where you can insert value for the variable - StartDate and EndDate.

But if you want to define variable in your T-SQL code you must use this syntax :

declare @StartDate datetime;

declare @EndDate datetime;

set @StartDate = ‘20160101’; – begin of January 2016

set @EndDate = ‘20160131’; – end of January 2016

SELECT \*

FROM DAILY\_SALES

WHERE PROCESSED\_DATE\_KEY BETWEEN @StartDate AND @EndDate;

I hope this can help you.

Best regards,

Sergio
