大家 我在SQL SERVER中有这个查询
declare @nivel AS INT
set @nivel =3
SELECT * FROM TABLE1 AS A inner join TABLE2 as B ON A.COL1 = b.COL + @NIVEL
我想要连接一个变量
A.COL1 = b.COL3
没有转换''STRING
答案 0 :(得分:2)
试试这个
select *
from Table1 a
join Table2 b on a.Col1=case @nivel
when 1 then b.Col1
when 2 then b.Col2
when 3 then b.Col3
...
end
然而,这是非常糟糕的设计。您应该考虑重新设计Table2
以包含类似的内容
| ColNo | ColumnData
| 1 | Data of column 1
| 2 | Data of column 2
| 3 | Data of column 3
然后您的查询会更直接
select *
from Table1 a
join Table2 b
on a.Col1 = b.ColumnData
and b.ColNo = @nivel
答案 1 :(得分:0)
尝试:
declare @nivel AS INT
set @nivel =3
SELECT * FROM TABLE1 AS A
inner join TABLE2 as B ON A.COL1 = b.COL + CAST(@NIVEL AS CHAR)