简单的SQL查询

时间:2011-04-27 08:38:05

标签: sql-server-2005

我在此查询中收到错误,例如不同语句

附近的语法不正确
DECLARE  @CNT INT
SELECT @CNT= DISTINCT ISNULL(Employee.id,0)
FROM Employee 
WHERE EMPLOYEE.NAME='KIRAN'

语法错误

SELECT  DISTINCT ISNULL(Employee.id,0)
FROM Employee 
WHERE EMPLOYEE.NAME='KIRAN'

此声明可以正常使用

感谢 王子

2 个答案:

答案 0 :(得分:1)

这样的事可能......

DECLARE  @CNT INT
SET @CNT = (SELECT DISTINCT ISNULL(Employee.id,0)
FROM Employee 
WHERE EMPLOYEE.NAME='KIRAN')

答案 1 :(得分:0)

1.use必须在@CNT变量之前写明文
2.仅选择第一行,因为@cnt不是表变量,它是变量,类型是int

DECLARE  @CNT INT
SELECT top 1 @CNT= ISNULL(Employee.id,0)
FROM Employee 
WHERE EMPLOYEE.NAME='KIRAN'

你可以简单地测试一下

declare @CNT INT

SELECT top 1 @CNT= ISNULL(Employee.id,0)
FROM Employee 
WHERE EMPLOYEE.NAME='KIRAN'

select @CNT