Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set a local variable to a default value

Status
Not open for further replies.

SteveMac32

Programmer
Jan 25, 2002
74
GB
Is it possible to set a local variable a default value, I ask this because the sample below is passed a value from a VB6 program if there is no value passed or a empty string is passed then the SQL statement should go down one path ‘Select * From TableA’ and if there is a string passed then enter the other path. I just run the simplified sample below and get:
Cannot assign a default value to a local variable, Must declare the variable '@VarABC' and Incorrect syntax near the keyword 'Else' I assume they all stem from the first error

Declare @VarABC char(4) = Null

If @VarABC Is Null
Select * From TableA
Else
Select * TableB

Thanks

Steve
 
You first need to declare the variable like this

declare @VarABC char(4)

to pass a value to a variable you need to use the 'SET' keyword...

set @VarABC = null

unfortuantely you can't declare and set the value for a variable in the same line in sql.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top