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

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%'";

Related

Two Inner Joins in OleDb SQL query [duplicate]

This question already has answers here:
SQL multiple join statement
(3 answers)
Closed 4 years ago.
I'm trying to make an SQL query with an OleDbCommand into an Access database (.accdb).
While this command works fine (in a OleDbCommand.ExecuteReader()):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID ";
This other doesn't, and I can't understand why (all examples I see around use the exact same syntax):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID " +
"INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_ID ";
The exception given is the following System.Data.OleDb.OleDbException:
Syntax error (missing operator) in query expression 'cred.CRED_ID = rel.REL_USR_CRED_CRED_ID INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_I' (message is cut here)
Version and details:
The database is a .accdb file created on Access 2010
The connection is created in C# with System.Data.OleDb.OleDbConnection
The connection provider is "Microsoft.ACE.OLEDB.12.0"
(This seems like a useless query, but of course I'll add a WHERE usr.SOME_FIELD = some_condition)
"For multi-table joins, you have to nest the extra joins in brackets:"
SQL multiple join statement

Converting sql to lambda expression or Linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Hi I have following sql statement I want to convert it to lambda expression or linq
select
a.lid
,a.name
,a.notes
,lrh.e
,lrh.date
from rate a
left outer join lab_history lrh on(lrh.lab_id=a.lid)
Please let me know how to change this to lambda expression or linq. Thanks
You can use this query:
from a in db.rate
join lrh db.lab_history on a.lid equals lrh.lab_id
into lrhs
from lrh in lrhs.DefaultIfEmpty()
select new
{
lid = a.lid,
... another props
}
You can try
var labRateObj = (from a in ContextObj.rate
join lrh in ContextObj.lab_history on a.lid equals lrh.lab_id into labrate
from s in labrate.DefaultIfEmpty()
select new {
lid = a.lid,
name = a.name,
notes = a.notes,
e = lrh.e,
date = lrh.date
});

Incorrect syntax near the keyword 'ON' [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
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";

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