The multi-part identifier "Reservation.ReservationID" could not be bound - sql

I am trying an inner join like this.
select R.ReservationID, R.BookingNumber,P.FirstName, P.LastName
from Reservation R inner join PersonName P
on Reservation.ReservationID = PersonName.ResrvationID
I am getting the errors as:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Reservation.ReservationID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "PersonName.ResrvationID" could not be bound.

The aliases "R" and "P" masks the actual table name.
Although it does not apply here, it makes sense generally. For example, when you have a self join
select R.ReservationID, R.BookingNumber,P.FirstName, P.LastName
from Reservation R inner join PersonName P
on R.ReservationID = P.ReservationID --use aliasas

Try instead
select
R.ReservationID
, R.BookingNumber
, P.FirstName
, P.LastName
from Reservation R
inner join PersonName P
on R.ReservationID = P.ResrvationID

Related

How do I resolve multi-part identifier errors

I am unable to figure out the issue with the following query:
select distinct
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
Sum(s.CurySlsPrice - s.CuryCost) AS GrossProfit,
Sum((s.CurySlsPrice - s.CuryCost)/s.CurySlsPrice) AS GrossPer
from
SOLine s, Inventory i, Vendor v
Inner Join
Inventory on Inventory.InvtID = s.InvtID
Inner join
Vendor on Vendor.VendID = Inventory.Supplr1
Union All
select distinct
a.InvtID, s.OrdNbr, s.OrdNbr, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
s.Status, a.PerPost
from
ARTran a, Inventory i, SOLine S
Inner join
Inventory on Inventory.InvtId = ARTran.InvtID
Inner join
ARTran on ARTran.OrdNbr = SOLine.OrdNbr
The errors are as follows:
Msg 4104, Level 16, State 1, Line 20
The multi-part identifier "s.InvtID" could not be bound.
Msg 4104, Level 16, State 1, Line 31
The multi-part identifier "ARTran.InvtID" could not be bound.
Msg 4104, Level 16, State 1, Line 33
The multi-part identifier "SOLine.OrdNbr" could not be bound.
I have tried everything I know (my brain may be a little foggy) yet continue to get the errors.
What is the issue with my query?
You should clean up your query to something like this:
use only the proper JOIN syntax - get rid of that old, crappy "comma-separated list of tables" in the FROM clause!
use table alias on the joined tables
Try this:
SELECT
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
SUM(s.CurySlsPrice - s.CuryCost) AS GrossProfit,
SUM((s.CurySlsPrice - s.CuryCost)/s.CurySlsPrice) AS GrossPer
FROM
SOLine s
INNER JOIN
Inventory i ON i.InvtID = s.InvtID
INNER JOIN
Vendor v ON v.VendID = i.Supplr1
GROUP BY
i.Supplr1, v.Name, s.InvtID, s.CuryCost, s.CurySlsPrice, s.OrdNbr
UNION ALL
SELECT DISTINCT
a.InvtID, s.OrdNbr, s.OrdNbr, s.CuryCost, s.CurySlsPrice, s.OrdNbr,
s.Status, a.PerPost
FROM
SOLine s
INNER JOIN
Inventory i ON i.InvtId = a.InvtID
INNER JOIN
ARTran a ON a.OrdNbr = s.OrdNbr
The columns you're SELECTing don't exist. Check your database schema

The multi-part identifier " " could not be bound

i have this stored procedure
Create Proc Get_Order_Production
AS
SELECT i.Name_Item
,[ID_Order]
,[Project]
,[Length]
,[Width]
,[Quantity]
,[Weight]
,[Supplier]
,[GDS_Name]
,[GDS_Date]
,[WorkshopName]
,[WorkshopDate]
,[ReservedQty]
FROM OdredDetails od
INNER JOIN ItemQuantity iq
ON iq.ID_ItemQte = od.ID_ItemQte
INNER JOIN item i
ON i.ID_Item = iq.ID_Item
where [Order].Number_Order=od.ID_Order and [Order].[Location]='PRODUCTION'
When I execute I receive this error
Msg 4104, Level 16, State 1, Procedure Get_Order_Production, Line 23 [Batch Start Line 2]
The multi-part identifier "Order.Number_Order" could not be bound.
Msg 4104, Level 16, State 1, Procedure Get_Order_Production, Line 23 [Batch Start Line 2]
The multi-part identifier "Order.Location" could not be bound.
I try to use the original tables name and use parentheses but I failed
USE Join syntax , remove join from where clause
SELECT
--column names
FROM order Inner join
OdredDetails od on [Order].Number_Order=od.ID_Order
--(it is orderdetails I guess)
INNER JOIN ItemQuantity iq
ON iq.ID_ItemQte = od.ID_ItemQte
INNER JOIN item i
ON i.ID_Item = iq.ID_Item
WHERE [Order].[Location]='PRODUCTION'

SQL Server error: the multi-part identifier "C.Name" could not be bound

I have this SQL query:
CREATE VIEW QueryV5
AS
SELECT DISTINCT
C.Name, P.Name, SUM(Duration.Time) AS TotalTime
FROM
cmpt354_starwars.dbo.Characters C,
cmpt354_starwars.dbo.Planets P,
(SELECT (T.[Time of Departure] - T.[Time of Arrival]) AS Time
FROM cmpt354_starwars.dbo.TimeTable T
WHERE T.CharacterName = C.Name AND T.PlanetName = P.Name) AS Duration
WHERE
P.Affiliation = 'neutral'
And I get the errors:
Msg 4104, Level 16, State 1, Procedure QueryV5, Line 3
The multi-part identifier "C.Name" could not be bound.
Msg 4104, Level 16, State 1, Procedure QueryV5, Line 3
The multi-part identifier "P.Name" could not be bound.
I don't understand what's going on, why it won't let me use Duration as an alias for the nested query. I've compared my query to other people's and I can't see any syntactic differences. What's going on?
C.Name, P.Name both are having the same column name, that causing the issue. Please provide different alias name for any one of the column will solve the issue.
So this can be
SELECT DISTINCT C.Name, P.Name, SUM(Duration.Time) AS TotalTime
replace to this:
SELECT DISTINCT C.Name, P.Name AS PlantName, SUM(Duration.Time) AS TotalTime
UPDATE:
Could you try with the JOIN approach
CREATE VIEW QueryV5 AS
SELECT DISTINCT
C.Name, P.Name AS PlanetName, SUM(T.[Time of Departure] - T.[Time of Arrival]) AS TotalTime
FROM cmpt354_starwars.dbo.TimeTable T
JOIN cmpt354_starwars.dbo.Characters C ON C.Name = T.CharacterName
JOIN cmpt354_starwars.dbo.Planets P ON P.Name = T.PlanetName
WHERE P.Affiliation = 'neutral'
GROUP BY C.Name, P.Name
Please, use ANSI syntax of JOIN:
CREATE VIEW QueryV5
AS
SELECT T.CharacterName,
T.PlanetName,
SUM(T.[Time of Departure] - T.[Time of Arrival]) AS TotalTime
FROM cmpt354_starwars.dbo.TimeTable T
INNER JOIN cmpt354_starwars.dbo.Planets P
ON T.PlanetName = P.Name
INNER JOIN cmpt354_starwars.dbo.Characters C
ON T.CharacterName = C.Name
WHERE P.Affiliation = 'neutral'
GROUP BY T.CharacterName, T.PlanetName
And there is no need for C.Name and P.Name - use columns from TimeTable.

The multi-part identifier could not be bound

trying this
select tblPersonalInfo.companyname, tblJobBudget.title,tblJobBudget.lastmodifiedby,
tblJobAdv.advtitle, tblJobAdv.userId,
tblApplication.advid, tblApplication.position
from tblJobAdv
inner join tblApplication
ON tblJobAdv.advid = tblApplication.advid
inner join tblPersonalInfo
On tblJobBudget.lastmodifiedby = tblPersonalInfo.userid
gives error
Msg 4104, Level 16, State 1, Line 8
The multi-part identifier "tblJobBudget.lastmodifiedby" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "tblJobBudget.title" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "tblJobBudget.lastmodifiedby" could not be bound.
This is because there aren't any table or table alias with tblJobBudget identifier.
Your tables are:
tblJobAdv
tblApplication
tblPersonalInfo
But not:
tblJobBudget
If you need columns from table tblJobBudget you should include tblJobBudget in tables with a join clause:
from tblJobAdv
inner join tblApplication
ON tblJobAdv.advid = tblApplication.advid
inner join tblJobBudget <--here
ON ...
inner join tblPersonalInfo
ON ...

Advanced SQL INSERT

I have this query that works fine. It selects rows into the SY.UserOptions table for the ‘Jeff’ user.
However, I created another query that I want to do the same thing, but for every user. So I added SY.Users to the query, which in effect mulplies the 2 tables together. However, it gives me an error that I do not understand.
--This works
SELECT ‘Jeff’, t.Application, t.Task, tl.Description
FROM SY.Tasks t
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
-- This does not work
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Tasks t, SY.Users u
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
--Here is the error
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.Application" could not be bound.
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.Task" could not be bound.
Msg 4104, Level 16, State 1, Procedure CreateUserOptions, Line 15
The multi-part identifier "t.DftAccessLevel" could not be bound.
Can I not multiply tables together like that and include a join?
You need a field to join the users table to the Tasks table.
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Tasks t
INNER JOIN SY.Users u --LEFT OUTER if it makes a difference
ON t.user = u.User --not sure if these fields are available maybe some type of userId?
LEFT OUTER JOIN SY.TaskLevels tl
ON t.Application = tl.Application
And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
I think the problem is that in the second query, when you join the SY.Users table and the SY.TaskLevels table, you are referencing the SY.Tasks table - which is not part of the join.
Switch the Sy.Users table and the Sy.Tasks table around and your problem should be fixed.
It's because you're joining USERS to TaskLevels instead of Tasks to TaskLevels.
Try This:
SELECT u.[User], t.Application, t.Task, tl.Description
FROM SY.Users u, SY.Tasks t
LEFT OUTER JOIN SY.TaskLevels tl ON t.Application = tl.Application And t.Task = tl.Task AND t.DftAccessLevel = tl.AccessLevel
This will give you the cartesian product of users with (Tasks TaskLevels) though. So every user will have every task.