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.
Related
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.
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
I'm still learning SQL and I've been scavenging the internet for a solution.. For some reason I can't grasp the concept for this query 100%. I've been on this for a few days and getting no where. I do apologize in advance, I'm sure this is pretty simple for most of you.
I have a table called dbo.personnelUDF and dbo.personnel table in the same database. The dbo.personnelUDF references back to dbo.personnel.personid. Basically, I need to find the value located in dbo.personneludf.employee_id_ and update the dbo.personneludf.active_fueler_ = '1'
This is what am I'm getting.. what am I doing wrong??
UPDATE dbo.PersonnelUDF
SET Active_Fueler_ = '1'
FROM dbo.PersonnelUDF AS a
INNER JOIN dbo.Personnel ON dbo.Personnel.ObjectID = dbo.PersonnelUDF.ObjectID
WHERE dbo.PersonnelUDF.Employee_ID_ = 123456
Error:
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "dbo.PersonnelUDF.ObjectID" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "dbo.PersonnelUDF.Employee_ID_" could not be bound.
You have aliased the table and then havent used it, you can do the below
UPDATE dbo.PersonnelUDF
set Active_Fueler_ = '1'
from dbo.PersonnelUDF INNER JOIN dbo.Personnel
ON dbo.Personnel.ObjectID=dbo.PersonnelUDF.ObjectID
where dbo.PersonnelUDF.Employee_ID_=123456
OR
UPDATE A
set Active_Fueler_ = '1'
from dbo.PersonnelUDF AS A INNER JOIN dbo.Personnel
ON dbo.Personnel.ObjectID = A.ObjectID
where A.Employee_ID_=123456
The error is because you have aliased PersonnelUDF in the FROM clause "AS a". If you get rid of that, it should work.
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
...
Im writing sql query and i have issue which i cannot fix.
I'm trying this:
CREATE TABLE a
AS
WITH cteCandidates (Miejscowosc, Ulica, NrDomu, KodPocztowy)
AS
(
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM Gimnazja
INTERSECT
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM SzkolyPodstawowe
)
Select
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
FROM
Gimnazja AS e
INNER JOIN cteCandidates AS c
ON e.Miejscowosc = c.Miejscowosc AND e.Ulica = c.Ulica AND e.NrDomu = c.NrDomu AND e.KodPocztowy = c.KodPocztowy
INNER JOIN SzkolyPodstawowe s
ON s.Miejscowosc = e.Miejscowosc AND s.Ulica = e.Ulica AND s.NrDomu = e.NrDomu AND s.KodPocztowy = e.KodPocztowy
Order By 'Gimnazja'
And errors which i get:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 319, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
And i don't know why. I think that my query is proper but clearly not, and i don't understand why.
Perhaps you want:
WITH cteCandidates (...)
SELECT
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
INTO a -- < -- table created here
FROM
Gimnazja AS e
INNER JOIN ...;
CREATE TABLE is used to define a table schema. The WITH keyword is used to define a CTE retrieval of data. These are two completely different operations.
If you need a table within your SQL look at http://msdn.microsoft.com/en-us/library/ms174979.aspx to see the syntax for defining it. You then may populate it with INSERT but that's highly unlikely to be from a CTE ( see below)
Usually with CTEs you don't need to insert the data into a table; in the statement immediately after it you can just use the data retrieved as the table has been temporarily defined and populated for you automatically.
A CTE can be used to define a VIEW - see Create view based on hierarchy / used of CTE for an example
Are you trying to create a view? Then it'd work...