Avatar of rodneygray
rodneygrayFlag for United States of America

asked on 

Create SQL View in another database from 2 tables in existing database. Mdsh 1013

I have two databases A & B. I need to create a view in database A from 2 tables in database B. My objective is to have Name, MiddleName, Last Name, and badge number in the view which will be located in database A.
______________________________
Use A
go
_______________________________
create view EmployeeWork
AS
      Select B.dbo.inf_employee.Name,
               B.dbo.inf_employee.middlename,
               B.inf_employee.lastname,
               B.dbo.inf_badge_repo.cardnum
      from B.dbo.inf_employee
      INNER JOIN B.dbo.inf_employee on B.dbo.inf_employee.badge = B.dbo.inf_badge_repo.badge
go
__________________________________________

When I run this code, I get the following error messages " Msg 1013, Level 16, State 1, Procedure EmployeeWork, Line 3
The objects "B.dbo.inf_employee" and "B.dbo.inf_employee" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
Microsoft SQL Server

Avatar of undefined
Last Comment
rodneygray
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rodneygray
rodneygray
Flag of United States of America image

ASKER

Great article
Rodney, the article is enough to help you or you need more input from the Experts?
Avatar of rodneygray
rodneygray
Flag of United States of America image

ASKER

My issue turned out to be the method used to join the tables. I had to use the CAST function to convert the linking fields to an integer value. Final code that worked:
use databasename
go
create view EmployeeWork
AS
      Select A.table1.Name As FirstName
               ,A.table1.MiddleName
               ,A.table1.LastName
               ,A.table1.BirthDate
               ,A.table1.Address
               ,A.table1.ZipCode
               ,A.table1.City
               ,A.table1.State
               ,A.table1.HireDate
               ,A.table1.DismissalDate
               ,A.table1.Image as photo
               ,A.table1.badge
               ,A.table1.code
               ,A.table2.name as DeptName
               ,A.table3.cardnum
      from A.table1
      inner JOIN A.table3 on cast(A.table3.badge as int) = cast(A.table1.badge as int)
      inner join A.table2 on A.table2.id = A.table1.groupid


go
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo