About 5,450,000 results
Open links in new tab
  1. Set Variable value in exists condition sql server

    Mar 11, 2014 · If you want it to set NULL if it does not exist, you may want to you the query like this: DECLARE @id bigint = 0 SET @id = (SELECT Id FROM Table1 WHERE column1 = '123') SELECT @id; -- Should select NULL if the row was not …

  2. Setting up a variable in a SQL IF-ELSE statement

    Mar 3, 2022 · What you could do in this scenario if using SQL Server 2017+ is use string_agg which would give you all ID values if there's duplicates: declare @id varchar(max); select @id=String_Agg(IntId, ',') from MyTable where [Name] = 'jon'; print isnull(@id, 'not an id');

  3. sql - Why is a variable declared inside IF statement created even …

    Apr 5, 2025 · SQL Server can't actually declare the variable in the second pass, because it was already declared in the first pass. So this is what SQL Server does: DECLARE statements without initialization (like DECLARE @A int;) are ignored.

  4. IF...ELSE (Transact-SQL) - SQL Server | Microsoft Learn

    Nov 22, 2024 · Imposes conditions on the execution of a Transact-SQL statement. The Transact-SQL statement that follows an IF keyword and its condition is executed if the condition is satisfied: the Boolean expression returns TRUE.

  5. SQL Server IF ELSE Statement By Examples - SQL Server Tutorial

    When the condition in the IF clause evaluates to FALSE and you want to execute another statement block, you can use the ELSE clause. The following illustrates the IF ELSE statement:

  6. SQL IF Statement for Conditional Logic - MSSQLTips.com - SQL

    Sep 12, 2022 · SQL ELSEIF and ELSE Condition Like many other programming languages, T-SQL can pair the IF keyword with ELSE IF and ELSE to more easily check multiple mutually exclusive scenarios. When combined, the first statement must be a single IF statement.

  7. T-SQL Programming Part 1 – Defining Variables, and IF…ELSE logic

    Oct 16, 2003 · This first article will discuss defining variables, and using the IF…ELSE logic. Local Variables. As with any programming language, T-SQL allows you to define and set variables. A variable holds a single piece of information, similar to a number or a character string. Variables can be used for a number of things.

  8. sql server - Check If Value Exists In Table - Database …

    Nov 28, 2016 · This is what I am needing done, if my Exec sp_executesql @SQL statement returns a value (meaning it exists in the table) I want to set the value returned to a variable. If it does not return a value, then I want to print a message saying it does not exist.

  9. SQL Server IF…ELSE Condition Statement: T-SQL Select Query

    Jun 28, 2024 · IF statement with No Else. You can use an IF statement in SQL without an ELSE part. Remember we stated that the ELSE part is optional. For example: DECLARE @Course_ID INT = 2 IF (@Course_ID <=2) Select * from Guru99 where Tutorial_ID = 1. It prints the following:

  10. Avoiding IF.. ELSE by using WHERE EXISTS - SQL Server Planet

    Jan 29, 2012 · When coming from a programming background, it’s natural to want to frame conditional operations within the familiar if.. else constructs. This happens in a lot of SQL code I have worked with (and I used to be a contributor also). The technique below is based on a common scenario.

Refresh