Check if record exists
-
Hello, I created the following SQL script to check if a record exists: IF (EXISTS (SELECT LevelName FROM dbo.by27_Levels WHERE LOWER(@LevelName) = LOWER(LevelName))) Return (1) ELSE Return (0) And I also found in a web page another solution: IF EXISTS(SELECT 1 FROM TABLENAME WHERE LevelName=@LevelName) SELECT 1 ELSE SELECT 0 - Which approach should I use? - Why "SELECT 1 FROM"? - And when should I use SELECT or RETURN? All I need is to know if the record exists ... nothing else. I will use this procedure on an ASP.NET 2.0 / C# web site. I am not sure if this important but anyway ... Thank You, Miguel
-
Hello, I created the following SQL script to check if a record exists: IF (EXISTS (SELECT LevelName FROM dbo.by27_Levels WHERE LOWER(@LevelName) = LOWER(LevelName))) Return (1) ELSE Return (0) And I also found in a web page another solution: IF EXISTS(SELECT 1 FROM TABLENAME WHERE LevelName=@LevelName) SELECT 1 ELSE SELECT 0 - Which approach should I use? - Why "SELECT 1 FROM"? - And when should I use SELECT or RETURN? All I need is to know if the record exists ... nothing else. I will use this procedure on an ASP.NET 2.0 / C# web site. I am not sure if this important but anyway ... Thank You, Miguel
Second approach; Select 1 is used to minimize the processor workload (there is no need to return the fields) you could also consider: SELECT count(*) AS Results FROM dbo.by27_Levels WHERE LOWER(@LevelName) = LOWER(LevelName)