if条件为什么不能加括号

yuekai 2004-11-21 08:26:18
这个SQL语句
if exists (select * from table1) select * from table2
可以执行但是加上括号就不可以用了
(if exists (select * from table1) select * from table2)

提示错误如下
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'if'.
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near ')'.
要怎么解决,我这个条件是放在where条件后面的需要添加这个括号。请问有什么办法解决?
...全文
318 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuekai 2004-11-21
  • 打赏
  • 举报
回复
to:Yang_(扬帆破浪) 没有看到你后面的回复,是这个原因阿~~~

谢谢了,我再想别的方法好了
yuekai 2004-11-21
  • 打赏
  • 举报
回复
整个SQL语句是这个样子
select * from table where 1=1
AND exists (if exists (select * from table1)select * from table1)
有个括号要加载if的外边,可是添加后就报错,如同上面写的
单独写if exists (select * from table1) select * from table2正确
这样写(if exists (select * from table1) select * from table2)就有错误
Yang_ 2004-11-21
  • 打赏
  • 举报
回复
我这个条件是放在where条件后面的需要添加这个括号。请问有什么办法解决?
where 后面不能跟if,我想,你对t-sql的语法还不是很清楚,建议多看书和联机帮助。
Andy__Huang 2004-11-21
  • 打赏
  • 举报
回复
//if exists (select * from table1) select * from table2

相當于這樣寫
if exists (select * from table1)
begin
select * from table2
end


而這句
//(if exists (select * from table1) select * from table2)
前后是什么句子﹖所以呢加了這個括號顯然不對
Yang_ 2004-11-21
  • 打赏
  • 举报
回复
你的括号不是加在if条件上,而是加在if语句上了

加在if条件上:

if (exists (select * from table1)) and (1=1)select * from table2

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧