Solved

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

Posted on 2016-08-02
4
57 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 50

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

734 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