Solved

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

Posted on 2016-08-02
4
27 Views
Last Modified: 2016-09-20
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.
0
Comment
Question by:rodneygray
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
you need to use aliases  see this article
https://www.experts-exchange.com/articles/11135/Why-should-I-use-aliases-in-my-queries.html
a different alias for the same table, and it will work
1
 
LVL 1

Author Comment

by:rodneygray
Comment Utility
Great article
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Rodney, the article is enough to help you or you need more input from the Experts?
0
 
LVL 1

Author Closing Comment

by:rodneygray
Comment Utility
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now