0

Using SQL Server

I want to check two values in two table.

I have two integer value (value1, value2)

Table1

ID Value

001 200
002 300
...

Table2

ID Value

001 300
002 400
...

Now I want to check whether value1 and value2 is matching with table1 value and table2 value

Tried Query

SELECT value from table1 Where  id = '" & textbox1.Text & "'

Condition

I want to check whether the value1 is matching with table1 or table2, then value2 is matching with table1 or table2. If it is matching then show the value otherwise null.

How to make a query.

Need Query help

Gopal
  • 11,712
  • 52
  • 154
  • 229
  • 1
    If `value1=300` and `value2=400`, what should the result of the query be? – Lieven Keersmaekers Oct 16 '11 at 09:32
  • @Lieven. I want to check two values in two table. How to make a query. First Value Check in table1 & table2, Second value check in table1 & table2 – Gopal Oct 16 '11 at 09:38
  • 1
    Still making sense of the original question but have you looked into joins? http://en.wikipedia.org/wiki/Join_(SQL) – Citricguy Oct 16 '11 at 09:43
  • 2
    @Gopal - you are restating your question. That does not answer my question or makes your question any clearer to me. – Lieven Keersmaekers Oct 16 '11 at 10:03
  • 1
    I think it would help if you posted a *complete* example illustrating what you want: table samples, sample values of `value1` and `value2`, and the expected result *based on the given table samples and sample values* (preferably the way you would like it to look like). – Andriy M Oct 16 '11 at 12:22

3 Answers3

1
SELECT
    MAX(T1.ID)
FROM
    (
    SELECT TOP 1 ID
    FROM table1 WHERE value = @Value1
    ORDER BY ID
    ) T1
    JOIN
    (
    SELECT TOP 1 ID
    FROM table2 WHERE value = @Value2
    ORDER BY ID
    ) T2 ON T1.ID = T2.ID

Notes:

For the last point, if you want any match

SELECT
   MAX(1)
FROM
    (
    SELECT TOP 1 ID
    FROM table1 WHERE value = @Value1
    ORDER BY ID
    ) T1
    CROSS JOIN
    (
    SELECT TOP 1 ID
    FROM table2 WHERE value = @Value2
    ORDER BY ID
    ) T2
Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
0

Usually, I add the query as a string without concatenating it..

SELECT * FROM ...etc WHERE id=@id AND networktag=@networktag

then deal with ID using the parameter on command (i.e. an instance of SqlCommand in C#) this is mostly using c# and I think easy to do in VB

Hope this helps

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
sys_debug
  • 3,883
  • 17
  • 67
  • 98
  • 1
    You are correct about using parameters to avoid SQL injection. But perhaps would be better with something more specific for the question. – gbn Oct 16 '11 at 09:53
0

If I understand you correctly, you could use a UNION to accomplish this.

SELECT  *
FROM    (
          SELECT ID, Value, 'Table1' AS Source
          FROM   Table1
          UNION ALL
          SELECT ID, Value, 'Table2'
          FROM   Table2
        ) u
WHERE   u.Value IN (@Value1, @Value2)
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146