Forum Discussion

A819A1L's avatar
A819A1L
Brass Contributor
Apr 08, 2021
Solved

MSSQL - Can't create view with data from two databases in one server

Hello    I'm trying to create a view using data from two databases and 3 tables in total but it isn't working.     The two databases are thardata and Gardners The three tables are thardata.dbo.W...
  • olafhelper's avatar
    Apr 09, 2021

    Hello A819A1L ,

     

    1. Use alias for objects for better readability, that reduce also failures

    2. Don't use GUI/Wizards, they are not very reliable. Always use plain T-SQL

    3. Set object name with reserved words and special characters in bracket

    4. In the first JOIN => ON clause is the column name "OperationCode" missing (see 1.)

     

    Please try if this works for you:

     

     

     

    CREATE VIEW dbo.AD_LiveData
    AS
        SELECT WO.OperatorName
              ,WO.OperationCode
              ,WO.OperationName
              ,WO.DateTimeStarted
              ,WO.JobNo
              ,WO.TerminalID
              ,WO.InterruptedDateTime
              ,MAL.[Resource]
              ,CI.StatusType
        FROM thardata.dbo.WorkingOperations AS WO
             INNER JOIN 
             thardata.dbo.MachineAndLabour AS MAL
                 ON WO.OperationCode = MAL.Code
             INNER JOIN 
             Gardners.dbo.[CurrentIDCActivities$] AS CI 
                 ON WO.OperationCode = CI.Code
        WHERE WO.Interrupted = '0';

     

     

     

    Olaf

Resources