Forum Discussion
A819A1L
Apr 08, 2021Brass Contributor
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...
- 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
olafhelper
Apr 09, 2021Bronze Contributor
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
- A819A1LApr 09, 2021Brass ContributorOlaf thank you! This worked perfectly.
Do you know of any really good online learning material that would help me get my head around this sort of thing?- olafhelperApr 09, 2021Bronze Contributor