返回值列名

时间:2013-10-30 16:45:10

标签: sql sql-server tsql

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

然而,当它返回时,它会显示如下

(no column name)
     True

我的问题我需要“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

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:3)

取消SELECT / THEN中的ELSE阻止并使用AS为该列命名:

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

答案 2 :(得分: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]