Error: The multi-part identifier could not be bound - sql-server

select teamid,
a.playerid,
dbo.FullName(a.playerid) as fullName,
Total_Hits,
Total_At_Bats,
Totals_At_Bats,
Batting_Avg,
Team_Batting_Rank,
All_Batting_Rank
FROM batting
Error message received:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "a.playerid" could not be bound.

Simply remove the a alias cause there no a alias/table in the FROM clause, that's why you get this error message, because SQL Server can't find playerid column in a table named/aliased a
select teamid,
playerid,
dbo.FullName(playerid) as fullName,
Total_Hits,
Total_At_Bats,
Totals_At_Bats,
Batting_Avg,
Team_Batting_Rank,
All_Batting_Rank
FROM batting
There is no column a.playerid there, unless you add an alias to your table as
FROM batting a

Related

Bouncing Between Multi-Part Identifier Could Not Be Bound & Ambiguous Column Name

I keep getting two error messages depending on what I try to do to fix them.
Firstly here is my code:
SELECT
ConsltNum AS 'Consultant Number',
COUNT(ConsltNum) AS 'Client Count',
AVG(Balance) AS 'Average'
FROM
Client Cl
INNER JOIN
Consultant Cn ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY
Cn.LastName
Upon running it, I get this.
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'ConsltNum'.
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'ConsltNum'.
Now, I know it is ambiguous because the ConsltNum exists in both tables I've included. Normally I'd try and dial into that database by changing the code to this:
SELECT
Client.ConsltNum AS 'Consultant Number',
COUNT(Client.ConsltNum) AS 'Client Count',
AVG(Balance) AS 'Average'
FROM
Client Cl
INNER JOIN
Consultant Cn ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY
Cn.LastName
Upon running this, I get:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Client.ConsltNum" could not be bound.
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "Client.ConsltNum" could not be bound.
A few other things worth mentioning: I've tried dialling to dbo.Client.ConsltNum and it throws the same error.
Just use the right alias:
SELECT cl.ConsltNum AS Consultant_Number,
COUNT(*) AS Client_Count, AVG(?.Balance) AS Average
FROM Client Cl INNER JOIN
Consultant Cn
ON Cl.ConsltNum = Cn.ConsltNum
GROUP BY cl.ConsltNum;
The ? is for the the alias of the table where balance comes from.
Notes:
You need to use the table alias assigned for the column.
The GROUP BY should match the SELECT column.
Only use single quotes for string and date constants. Give columns names that don't need to be escaped.
You might was well use COUNT(*), because you know ConsltNum is never NULL.

SQL Server syntax error identifier could not be bound

Why is this SQL getting a syntax error and how can it be fixed?
UPDATE s
SET s.modified_date = l.action_date
FROM
(SELECT l.action_date, l.user_id FROM item_audit_log) l
WHERE l.user_id = s.staff_id
Error:
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.action_date" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.user_id" could not be bound.
You are getting the error because your subquery references "l" in the subquery, but it is not defined inside the subquery (it is defined in the outer scope). However, you don't need the subquery:
UPDATE s
SET modified_date = l.action_date
FROM staffs s JOIN
item_audit_log l
ON l.user_id = s.staff_id;
This assumes you have a table called s. I imagine that this really should be an alias, also defined in the FROM clause.
As your request is "WHY" this occurs, let me clarify it :
This error usually occurs when an alias is used when referencing a
column in a SELECT statement and the alias used is not defined
anywhere in the FROM clause of the SELECT statement.

MDX+TSQL column name doesn't exist

I have to create a mixture of MDX and TSQL as follows:
select "[State].[Country].[Country].[MEMBER_CAPTION]" as State,
"[Measures].[someMeasure]" as [Sum]
from openquery(my_olap_server,
'select [Measures].[someMeasure] on 0,
filter([State].[Country].[Country],not isempty([Measures].[someMeasure])) on 1
from (select {[State].[Country].[Country].&[index_here]} on columns from [My Cube])')
If the MDX returns no value, than the [State].[Country].[Country].[MEMBER_CAPTION] does not exists, so the query fails with the message
Msg 207, Level 16, State 1, Line 2
Invalid column name '[State].[Country].[Country].[MEMBER_CAPTION]'
.
Is there a way to force either MDX or TSQL (but i'm guessing MDX) to provide this?
Thanks
select "[Measures].[StateName]" as State,
"[Measures].[someMeasure]" as [Sum]
from openquery(my_olap_server,
'with member [Measures].[StateName]
as [State].[Country].CURRENTMEMBER.MEMBER_CAPTION
select {[Measures].[StateName],[Measures].[someMeasure]} on 0,
filter([State].[Country].[Country],not isempty([Measures].[someMeasure])) on 1
from (select {[State].[Country].[Country].&[index_here]} on columns from [My Cube])')
is the solution. Simple enough :)

What is the error in DELETE query when using joins

Code is running fine when commenting DELETE statement and trying with SELECT statement.
Please help
DELETE FROM
--select * from
Site as s
join
(select SiteID,Code, Name, Dense_rank() over (partition by Code order by SiteID ) as Rank from Site
) as t
on s.SiteID = t.SiteID
WHERE t.Rank != 1
Getting following error message
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'as'.
You can't alias a delete table, but delete can refer to an alias. Instead of this:
delete from Site as s
...
Try:
delete from s
from Site as s
...

sql azure beginner- getting error for an simple insert statement

I am trying to insert a row of data into a table in SQL Azure --
This is the insert statement--
INSERT INTO cloud_storage_credentials (cloud_provider,api_key, api_secret)
VALUES("Rackspace", "<random_value>", "<random_value>");
However I am getting the following error--
Msg 207, Level 16, State 1, Line 2
Invalid column name 'Rackspace'.
Msg 207, Level 16, State 1, Line 2
Invalid column name '<random_value>'.
Msg 207, Level 16, State 1, Line 2
Invalid column name '<random_value>'.
What am I doing wrong here? I followed an SQL Server tutorial to write the above query...
change the " (double quotes) to ' (tick/single quote)
INSERT INTO cloud_storage_credentials (cloud_provider,api_key, api_secret)
VALUES('Rackspace', '<random_value>', '<random_value>');
Change your "s with 's. As it is, it thinks that the values you are inserting are name of columns.

Resources