Select from 2 databases

  • I am using asp.NET code, and want to SELECT from two different databases in the same SELECT statement.  Both databases are on the same server.

    Would this be better in a stored procedure?

    Current code follows. control table is moving to different database.

    "SELECT DISTINCT cr_cases.county_code, " "cr_cases.case_number, cr_offense.charged_offn_code, cr_offense.date_created "

    "FROM cr_offense

    INNER JOIN control ON cr_offense.county_code = control.county "

    "AND cr_offense.charged_offn_code = control.offense "

    "AND active = 1

    INNER JOIN " "cr_cases ON cr_offense.county_code = cr_cases.county_code  "AND cr_offense.case_number = cr_cases.case_number "

    "WHERE

    (cr_cases.case_number LIKE N'2003%'

    OR cr_cases.case_number LIKE N'2004%') "

    "AND (cr_cases.isnew = 1)

    ORDER BY cr_cases.case_number"

    Thanks - Bil

  • It would always be beneficial to use stored procedures.

    In case when you have two databases A and B residing on the same server.

    Lets consider we have Employee table on Database A and

    Salary Table on Database B (both the tables have empid)

    We can perform joins on the two databases as follows

    select a.name,b.salary from A.dbo.Employee a

    inner join

    B.dbo.Salary b

    on a.empid=b.empid.

    I hope the above solution helps you in your problem


    Kindest Regards,

    Sureshkumar Ramakrishnan

Viewing 2 posts - 1 through 1 (of 1 total)

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