Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Switch like logic in T-SQL

Tags:

tsql

This looks like a noob T-SQL question but I want do switch like logic in a stored procedure and I was thinking that using a CASE would be the way to do this with something like

 SELECT CASE @Type         WHEN 1 THEN             INSERT INTO dbo.Credit (                 CompanyName,                 PhoneNumber,                 City,                 State             ) VALUES (                  @CompanyName,                 @PhoneNumber,                 @City,                 @State)          WHEN 2 THEN               INSERT INTO dbo.Debit (                 CompanyName,                 PhoneNumber,                 City,                 State             ) VALUES (                  @CompanyName,                 @PhoneNumber,                 @City,                 @State)          WHEN 3 THEN               --ETC      END     

but I keep getting errors, is there just a systax error or is what I'm doing out to lunch?

like image 361
Bob The Janitor Avatar asked Jul 30 '09 16:07

Bob The Janitor


People also ask

Can we use switch statement in SQL?

@RamSingh - there's no switch statement in the SQL language. As others have indicated, you can use a CASE expression, but it has to compute and return a scalar value.

Can you do logic in SQL?

SQL Server has a unique capability of allowing you to execute real-time programmatic logic on the values within your query. Based on those logical evaluations, you can generate various values as part of the returned data set.

What is switch in SQL?

Switch returns a Null value if: None of the expressions is True. The first True expression has a corresponding value that is Null.


2 Answers

You need to use If/Else If structure, like this:

If @Type = 1     Begin         INSERT INTO dbo.Credit (                 CompanyName,                 PhoneNumber,                 City,                 State         ) VALUES (                  @CompanyName,                 @PhoneNumber,                 @City,                 @State)      End Else If @Type = 2     Begin         INSERT INTO dbo.Debit (                 CompanyName,                 PhoneNumber,                 City,                 State         ) VALUES (                  @CompanyName,                 @PhoneNumber,                 @City,                 @State)      End Else If @Type = 3     Begin         --ETC     END 
like image 157
George Mastros Avatar answered Oct 15 '22 02:10

George Mastros


You can do something like these:

SET @SQL = CASE @Type             WHEN 1 THEN                     @SQL1             WHEN 2 THEN                       @SQL2             ELSE                      @SQL3      END  EXEC(@SQL) 

UPDATE 9/18/2016

NOTE: This is a easy and quick solution, but keep in mind this is a not a long term solution to be implemented in production environments. I agree with @Jon Galloway: "I don't think CASE is the right fit here."

Another more professional implementation will be to create 3 different stored procedures that do their own job (Single Responsibility Principle), something like this:

If @Type = 1     EXEC InsertCredit @CompanyName, @PhoneNumber, @City, @State Else If @Type = 2     EXEC InsertDebit @CompanyName, @PhoneNumber, @City, @State Else If @Type = 3     EXEC OtherInsert @CompanyName, @PhoneNumber, @City, @State 
like image 20
Jaider Avatar answered Oct 15 '22 02:10

Jaider