Solved

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

Posted on 2016-08-02
4
33 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
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 46

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 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

19 Experts available now in Live!

Get 1:1 Help Now