Is there Switch case or GO TO in SQL?

  • Based on a condition, a set of queries will be executed. EX:

    Select Col1 = CASE LastColumn WHEN 'A' THEN 'First' .. But When 'A', then I need to execute a query. How do we do that?

    Is IF...ELSE the only means? Do we have any SWITCH CASE or GO TO as we have in C?

    Are there any other ways to achieve the same?

  • Well you can execute a query in the case... which is rarely the best way to go.

    What do you need to do exactly?

  • Thanks for the immediate reply.

    I would be checking for a value in a particular column and depending on the value.. i would be running update queries (different one for diff value .. for some values Bulk insert is used to load a small file and then update is run).

    If its not the best method, then I would be happy to go to IF ELSE.

  • You need a series of ifs for this.

    It's not like you were doing

    case Cola

    WHEN 1 then select from table2

    when 2 then select from view2

    And even if a case like that I'd still use derived tables or maybe outer applies.

    I would use a case like that to call different functions to do different maths or conversions or whatknot... not reaccess another tbale.

  • Only one table is involved in the entire operation (case/update/bulk insert)

  • Then post what you have. I can't generalize on this anymore.

    Merge might be what you are after in case you want to look that up.

  • I would have to sanitize the code before posting :(...even then it would be difficult, anyway will check on that..

    Thanks Ninja for reminding about the MERGE. 🙂

  • k, I can't help without seeing a little more what you need to do.

    P.S. Unless your data structure is the next mc2, there's usually no problem in posting snippets on the web 😉

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply