2011-04-27 95 views
0

我在此查詢附近等不同的說法簡單的SQL查詢

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' 

這個語句工作正常

謝謝 王子

回答

1

像這樣的事情也許..

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

如果內部查詢返回多個行會發生什麼? – 2011-04-27 09:15:14

+0

比你有問題..但這不是問題 – redrobot 2011-04-27 09:40:29

+0

查詢返回相同的值,即使對於多個行值 – happysmile 2011-04-27 10:00:13

0

1.使用必須編寫不同@CNT變量
2.select只有第一行之前,因爲@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