如何在 if exists 条件下执行子查询? [英] How to execute sub query in if exists condition?

查看:40
本文介绍了如何在 if exists 条件下执行子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

declare @qry varchar(100)
declare @cnt int
set @qry = ' where '

if exists( select * from  ARTICLE_MANAGE +@qry+ article_id=65)
BEGIN
select top 1* from  ARTICLE_MANAGE order by article_id desc
END
ELSE
BEGIN
select * from  ARTICLE_MANAGE order by article_id desc
END

这是查询.'@qry' 被我们传递给查询的内容改变了

This is the query. '@qry' is changed by what we passed to the query

推荐答案

DECLARE @qry VARCHAR(100);
DECLARE @cnt INT;
set @qry = ' where '
DECLARE @ExeQuery VARCHAR(MAX);
SET @ExeQuery='if exists( select * from  ARTICLE_MANAGE '+@qry+' article_id=65)
BEGIN
select top 1* from  ARTICLE_MANAGE order by article_id desc
END
ELSE
BEGIN
select * from  ARTICLE_MANAGE order by article_id desc
END'
 EXEC(@ExeQuery)

这篇关于如何在 if exists 条件下执行子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆