1

下面的存储过程按预期正常工作。如果“FleetBarcode”存在则返回 True,否则返回 False。

但是,当它返回它时,它显示如下

(no column name)
     True

我的问题是我需要“无列名”部分来定义列名。到目前为止,尝试了以下方法,该方法为“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
4

3 回答 3

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

同样对于列别名,使用方括号比使用单引号更常见。

于 2013-10-30T16:47:02.467 回答
3

如果/块SELECT中的 s删除并使用给列命名:THENELSEAS

SELECT CASE WHEN EXISTS(
...
THEN 'True')
ELSE 'False')
END AS [Exist]  --<-- add name here
于 2013-10-30T16:47:11.750 回答
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]
于 2013-10-30T16:47:29.003 回答