select case Count(*) when 0 then '1' else ys end as ys from yfmx where ypid=23423
select case when (select count(*) from from yfmx where ypid=23423) is null then 1 else ys end from yfmx
看看这个行不?如有错误还请原谅(没有经过测试) select case when (select count(*) from yfmx where ypid=23423)>0 then 1 else ys end from yfmx where ypid=23423
select case when max(ye) is null then 0 else max(ye) end from (select top 1 ye from yfmx where ypid=2 and ksbh=050104 order by id desc) a 我也写了一条,可以返回需要的值,因为此返回值将作为一个值插入一条数据中,当这句写在sql之中时就错了:服务器: 消息 156,级别 15,状态 1,行 4 在关键字 'select' 附近有语法错误。
SELECT ISNULL( (SELECT TOP 1 LTRIM(ys) from yfmx where ypid=23423 ORDER BY YS DESC),'1')楼主YS如果是INT型,后面的'1'可改为1 前面的LTRIM(YS)不用加LTRIM ,而且你要取最后一条,得按什么排序,在ORDER BY 后面更改
DECLARE @yfmx TABLE(YS INT,YPID INT) INSERT @yfmx SELECT 2,23423 UNION ALL SELECT 3,23423 SELECT ISNULL( (SELECT TOP 1 ys from @yfmx where ypid=23423 ORDER BY YS DESC),1) (所影响的行数为 2 行) ----------- 3(所影响的行数为 1 行)
select case when (select count(*) from from yfmx where ypid=23423) is null then 1 else ys end from yfmx
select
case when (select count(*) from yfmx where ypid=23423)>0
then
1
else
ys
end
from yfmx where ypid=23423
我也写了一条,可以返回需要的值,因为此返回值将作为一个值插入一条数据中,当这句写在sql之中时就错了:服务器: 消息 156,级别 15,状态 1,行 4
在关键字 'select' 附近有语法错误。
(SELECT TOP 1 LTRIM(ys) from yfmx where ypid=23423 ORDER BY YS DESC),'1')楼主YS如果是INT型,后面的'1'可改为1
前面的LTRIM(YS)不用加LTRIM
,而且你要取最后一条,得按什么排序,在ORDER BY 后面更改
INSERT @yfmx
SELECT 2,23423
UNION ALL
SELECT 3,23423
SELECT ISNULL(
(SELECT TOP 1 ys from @yfmx where ypid=23423 ORDER BY YS DESC),1)
(所影响的行数为 2 行)
-----------
3(所影响的行数为 1 行)