Solved

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

Posted on 2016-08-02
4
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41739344
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
ID: 41741664
Great article
0
 
LVL 51

Expert Comment

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

Author Closing Comment

by:rodneygray
ID: 41806849
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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

630 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