Incorrect syntax near the keyword 'ON' [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
While executing following query, it shows error, "Incorrect syntax near the keyword 'ON'."
string query = "SELECT Employee_Master.Employee_Code AS 'EmployeeCode',
Employee_Master.Employee_ID AS'Employee_ID',
Employee_Master.FIRST_NAME +' '+ ISNULL(Employee_Master.MIDDLE_NAME,'') +' ' +Employee_Master.LAST_NAME AS'emp_Name',
Task_Master.Task_Code AS'Task_Code',
Task_Master.Task_Name AS'Task_Name',
Task_Completion_Status_Master.Task_Complition_Status_Name AS 'Task_status',
Work_Submission_Master.Submission_Date AS'Submission_Date' " + "FROM Work_Submission_Detail INNER JOIN"+ "Work_Submission_Master ON Work_Submission_Detail.Work_Submission_ID = Work_Submission_Master.Work_Submission_ID INNER JOIN"+ "Task_Master ON dbo.Work_Submission_Detail.Task_ID = Task_Master.Task_ID INNER JOIN"+ "Task_Completion_Status_Master ON "+ "Work_Submission_Detail.Completion_Status = Task_Completion_Status_Master.Task_Complition_Status_ID INNER JOIN"+ "Employee_Master ON Work_Submission_Master.Employee_ID = Employee_Master.Employee_ID";

You need to put space after INNER JOIN
INNER JOIN"+ "Work_Submission
Should be
INNER JOIN "+ "Work_Submission
Wherever you have used the INNER JOIN, you missed that space. So put it wherever you have used this.
Using such large inline query, I would suggest you to use Stored Procedure instead of inline query, that will help you write query with ease as well as readability along with less chance to have typo errors.

Try this
string strqry = "SELECT EM.Employee_Code as 'EmployeeCode', EM.Employee_ID as'Employee_ID', EM.FIRST_NAME +' '+ ISNULL(EM.MIDDLE_NAME,'') +' ' +EM.LAST_NAME as'emp_Name', TM.Task_Code as'Task_Code', TM.Task_Name as'Task_Name',Task_Completion_Status_Master.Task_Complition_Status_Name as 'Task_status',WSM.Submission_Date as'Submission_Date' " +
"FROM Work_Submission_Detail WSD INNER JOIN "+
"Work_Submission_Master WSM ON WSD.Work_Submission_ID = WSM.Work_Submission_ID INNER JOIN "+
"Task_Master TM ON dbo.WSD.Task_ID = TM.Task_ID INNER JOIN "+
"Task_Completion_Status_Master ON "+
"WSD.Completion_Status = Task_Completion_Status_Master.Task_Complition_Status_ID INNER JOIN "+
"Employee_Master EM ON WSM.Employee_ID = EM.Employee_ID";

Related

getting ORA-00933 while executing SQL query using ExecuteStoreQuery method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting ORA-00933 error while executing SQL query using ExecuteStoreQuery method in C#.
Here is the code:
public IEnumerable<Administrator> FilterEmployees(string name)
{
Context db = new Context();
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES" +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID" +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID" +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";
List<Administrator> employees;
return db.ExecuteStoreQuery<Administrator>(query, "employees", System.Data.Objects.MergeOption.AppendOnly);
}
The query is fine(tested on Oracle SQL Developer)
You're concatenating strings, and there is no whitespace between the last word of one string and the beginning of the next. Leading to this query being executed : (formatting mine)
SELECT
USER_ROLES.USER_ROLES_ID,
EMPLOYEES.EMPLOYEE_ID,
EMPLOYEES.EMPLOYEE_NAME,
EMPLOYEES.SURNAME_1,
EMPLOYEES.SURNAME_2,
ROLES.NAME FROM USER_ROLESINNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_IDINNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_IDWHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'
^ Problem is here ^and here ^and here
Add a space to the end of each of the strings.
The query is not correct-
While writing such queries inline, you must be sure that there should be proper spaces between the keywords-
You should debug the code and see what query variable returns
I think you should modify your query as follows (by giving spaces at the end of each line)
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES " +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID " +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID " +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";

SQLException at Inner Join

I am using a C# program that calls a SQL statement that is executed on an instance of SQL Server 2008 R2. Here is the SQL Call:
SELECT TOP 1 as1.AssetTagID, as1.TagID, as1.CategoryID, as1.Description,
as1.HomeLocationID, as1.ParentAssetTagID
FROM Assets AS as1 ORDER BY ar.DateScanned DESC
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE (ar.ReadPointLocationID='Readpoint1' OR ar.ReadPointLocationID='Readpoint2')
AND as1.TagID!='000000000000000000000000';
I am getting an SQLException around INNER. The exception text is the following:
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'INNER'.
I can put the stack trace on here as well, but I felt like it would clutter the question. Here is the actual string in the C# code that I am using with the call:
"SELECT TOP 2 as1.AssetTagID, as1.TagID, " +
"as1.CategoryID, as1.Description, as1.HomeLocationID," +
"as1.ParentAssetTagID FROM Assets AS as1 ORDER BY ar.DateScanned DESC\n" +
"INNER JOIN AssetsReads AS ar ON as1.AssetTagID = ar.AssetTagID\n" +
"WHERE (ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[0] +
"' OR ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[1] + "')\n" +
"AND as1.TagID!='000000000000000000000000';"
Your ORDER BY statement can't be there. Move it to the end
I'll also give the obligatory "Don't do this" speech. Concatenating SQL strings like this opens you up to SQL injection attacks. There is plenty of information about that on SO and Google so I won't go into it, but you should definitely consider making this a parameterized query.
Like he said... your order by clause was out of order :)
SELECT TOP 1 as1.AssetTagID,
as1.TagID,
as1.CategoryID,
as1.Description,
as1.HomeLocationID,
as1.ParentAssetTagID
FROM Assets AS as1
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE ( ar.ReadPointLocationID = 'Readpoint1'
OR ar.ReadPointLocationID = 'Readpoint2' )
AND as1.TagID != '000000000000000000000000'
ORDER BY ar.DateScanned DESC;
I'll also note that using schema qualified objects is recommended by Microsoft (http://technet.microsoft.com/en-us/library/ms190387(v=sql.105).aspx). Also you should use parenthesis around your top (value) statement.
SELECT TOP (1) [as1].[AssetTagID],
[as1].[TagID],
[as1].[CategoryID],
[as1].[Description],
[as1].[HomeLocationID],
[as1].[ParentAssetTagID]
FROM [<schema>].[Assets] AS [as1]
INNER JOIN [<schema>].[AssetsReads] AS [ar]
ON [as1].AssetTagID = [ar].[AssetTagID]
WHERE ( [ar].[ReadPointLocationID] = 'Readpoint1'
OR [ar].[ReadPointLocationID] = 'Readpoint2' )
AND cast([as1].TagID AS [INT]) != 0
ORDER BY [ar].[DateScanned] DESC;

convert sql query into lambda expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is my sql query i want to convert it into lambda expression
Select P.PlaceName As UnitNumber,
PB.PlaceName,
A.Locality,
A.SubLocality,
A.Sublocality_Level_1
from Listing L
inner join Place P ON L.PlaceId = P.Id
Inner Join Place PB ON PB.Id = P.ParentPlaceId
Inner Join [Address] A ON A.Id = PB.AddressId
Where L.Id =9
Thanks in advance .
Query expression is much simpler in this case. I don't recommend you to use lambdas with many joins
from l in db.Listing
join p in db.Place on l.PlaceId equals p.Id
join pb in db.Place on p.ParentPlaceId equals pb.Id
join a in db.Address on pb.AddressId equals a.Id
where l.Id == 9
select new {
UnitNumber = p.PlaceName,
pb.PlaceName,
a.Locality,
a.SubLocality,
a.Sublocality_Level_1
}
db is your context

Ambigous error c# / sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
can someone please tell me why i am getting an error with this code ?
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,SUM(total) as total from" +
" ( SELECT CASE WHEN claimed = 'Y' THEN inv_amt *.45 END as isclaimed, (inv_amt *.45) as inclaimable, inv_amt as total from invasset" +
" INNER JOIN Invoice ON invoice.invoice = invasset.invoice WHERE invasset.asset_no = #AssetNumber ) as D2", DataAccess.AssetConnection);
In subquery specifiy where is inv_amt column coming from (is it invoice.inv_amt or invasset.inv_amt)
ambigous column name inv_amt
It seams like the two tables invasset and Invoice both contains the column inv_amt, You have to reference it to an alias in the inner SELECT statement, something like: invoice.inv_amt or invasset.inv_amt:
SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,
SUM(total) as total
FROM
(
SELECT CASE WHEN claimed = 'Y' THEN invoice.inv_amt *.45 END as isclaimed,
(inv_amt *.45) as inclaimable, inv_amt as total
from invasset INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE invasset.asset_no = #AssetNumber
) as D2
When I format your code to be human-readable...
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT
SUM(isclaimable) AS claimable,
SUM(isclaimed) AS claimed,
SUM(total) AS total
FROM
(SELECT
CASE WHEN claimed = 'Y' THEN inv_amt *.45 END AS isclaimed,
(inv_amt *.45) AS inclaimable,
inv_amt AS total
FROM
invasset
INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE
invasset.asset_no = #AssetNumber) AS D2",
DataAccess.AssetConnection);
I can't help but notice that your outer SELECT is looking for a column called isclaimable while your inner SELECT is returning a column called inclaimable. There's a typo in your code.
Edit: In response to your comment on the question, which tables contain the field inv_amt? Clearly you're referencing two tables which have it. Since the only tables you're referencing are invasset and Invoice then clearly both of those tables have a column named inv_amt. You'll have to specify which one you want in your query.
So instead of:
`inv_amt AS total`
You'd want something like:
`invasset.inv_amt AS total`
(Assuming that's the table you want, otherwise use the other table.)

What is the best practice for writing sql queries inside c# code [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
On my current project I'm using SQL CE. Since it doesn't have support for stored procedures I have to write sql queries inside repository.
Option 1:
StringBuilder query = new StringBuilder();
query.Append("SELECT");
query.Append(" c.CUSTOMER_ID,");
query.Append(" COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME");
query.Append(" ct.NAME as CUSTOMER_TYPE");
query.Append("FROM ");
query.Append(" CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID");
Option 2:
string query = "SELECT c.CUSTOMER_ID, COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME, ct.NAME as CUSTOMER_TYPE FROM CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID";
Option 1 seems like a much more readable, especially when I have 10+ tables in join, but option 2 is faster.
Which option should I accept and what's the best practice in this case?
Option 2 may be a few nanoseconds faster, but when you add the time to actually execute in the database (several milliseconds) a few extra nanaoseconds barely registers as noise.
In any case, there is another option that's the best of both worlds: #-strings:
string query = #"
SELECT
c.CUSTOMER_ID,
COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME
ct.NAME as CUSTOMER_TYPE
FROM
CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c
ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID
";
Option 3 - use verbatim string literals:
string query = #"
SELECT
c.CUSTOMER_ID,
COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME,
ct.NAME as CUSTOMER_TYPE
FROM
CT_CUSTOMER_TYPE AS ct
INNER JOIN CUSTOMER AS c
ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID";
I puts SQL string into resource files, it allows easy edit multiline queries, and provides strongly typed named access to that queries even with IntelliSence tooltips.
Why not option 3:
"Select bla bla bla"
"bla bla bla"
"...."
one long literal, split to many lines.
I always use the second method as it is much faster. You use up too many lines of code with the first method, leading to a larger overhead.
I'm used to this approach:
use +, this operator will add string at compile, not run time (https://learn.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings)
this syntax shouldn't change at run time, use const.
use indent to format script well.
The goal purpose is keeping readability and reduces data transfer, and there's no run-time performance cost :) , perfect.
string const query =
"SELECT c.CUSTOMER_ID, " +
"COALESCE (c.FIRST_NAME, ''_ + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME, " +
"ct.NAME as CUSTOMER_TYPE " +
"FROM CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c " +
"ON ct.CUSTOMER_TYPE_ID = c.CUSTOMER_TYPE_ID;";
Have you thought about using LINQ?
https://matthewmanela.com/blog/sql-ce-3-5-with-linq-to-sql/
using(Northwind db = new Northwind(MyConnectionString))
{
(from p in db.Products).ToList();
(from p in db.Customers).ToList();
}
http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.coalesce.aspx

Categories