将默认值分配给变量SQL Express

时间:2014-11-25 16:20:54

标签: sql-server tsql

我正在尝试在SQL Express中创建一个变量,问题是我希望默认值来自查询而我无法弄明白。这就是我想要做的事情:

DECLARE @myVar int = select max(MYCOLUMN) from MYTABLE;

select (someColumn / @myVar) as newCol
from MYTABLE

2 个答案:

答案 0 :(得分:0)

DECLARE @myVar int = 0;
select @myVar =  max(MYCOLUMN) 
from MYTABLE;

select (someColumn / @myVar) as newCol
from MYTABLE

答案 1 :(得分:0)

您真正缺少的是围绕您尝试设置为默认值的括号。

DECLARE @myVar INT = (SELECT MAX(MYCOLUMN) FROM MYTABLE)

SELECT (someColumn / @myVar) AS newCol
FROM MYTABLE