如果没有数据,则select语句检索默认值 [英] select statement retrieving default value if no data presnt
问题描述
大家好,
我选择陈述为
从员工中选择EmpName
它返回输出为
EmpName
SUNIL
MAHESH
AJAY
VIJAY
如果我这样做
从雇员那里选择EmpName =''SU ''
它返回
EmpName
空值
因为这个SU不存在于数据库中
i想要我的最终语句来检索一些默认值ex:0
或一些我可以在以后更改的文本。
所以我的
从员工中选择EmpName,其中EmpName =''SU''
应该返回
EmpName
0
i希望用更少的sql代码使其成功
因为我有一堆参数需要为不同的列检索不同的值作为默认值。
比ks,
SUNIL MALI。
Hi all,
I have select statement as
select EmpName from Employee
it returns output as
EmpName
SUNIL
MAHESH
AJAY
VIJAY
if i do it like
select EmpName from Employee where EmpName=''SU''
it returns
EmpName
with Empty values
because this SU is not present in database
i want my final statement to retrieve some default value ex: 0
or some text which i can change later.
so my
select EmpName from Employee where EmpName=''SU''
should return
EmpName
0
i want to make it succeed with less sql code
as i have bunch of parameter which needs to retrieve different values for different columns as default.
Thanks,
SUNIL MALI.
推荐答案
您好
尝试检查记录是否存在....
Hi
Try to Check Record Exists or not....
SELECT ISNULL(T.Col1,0) 'Col1',
ISNULL(T.Col2,0) 'Col2',
ISNULL(T.Col3,0) 'Col3'
FROM (
SELECT Col1, Col2, Col3
FROM TableName WHERE Condition
UNION ALL
SELECT NULL, NULL, NULL WHERE NOT EXISTS(SELECT 1 FROM TableName WHERE Condition)
) T
问候,
GVPrabu
Regards,
GVPrabu
这篇关于如果没有数据,则select语句检索默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!