如果第一条语句有结果,则执行,否则执行第二条语句
如果第一条语句有结果,则执行,
如果第一条语句执行结果为空,则执行第二条语句
怎么写
SELECT TOP 1 *FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18') AS AORDER BY 数量 DESCSELECT TOP 1 *FROM TEST ORDER BY 日期 DESC
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楼
如果是第二个不执行
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 AORDER BY 数量 DESCelseSELECT TOP 1 *FROM TEST ORDER BY 日期 DESC
[解决办法]
if exists(SELECT TOP 1 *FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18') AS AORDER BY 数量 DESC)SELECT TOP 1 *FROM ( SELECT * FROM TEST WHERE 日期 BETWEEN '2012-7-16'AND'2012-7-18') AS AORDER BY 数量 DESCelseSELECT TOP 1 *FROM TEST ORDER BY 日期 DESC