[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

If or Case Statement

Posted on 2005-04-26
2
Medium Priority
?
196 Views
Last Modified: 2010-03-19
Hi,
I'm trying to write a script that will do the following:
It will verfiy what code is in Table A and then it will write a pre determined number into Table B.

Example:
Table A Codes
HRT
HRA
OA

So if the code in Table A is HRT it will write 8 into Table B.

I don't know if I should use If or Case statement.

TIA,

Frank
0
Comment
Question by:fcobuzzi
2 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13867029
Just for clarification, if Table A has a code of HRT, how many records will be inserted in Table B?  Just 1 or depends how many times HRT exists in Table A?

If you just want a record inserted to Table B if there's a HRT in Table A, this should work:

INSERT INTO Table B
SELECT 8
WHERE EXISTS (SELECT 'X' FROM TableA WHERE Code = 'HRT')
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 13867305
I think your original suggestion is correct, use a CASE statement:


INSERT INTO b (col1, col2, codeNumber)
SELECT col1, col2,
    CASE code
        WHEN 'HRT' THEN 8
        WHEN 'HRA' THEN 12
        WHEN 'OA' THEN 16
        WHEN ... THEN ...
        ELSE 0 END
FROM a
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question