Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shutterbug99
Starting Member
2 Posts |
Posted - 2011-03-04 : 16:14:12
|
| I'm trying to declare a variable and use it in multiple SQL statements. (using the variable in the WHERE clause)Declare @Warrant VARCHAR(25)set @Warrant = '020111-3';SELECTdbo.apvendor.apvn_vend as [VendorNum],dbo.apvendor.apvn_name as [VendorName],dbo.apinvoih.apih_check_no as [CheckNum],dbo.apinvoih.apih_inv_net as [InvoiceNet],dbo.apinvoih.apih_inv_num as [InvoiceNum],dbo.apinvoih.apih_desc as [Description],dbo.apinvoih.apih_warrant as [Warrant],dbo.apinvoih.apih_wire_xfer,dbo.apinvoih.apih_status,dbo.apinvoih.apih_inv_date as [InvoiceDate]FROMdbo.apinvoihINNER JOIN dbo.apvendor ON dbo.apvendor.apvn_vend = dbo.apinvoih.apih_vendorWHERE(dbo.apinvoih.apih_warrant = @Warrant) AND (dbo.apinvoih.apih_wire_xfer = 'C');When I run the above code I get [Err] 42000 - [SQL Server]Must declare the scalar variable "@Warrant".What am I doing wrong?Thanks,Scott |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-04 : 16:24:24
|
| For me your code compiles without error.Are you selecting only the select portion before running ? |
 |
|
|
shutterbug99
Starting Member
2 Posts |
Posted - 2011-03-04 : 16:24:48
|
| Nevermind... it was the ';' |
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-04 : 16:35:52
|
quote: Originally posted by shutterbug99 Nevermind... it was the ';'
Are you sure it is ';' issue because afaik , ';' in sql never cause such issue.For me the below statement works :Declare @name VARCHAR(25)set @name ='syscolumns';SELECT* from sysobjects where name = @name |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-03-05 : 02:04:17
|
for me it seems that at first you were trying to compile/execute the above statement without selecting the first two lines ... However if you run the whole at once you will not face this issue (you can try and check it) . AFAIK too, It should not be the semicolon problem.CheersMIK |
 |
|
|
|
|
|
|
|