Setting a variable from a multi-table Select

  • Hi all,

    I have a variable to declare, and this works:

    Declare @CopyFromUser Varchar(10) =

    (Select Top 1 Op_Ref From [MyServername].[TemperTest_BATON].[dbo].Op Where Op_Forename+' '+Op_Surname = @CopyFromUserFull)

    However, the Op_Ref could be in any combination of 5 databases with thankfully identical schema, so I need to select against them, the following is as far as I've got but is still erroring out, could use a hand 🙂

    Declare @CopyFromUser Varchar(10) = (Select Top 1 * from

    (Select Op_Ref From [MyServername].[TemperTest_BATON].[dbo].Op Where Op_Forename = 'Alice'

    And Op_Surname = 'Cooper'

    Union

    Select Op_Ref From [MyServername].[TemperTest_SERGEI].[dbo].Op Where Op_Forename = 'Alice'

    And Op_Surname = 'Cooper'

    Union

    Select Op_Ref From [MyServername].[TemperTest_PRO].[dbo].Op Where Op_Forename = 'Alice'

    And Op_Surname = 'Cooper'

    Union

    Select Op_Ref From [MyServername].[TemperTest_C30].[dbo].Op Where Op_Forename = 'Alice'

    And Op_Surname = 'Cooper'

    Union

    Select Op_Ref From [MyServername].[TemperTest_ROp].[dbo].Op Where Op_Forename = 'Alice'

    And Op_Surname = 'Cooper'))

     

    TIA

    • This topic was modified 2 years, 3 months ago by  JaybeeSQL.
  • You will need an alias for the derived table containing the unions.

    Just add "AS a" after the final close bracket/parenthesis. (Ironically the TIA from your message would probably have sufficed).

    Without an alias, you will see this error message "Incorrect syntax near ')'."

     

     

    • This reply was modified 2 years, 3 months ago by  Ed B.
  • What is the error?

    I'd guess it's related to not having aliased the subquery -- e.g.,

    Declare @CopyFromUser Varchar(10) = (Select Top 1 * from
    (Select Op_Ref From [MyServername].[TemperTest_BATON].[dbo].Op Where Op_Forename = 'Alice'
    And Op_Surname = 'Cooper'
    Union
    Select Op_Ref From [MyServername].[TemperTest_SERGEI].[dbo].Op Where Op_Forename = 'Alice'
    And Op_Surname = 'Cooper'
    Union
    Select Op_Ref From [MyServername].[TemperTest_PRO].[dbo].Op Where Op_Forename = 'Alice'
    And Op_Surname = 'Cooper'
    Union
    Select Op_Ref From [MyServername].[TemperTest_C30].[dbo].Op Where Op_Forename = 'Alice'
    And Op_Surname = 'Cooper'
    Union
    Select Op_Ref From [MyServername].[TemperTest_ROp].[dbo].Op Where Op_Forename = 'Alice'
    And Op_Surname = 'Cooper') Ops
    )

     

  • The alias indeed, but after the penultimate closing bracket 🙂

    Cheers fellas

  • Without an order by, are you sure you are getting the proper value?  This looks like a bug waiting to happen.

    Can more than one of the 5 selects return a value?  If so, how are you determining the correct one to return?

     

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • JaybeeSQL wrote:

    The alias indeed, but after the penultimate closing bracket 🙂

    Cheers fellas

    Yes! I forgot that the query was inside brackets.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply