ASP NET SqlException: Incorrect Syntax - sql

I have a method in my ASP NET webpage to execute a SQL command. When I execute the following command I am getting
SqlException was unhandled by user code
The SQL error is
Incorrect syntax near Chemicals'
Here is the SQL command string:
SELECT MainHeading
FROM Areas
INNERJOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo
WHERE (Chemicals.IDMark = #item)
Where #item is a session variable.
The simplified tables are as follows

You missed a space between innerjoin
SELECT MainHeading
FROM Areas
INNER JOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo
WHERE (Chemicals.IDMark = #item)

It must be:
SELECT MainHeading FROM Areas INNER JOIN Chemicals ON Areas.AreaNo = Chemicals.AreaNo WHERE (Chemicals.IDMark = #item)
INNER JOIN were two words :)
As an information for you, INNER JOIN is the same as JOIN.

Related

Getting the following error: SQL 80001 Incorrect syntax near "Natural"

I am having issues with the following code and I have attached the two pieces of code I am working with as well as the error I am getting.
Here is the code:
CREATE PROCEDURE Get_rentalandemployees
AS
BEGIN
SELECT
Rental_ID, Rental_Start_Date, Rental_End_Date,
Rental_Type_Description,
Employee_id, Employee_First_Name, Employee_Last_Name,
COUNT(VIN) AS Vehicle_count
FROM
Rentals R
INNER JOIN
Rental_Types RT ON R.Rental_Type = Rental_type_Id
INNER JOIN
Employees E ON E.Employee_id = R.Rental_Employee_Id
NATURAL JOIN
Rental_Vehicles RV
GROUP BY
Rental_ID
END;
Here is the second piece I am working with:
CREATE PROCEDURE Rental_repair_cost
AS
BEGIN
SELECT
Rental_ID, Rental_Start_Date, Rental_End_Date,
SUM(ISNULL(Repair_cost, 0)) AS Total_repair_cost
FROM
Rentals R
NATURAL JOIN
Rental_vehicles
WHERE
Rental_Type_Description = 'Personal'
END;
Here are the errors:
SQL 80001: Incorrect syntax near 'Natural'. Miscellaneous SQL.query5.sql 10
SQL 80001: Incorrect syntax near 'Natural'. Miscellaneous SQL.query7.sql 7
Happily, SQL Server doesn't support NATURAL JOIN. So you need an explicit ON condition:
SELECT Rental_ID, Rental_Start_Date, Rental_End_Date,
SUM(COALESCE(Repair_cost,0)) as Total_repair_cost
FROM Rentals R JOIN
Rental_vehicles
ON R.Vehicle_ID = rv.Vehicle_ID -- guessing at the condition
WHERE Rental_Type_Description = 'Personal';
Note that there is nothing "natural" about so-called natural joins. In particular, they depend only on columns having the same name -- and they don't take advantage of properly declared foreign key relationships. I would suggest that you simply forget that some databases support them.

SQL Query error ' multi-part identifier vSMS_CombinedDeviceResources.MachineID could not be bound'

I have to create a custom report in System Center Configuration Manager using SQL Server Report Builder.
I have already selected the dataset and the connection to the SQL database is successfully established. I have to write a Query in SQL to fetch the data from the views. This is the SQL Query:
SELECT
(
vSMS_CombinedDeviceResources.MachineID,
vSMS_CombinedDeviceResources.Name,
vSMS_CombinedDeviceResource.CurrentLogonUser
FROM vSMS_CombinedDeviceResources
INNER JOIN vSMS_Update_ComplianceStatus
ON vSMS_CombinedDeviceResource.MachineID = vSMS_Update_ComplianceStatus.MachineID
INNER JOIN v_UpdateDeploymentSummary
ON v_UpdateDeploymentSummary.CI_ID = vSMS_Update_ComplianceStatus.CI_ID
WHERE v_UpdateDeploymentSummary.CollectionName=#CollectionName
AND vSMS_CombinedDeviceResources.ClientState!=0
)
But I am getting an error as
The multi-part identifier vSMS_CombinedDeviceResources.MachineID
could not be bound
Could anyone please specify the error that I have made in the SQL query?
you have a wrong ( ) after the select word and around the rest of query
SELECT vSMS_CombinedDeviceResources.MachineID
,vSMS_CombinedDeviceResources.Name
,vSMS_CombinedDeviceResource.CurrentLogonUser
FROM vSMS_CombinedDeviceResources
INNER JOIN vSMS_Update_ComplianceStatus ON vSMS_CombinedDeviceResource.MachineID = vSMS_Update_ComplianceStatus.MachineID
INNER JOIN v_UpdateDeploymentSummary ON v_UpdateDeploymentSummary.CI_ID = vSMS_Update_ComplianceStatus.CI_ID
WHERE v_UpdateDeploymentSummary.CollectionName=#CollectionName
AND vSMS_CombinedDeviceResources.ClientState!=0

Access SQL syntax error (missing operator) when query contains multiple JOINs

I'm trying to make an UPDATE query (using Access 2013) that calculates a value based on values stored in two separate linked tables. Here is the code I'm using:
UPDATE tblCreatures
INNER JOIN tblRole ON tblCreatures.Role = tblRole.RoleName
INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);
This gives me a syntax error, saying
Syntax error (missing operator) in query expression "tblCreatures.Role = tblRole.RoleName INNER JOIN tblRank ON tblCreatures.Rank = tblRank.RankNam"
(and yes, it cuts off at RankNam, not RankName)
Testing things out, if I remove one of the inner joins (and thus all references to that table) then the update query works just fine, but if I put the other inner join back in, I continuously get this same syntax error. I don't understand why... I should be able to put two inner joins next to each other, shouldn't I?
Access SQL requires parentheses when a query contains multiple JOINs. If you build the query in Access' query designer it will look like this (reformatted for clarity):
UPDATE
(
tblCreatures
INNER JOIN
tblRole
ON tblCreatures.Role = tblRole.RoleName
)
INNER JOIN
tblRank
ON tblCreatures.Rank = tblRank.RankName
SET tblCreatures.HP = ((tblRole.Level_0_HP + (tblCreatures.NominalLevel * tblRole.BonusHP)) * tblRank.HP_Multiplier);

Syntax error (missing operator) when using UPDATE with INNER JOIN

I'm trying to execute this SQL query on an MS Access DB:
UPDATE WW_B
SET WW_B.WWtype_ID=1
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr;
But I get the following error:
[Microsoft][ODBC Microsoft Access Driver]
Syntax error (missing operator) in expression 1 INNER JOIN New_data on WW_B.StdNr = New_data.StdNr.
I don't see where any operator is needed, since I don't use any parentheses or quotation marks.
I've also tried WWtype_ID='1' and WWtype_ID="1" and got the same error.
What am I doing wrong?
I was having the same problem and found this question while searching for an answer. Luckily I was able to find a solution while digging through some Access queries at work.
This is just another situation where MS Access does not play nice with standard SQL syntax.
The other answers are incorrect. You do not need a FROM clause, Access will just give you the same error message. Where you ran into the error was where you placed the JOIN. It seems intuitive that you would have FROM...JOIN... But this is MS Access where working with SQL is never intuitive.
In Access, UPDATE seems to take the place of FROM. Therefore, you add the JOIN statement to the end of the UPDATE clause.
UPDATE WW_B
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr
SET WW_B.WWtype_ID=1;
You can also add a WHERE clause after the SET statement if desired.
You need a FROM clause when using an INNER JOIN on an UPDATE:
UPDATE WW_B
SET WW_B.WWtype_ID = 1
FROM WW_B
INNER JOIN New_data
on WW_B.StdNr = New_data.StdNr
You are missing the FROM clause
UPDATE WW_B SET WW_B.WWtype_ID=1 FROM <your table> INNER JOIN New_data on WW_B.StdNr = New_data.StdNr

MS Access SQL Query - syntax error(missing operator) in query expression

I am trying to understand why my query(below) displays an error message in MS Access Sql query editor(sqlview) when I run it.
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
As far as I know the script below if I delete either of the INNER join lines. For instance, this script runs with no errors
SELECT *
FROM tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE]
And so does this
SELECT *
FROM tblUSPS
INNER JOIN tblAttribute ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID
But when I combine, something goes wrong and I am unable to find it so I would like some help identifying this please.
Access has strong opinions on parentheses.
SELECT *
FROM
(tblUSPS
INNER JOIN tblProductUSPS
ON tblProductUSPS.[PRODUCTUSPS_USPS] = tblUSPS.[USPS_CODE] )
INNER JOIN tblAttribute
ON tblUSPS.USPS_ID = tblAttribute.ATTRIBUTE_USPSID