0

我有一个这样的存储过程:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode, t.Status 
from Transaction_tbl t 
 where t.TBarcode=@carid
end

我的输出:

TBarcode             Status
57173621345          3

我想得到这样的输出:

TBarcode        location          Status
-----------------------------------------
57173621345     deliverd         3

我需要位置列显示始终只交付。那么我该怎么做呢?我需要使用函数还是有什么简单的方法?

4

2 回答 2

2

将 SP 中的选择更改为类似

select  t.TBarcode, 
        'delivered' location,
        t.Status 
from    Transaction_tbl t 
where   t.TBarcode=@carid

SELECT 子句 (Transact-SQL)

可以看到语法如下

SELECT [ ALL | DISTINCT ]
[ TOP ( expression ) [ PERCENT ] [ WITH TIES ] ] 
<select_list> 
<select_list> ::= 
    { 
      * 
      | { table_name | view_name | table_alias }.* 
      | {
          [ { table_name | view_name | table_alias }. ]
               { column_name | $IDENTITY | $ROWGUID } 
          | udt_column_name [ { . | :: } { { property_name | field_name } 
            | method_name ( argument [ ,...n] ) } ]
          | expression
          [ [ AS ] column_alias ] 
         }
      | column_alias = expression 
    } [ ,...n ] 

其中上述表达式定义为

是常量、函数、由一个或多个运算符或子查询连接的列名、常量和函数的任意组合。

于 2013-07-25T05:45:10.957 回答
0

试试这个——

ALTER PROCEDURE [dbo].[fetchkey] 

@carid NVARCHAR(50) = NULL

AS BEGIN

     SELECT
           t.TBarcode
         , Location = 'delivered' 
         , t.[status]
     FROM dbo.Transaction_tbl t
     WHERE t.TBarcode = ISNULL(@carid, T.TBarcode)

END
于 2013-07-25T05:47:18.550 回答