如何检查SQL Server中的表中是否存在特定记录?

9 sql sql-server

我想检查表中是否存在id为10的记录users.

我试图exist在SQL中使用关键字,但我无法弄清楚用于exist检查记录是否存在的正确语法.

我想要下面的东西

If ( exist (select id * from table where Id = id ) ) 
{

}
Run Code Online (Sandbox Code Playgroud)

Kas*_*Kas 23

这很容易使用Exist关键字,我在下面写了正确的语法,检查出来

if exists (select * from  [dbo].[table] where id= [the id you want to check] ) 
select 'True'  
else 
select 'False' 
return
Run Code Online (Sandbox Code Playgroud)

  • 不应该是`[dbo].[table]`而不是`[dbo.table]`? (3认同)
  • 如果您在条件中需要多个语句,则需要使用*BEGIN*和*END*关键字 (2认同)