如果存在,则SQL Server如果存在则为1,否则为2 [英] SQL Server IF EXISTS THEN 1 ELSE 2

查看:388
本文介绍了如果存在,则SQL Server如果存在则为1,否则为2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Sql Server2012.我有一个存储过程,它的一部分检查用户名是否在表中.如果是,则返回1,否则返回2.这是我的代码:

Using Sql Server 2012. I have a stored procedure and part of it checks if a username is in a table. If it is, return a 1, if not, return a 2. This is my code:

IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx') 1 else 2

但是,我仍然收到以下错误:

However, I keep receiving the below error:

'1'附近的语法不正确.

Incorrect syntax near '1'.

使用IF EXIST甚至有可能吗?

Is this even possible with an IF EXIST?

此致

迈克尔

推荐答案

如果您想这样做,那么这就是您要遵循的语法;

If you want to do it this way then this is the syntax you're after;

IF EXISTS (SELECT * FROM tblGLUserAccess WHERE GLUserName ='xxxxxxxx') 
BEGIN
   SELECT 1 
END
ELSE
BEGIN
    SELECT 2
END

您严格不需要BEGIN..END语句,但最好是从一开始就养成这种习惯.

You don't strictly need the BEGIN..END statements but it's probably best to get into that habit from the beginning.

这篇关于如果存在,则SQL Server如果存在则为1,否则为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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