SQL Server IF EXISTS THEN 1 ELSE 2(SQL Server IF EXISTS THEN 1 ELSE 2)
问题描述
使用 Sql Server 2012.我有一个存储过程,它的一部分检查用户名是否在表中.如果是,则返回 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 IF EXISTS THEN 1 ELSE 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server IF EXISTS THEN 1 ELSE 2


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01