Results 1 to 2 of 2

Thread: How to find a specific value that exists in one of the 3 tables

  1. #1
    Join Date
    Oct 2023
    Posts
    1

    How to find a specific value that exists in one of the 3 tables

    I am trying to find a certain value that exists in 3 tables with identical columns and I am trying to find with this process

    DECLARE @SearchValue NVARCHAR(255)
    SET @SearchValue = 'YourSearchValue'

    -- Search in Table1
    SELECT 'Table1' AS TableName, YourColumnName AS MatchedValue
    FROM Table1
    WHERE YourColumnName = @SearchValue

    -- Search in Table2
    UNION ALL
    SELECT 'Table2' AS TableName, YourColumnName AS MatchedValue
    FROM Table2
    WHERE YourColumnName = @SearchValue

    -- Search in Table3
    UNION ALL
    SELECT 'Table3' AS TableName, YourColumnName AS MatchedValue
    FROM Table3
    WHERE YourColumnName = @SearchValue

    The problem is that in the search every time I get an error about the highlighted field even when I change the field type


    for example the value housenum=273 in the three tables house1,house2,house3

    if you have another way its ok

  2. #2
    Join Date
    Nov 2020
    Posts
    36
    I don't see a 'highlighted' field.

    First SELECT line defines field names and types. Not necessary to repeat alias names in subsequent SELECT but shouldn't hurt.
    What data type is YourColumnName in each table?

    SQL syntax looks fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •