0

I am transferring data from one table to another table using sql script. In my parent table I have certain columns which have constants declared. I want that, instead of transferring the constant's value, their name should be transferred instead. Is it possible using sql script?

PARENT TBL

ID | NAME | COLOR_ID
2  | test | 1

I just want color_id to be changed to Green via sql script.

RECEIVING TBL

ID | NAME | COLOR
2  | test | Green

Script

Insert in receiving_tbl(id, name, color) select (id, name, color_id) from parent_tbl
4
  • Where is the information that 1 = Green? Commented Jul 15, 2014 at 19:19
  • @Serpiton Yes, 1= Green, 2=Red etc. Commented Jul 15, 2014 at 19:20
  • 1
    Where the color name are defined? There is another table I think Commented Jul 15, 2014 at 19:22
  • @Serpiton This is the problem, they are application constants but i want them to be transferred as name to other table. Certainly, i would be required to declare them but how? Commented Jul 15, 2014 at 19:24

2 Answers 2

2

If the color name are not defined in the database the only option is to define a CASE to translate the ID to the names, something like

INSERT INTO receiving_tbl(id, name, color) 
SELECT id, name
     , CASE color_id WHEN 1 THEN 'Green'
                     WHEN 2 THEN 'Red'
                     ...
                     ELSE NULL
       END 
FROM parent_tbl
Sign up to request clarification or add additional context in comments.

Comments

2

If you have a table with your color ids and names stored, you can do this easily by including a join to your "name" table.

If, however, you do not have this information stored in a table, you can create this type of logic by using a case statement.

CASE  case_expression
   WHEN when_expression_1 THEN commands
   WHEN when_expression_2 THEN commands
   ...
   ELSE commands
END CASE;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.