Lamis Abouzina
Lamis Abouzina

Reputation: 409

Must declare the scalar variable "@Hlava" Must declare the scalar variable "@Vaha"

the code below should outer join 2 declared table but i keep getting the above error....

DECLARE @Hlava table 
([Datum/čas] datetime, [Filler L2 Hlava] int)

DECLARE @Vaha table 
([Datum/čas] datetime, [Filler L2 weight] int)

insert into @Hlava ([Datum/čas], [Filler L2 Hlava]) select [DateTime],Value
 from INSQL_LINK.Runtime.dbo.AnalogHistory 
 where tagname = 'Filler_L2_hlava' order by datetime desc


 insert into @Vaha ([Datum/čas], [Filler L2 weight]) select [DateTime],Value
 from INSQL_LINK.Runtime.dbo.AnalogHistory 
 where tagname = 'Filler_L2_weight' order by datetime desc

 SELECT [Datum/čas],[Filler L2 Hlava]
FROM @Hlava
FULL OUTER JOIN @Vaha ON @Hlava.[Datum/čas] = @Vaha.[Datum/čas];

 SELECT [Datum/čas],[Filler L2 Hlava]
FROM @Hlava
Union All
 SELECT [Datum/čas],[Filler L2 weight]
FROM @Vaha;

why do i need to declare a table as a scalar variable ??

Upvotes: 2

Views: 83

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269447

In SQL Server, a table alias cannot begin with @. Hence, your qualified column names don't make sense in a query such as this:

SELECT [Datum/čas],[Filler L2 Hlava]
FROM @Hlava FULL OUTER JOIN
     @Vaha
     ON @Hlava.[Datum/čas] = @Vaha.[Datum/čas];

So, just given them aliases:

SELECT [Datum/čas], [Filler L2 Hlava]
FROM @Hlava h FULL OUTER JOIN
     @Vaha v
     ON h.[Datum/čas] = v.[Datum/čas];

I would also recommend that you simplify your column names so they don't have to be escaped. For instance, Datum_čas instead of Datum/čas.

Upvotes: 1

Related Questions