日期:2014-05-17  浏览次数:20582 次

如果第一条语句有结果,则执行,否则执行第二条语句
如果第一条语句有结果,则执行,
如果第一条语句执行结果为空,则执行第二条语句
怎么写
SQL code
SELECT TOP 1 *
FROM 
(
  SELECT *
  FROM TEST 
  WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18'
) AS A
ORDER BY 数量 DESC

SELECT TOP 1 *
FROM TEST 
ORDER BY 日期 DESC


------解决方案--------------------
改为if @@rowcount=0
------解决方案--------------------
delete from table1
select * from Table1
IF @@Rowcount = 0
select 123
------解决方案--------------------
SQL code

 SELECT TOP 1
         *
 FROM    ( SELECT    *
           FROM      TEST
           WHERE     日期 BETWEEN '2012-7-16' AND '2012-7-18'
         ) AS A
 ORDER BY 数量 DESC
 IF @@ROWCOUNT = 0 
     BEGIN
         SELECT TOP 1
                 *
         FROM    TEST
         ORDER BY 日期 DESC
     END

------解决方案--------------------
如果第一条语句有结果,则执行 

就是两个select 都要呢 还是执行第一个 第二个不执行?
如果是继续执行那么参考2、3楼
如果是第二个不执行
SQL code
if exists(SELECT 1 FROM TEST  WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18')
SELECT TOP 1 *
FROM 
(
  SELECT *
  FROM TEST 
  WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18'
) AS A
ORDER BY 数量 DESC
else

SELECT TOP 1 *
FROM TEST 
ORDER BY 日期 DESC

------解决方案--------------------
SQL code

if exists(SELECT TOP 1 *
FROM 
(
  SELECT *
  FROM TEST 
  WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18'
) AS A
ORDER BY 数量 DESC)
SELECT TOP 1 *
FROM 
(
  SELECT *
  FROM TEST 
  WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18'
) AS A
ORDER BY 数量 DESC
else
SELECT TOP 1 *
FROM TEST 
ORDER BY 日期 DESC