2013-07-25 109 views
0

我有一個存儲過程是這樣的:添加默認列結果

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

我的輸出:

TBarcode    Status 
57173621345   3 

我希望得到這樣的輸出:

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

我需要位置欄來顯示始終只顯示。 那麼我該如何做到這一點?我需要使用一個函數還是有任何簡單的方法?

回答

2

改變選擇的SP到像

​​

SELECT Clause (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 ] 

如果上述表達式定義爲

是常量,函數,列名稱,常量, 以及由運算符或運算符或子查詢連接的函數的任意組合。

+0

thanks..working罰款 – user2603688

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