Sql server 在存储过程结果中添加默认列
我有这样一个存储过程:Sql server 在存储过程结果中添加默认列,sql-server,Sql Server,我有这样一个存储过程: 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 location Status ----------------------------------------- 571
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 location Status
-----------------------------------------
57173621345 deliverd 3
我的输出:
TBarcode Status
57173621345 3
我希望得到如下输出:
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 location Status
-----------------------------------------
57173621345 deliverd 3
我需要的位置列显示始终交付只。
那我该怎么做呢?。我需要使用函数还是有简单的方法?将SP中的select更改为
select t.TBarcode,
'delivered' location,
t.Status
from Transaction_tbl t
where t.TBarcode=@carid
从
您可以看到语法如下所示
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 ]
选择[ALL | DISTINCT]
[最高(表达)[百分比][有联系]]
::=
{
*
|{table_name | view_name | table_alias}.*
| {
[{表名称|视图名称|表别名}]
{column_name |$IDENTITY |$ROWGUID}
|udt|u列_名称[{.|:::}{{property_名称| field_名称}
|方法名称(参数[,…n])}]
|表情
[[AS]列\u别名]
}
|列别名=表达式
}[,…n]
其中,上述表达式定义为
是常量、函数、列名、常量的任意组合,
和由一个或多个运算符或子查询连接的函数
试试这个-
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