1

I have 2 tables called table1 and table2. table1 has key, condition, level and conditionvalue columns and table2 has key and value columns.

table1 rows

key         condition  level     conditionVal
 1              >        error       10
 1              =        info         5    
 2              <        warning      5
 2              >        info         20 

I will get inputs(key and value) from user or other source and that values I need to insert into table2 based on the following condition.

if key is 1 and value is > 10 or value is equal to 5 then i need to insert. if key is 2 and value is > 20 or value is less than 5 then i need to insert.

How to acheive this using sql insert statement.

4
  • 2
    which DBMS? SQL Server, Oracle, MySQL,...? Commented Dec 15, 2016 at 13:04
  • 1
    Have a search for Dynamic SQL for your prefered DBMS Commented Dec 15, 2016 at 13:05
  • CASE in sql can help you? Commented Dec 15, 2016 at 13:06
  • I am sqlite3 for my C application Commented Dec 15, 2016 at 15:56

2 Answers 2

0

Try this:

IF EXISTS(
           SELECT 1
           WHERE (@Key=1 AND (@value>10 @value=5) ) OR (@Key=2 AND (@value>20 OR @value<5))
                                                                                            )

INSERT INTO Table2 (column1) VALUES (@values)
Sign up to request clarification or add additional context in comments.

Comments

0

try this :

INSERT INTO table2 (KEY, value)
  SELECT KEY , conditionVal 
    FROM table1 
    WHERE KEY IN
      (SELECT KEY
      FROM table1
      WHERE (KEY        =1
      AND (value        >10
      OR value          =5) )
      OR (KEY           =2
      AND (value        >20
      OR value          <5))
      AND 
    conditionVal IN
      (SELECT conditionVal
      FROM table1
      WHERE (KEY        =1
      AND (value        >10
      OR value          =5) )
      OR (KEY           =2
      AND (value        >20
      OR value          <5))

1 Comment

Table1 rows are dynamically added. So I need a dynamic solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.