2013-10-30 98 views
1

下面的存儲過程按預期正常工作。如果存在「FleetBarcode」,則返回True,否則返回False。返回值列名

然而,當它返回它,它顯示爲以下

(no column name) 
    True 

我的問題是我需要「無列名」部分有一個定義的列名。嘗試了下面的方法,它給出了'真'字段的別名。

謝謝你的時間。

ALTER proc [dbo].[usp_getjobs] 

@barcode as nvarchar(20) 


as 
SELECT CASE WHEN EXISTS(
SELECT 
[FleetBarcode], 
[Deleted] 

FROM w_DeliveryItems 


WHERE FleetBarcode = @barcode 
AND Deleted != 1 
) 
THEN (SELECT 'True' 'Exist') 
ELSE (SELECT 'False' 'Exist') 


END 

回答

5

使用

ALTER PROC [dbo].[usp_getjobs] @barcode AS NVARCHAR(20) 
AS 
    SELECT CASE 
      WHEN EXISTS(SELECT [FleetBarcode], 
           [Deleted] 
         FROM w_DeliveryItems 
         WHERE FleetBarcode = @barcode 
           AND Deleted != 1) THEN 'True' 
      ELSE 'False' 
      END AS [Exist] 

別名需要去外SELECT

對於列別名,使用方括號比單引號更常見。

+0

完美,謝謝。我從來沒有注意到這一天晚些時候! –

3

獲得的THEN/ELSE塊,如果SELECT小號去掉,用AS給列了個名字:

SELECT CASE WHEN EXISTS(
... 
THEN 'True') 
ELSE 'False') 
END AS [Exist] --<-- add name here 
1
ALTER proc [dbo].[usp_getjobs] 

@barcode as nvarchar(20) 


as 
SELECT CASE WHEN EXISTS(
SELECT 
[FleetBarcode], 
[Deleted] 

FROM w_DeliveryItems 


WHERE FleetBarcode = @barcode 
AND Deleted != 1 
) 
THEN (SELECT 'True' 'Exist') 
ELSE (SELECT 'False' 'Exist') 


END AS [Your Column Name]