nested query error with where clause - asp.net

I have a problem in this query.
I want to retrieve a data from a table by comparing with other column of table.
error is: An expression of non-boolean type specified in a context where a condition is expected, near ')'.
cmd.CommandText = "SELECT [news_id], [news_title] FROM [upload_news] WHERE [country]='" + DropDownList1.Text + "' AND (SELECT DISTINCT[authority] FROM [user_data] WHERE [authority]<>'trusted')";

The problem is that you are not doing any comparison with this condition:
AND (SELECT DISTINCT[authority] FROM [user_data] WHERE [authority]<>'trusted')
This is just returning a distinct list of items, and furthermore, is not being compared with something. Remember that statements on the WHERE clause are comparisons that results in a boolean value.

Related

SQLite Select...Where statement "no such column" error

I have the following SQLite statement:
"SELECT * FROM HISTORICALPICKS WHERE BETTYPE = " + betType.ToString().ToUpper() + " ORDER BY DATETIME ASC"
And betType.ToString().ToUpper() is the string "ATS"
For some reason I get the following error:
System.Data.SQLite.SQLiteException : SQL logic error or missing database
no such column: ATS
...which leaves me baffled. ATS is not a column. BETTYPE is the column. I want it to select rows that have "ATS" in the BETTYPE column.
My hunch is that something is wrong with the syntax. Here is a picture showing the table name and column name, along with a highlight of the value ATS:
You need single quotes around the value.
"SELECT * FROM HISTORICALPICKS WHERE BETTYPE = '" + betType.ToString().ToUpper() + "' ORDER BY DATETIME ASC"

JDBC - SQLITE Select to variable

I am trying to run a query / select statement and save it in a variable. I know how to get something specific from a specific column but not from counting rows.
This is working as I getting MYID specifically.
ResultSet MYIDrs = stmtCFG.executeQuery( "SELECT rowid, MYID from MYINDEX order by rowid desc limit 1;" );
MYID = MYIDrs.getString("MYID");
Now I am trying to count the rows that works in SQLite client but not in the jdbc as I can't figure out what to request.
this is what I have but is not resulting in what I am expecting.
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString(?????);
problem or question is: What do I put in the ????? as I already tried everything.
I am expecting to see a number.
I am really sorry I found what I was looking for by assigning a name TOTAL
This is my code and it works...
ResultSet FILE_COUNTrs = stmtCFG.executeQuery( "SELECT count(*) AS TOTAL from TABLE where MYID = '"+MYID+"';");
FILE_COUNT = FILE_COUNTrs.getString("TOTAL");
You use wrong data type. COUNT(*) returns Integer type, Not String.
You can do like this without assigning a label for COUNT(*)
int FILE_COUNT = FILE_COUNTrs.getInt(1); // 1: is the column index of COUNT(*)

Execute stored procedure error in select statement

I have a Procedure like this,
create or replace
PROCEDURE SP_PROOF
( proof_id IN NUMBER
, Type1 IN VARCHAR2
, StatementType IN NUMBER
, Resultset OUT NUMBER
) AS
BEGIN
IF StatementType = 1 Then
INSERT INTO ID_Proof (proofid,Id_type)
VALUES (proof_id, Type1);
ELSIF StatementType=2 THEN
SELECT proofid,Id_type Into Resultset FROM ID_Proof;
ELSIF StatementType=3 THEN
UPDATE ID_Proof SET Id_type = Type1 WHERE proofid = proof_id;
ELSIF StatementType=4 THEN
DELETE FROM ID_Proof WHERE proofid = proof_id;
end if;
end;
Im getting an error like this,
Error(14,1): PL/SQL: SQL Statement ignored
Error(14,64): PL/SQL: ORA-00947: not enough values
Please help me to correct the error.
Line 14 is:
SELECT proofid,Id_type Into Resultset FROM ID_Proof;
You are selecting two values, proofid and Id_type, into a single scalar variable Resultset. But you also have no filter, so even if you changed that to select a single value then you'd get a too-many-rows error if there was more than one row in the table (and no-data-found if the table is empty).
It isn't clear what you want to happen; perhaps you want select id_type into resultset from id_proof, but from the parameters id_type is a string - so selecting that into a number variable is likely to fail too. Or perhaps you want all IDs for the specified type, in which case the type of result set would need to be a table type or a ref cursor.
Having separate procedures and functions would be probably be clearer, too.

razor sql error

I have this razor statement
sql = "SELECT * FROM CarBike" +
"Order By id OFFSET #0 ROWS FETCH NEXT #1 ROWS ;";
var result = db.Query(sql, offset, pageSize);
i am getting error
Incorrect syntax near the keyword 'By'.
Invalid usage of the option NEXT in the FETCH statement.
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'By'.
Invalid usage of the option NEXT in the FETCH statement.
Please help me to correct this error
You need a space between CarBike and Order by
sql = "SELECT * FROM CarBike" +
" Order By id OFFSET #0 ROWS FETCH NEXT #1 ROWS ;";
NB: OFFSET/FETCH is SQL 2012+ only.
To achieve similar results in previous versions
select * from
(
select *, ROW_NUMBER() over (order by id) rn
from CarBike
) v
where rn between #0+1 and #0+#1
order by id

JOIN for Membership table

Why Microsoft uses stupid examples with GroupBy clause only for one table?
http://msdn.microsoft.com/en-us/library/bb896341.aspx
And why error is causing when i tried to Select some field that is not in GroupBy?
Error says that fields that is not in GroupBy clause doesn't exist in current context.
using (Entities db = new Entities())
{
var query = "SELECT RLS.RoleId, UIR.UserId, UIR.RoleId, USR.UserName FROM Entities.aspnet_Roles AS RLS " +
"INNER JOIN Entities.vw_aspnet_UsersInRoles AS UIR " +
"ON RLS.RoleId = UIR.RoleId " +
"INNER JOIN Entities.aspnet_Users AS USR " +
"ON UIR.UserId = USR.UserId " +
//"GROUP BY RLS.RoleId" // is uncomment - it says UserId doesn't exist in context
;
var x = new ObjectQuery<DbDataRecord>(query, db);
var y = x.ToTraceString().Replace("\n", " ").Replace("\t", " ").Replace("\r", " ");
}
The same for LINQ:
var x = from RLS in db.aspnet_Roles
join URS in db.vw_aspnet_UsersInRoles
on RLS.RoleId equals URS.RoleId
join USR in db.aspnet_Users
on URS.UserId equals USR.UserId
group RLS by RLS.RoleId into GRP
select new
{
GRP.Key
};
The question is: how to select all fields and GroupBy selection by only one field?
Thanks in advance.
All fields that are not in the GROUP BY clause have to be aggregate expressions such as AVG(...) or SUM(...) or MAX(...) etc.
So this is wrong:
SELECT RLS.RoleId, UIR.UserId, UIR.RoleId, USR.UserName
(...)
GROUP BY RLS.RoleId
because the last three expressions in the SELECT should either be aggregated somehow or added to the GROUP BY clause.
This is logical because by specifying GROUP BY RLS.RoleId you say that RLS.RoleId should be unique in the end result, so only one row per value. But for each RLS.RoleID there could be multiple values for UIR.UserId, UIR.RoleId or USR.UserName. So you need to tell the DBMS what to do to make them into one row: average, maximum, minimum... OR you add fields to the GROUP BY clause, so that not RLS.RoleID but the combination of column values should be unique. See also this example.
I don't see any aggregate functions (COUNT, SUM, AVG, etc) being used in the query you posted. Thus, there's no need to include a GROUP BY expression, and there's really no reason to do so. Could you please explain what you're trying to accomplish?
I think i didn't understand littlegreen.
His advice really helped ... though i was upset by SQL Server comparing to MySQL (there is enough to set one grouping field).
Works!
SELECT DISTINCT
RLS.RoleId,
COUNT(RLS.RoleId) AS RID_CNT,
MAX(CAST(UIR.UserId AS VARCHAR(36))) AS EXPR1,
MAX(CAST(USR.UserName AS VARCHAR(36))) AS EXPR2
FROM aspnet_Roles AS RLS
INNER JOIN vw_aspnet_UsersInRoles AS UIR ON RLS.RoleId = UIR.RoleId
INNER JOIN aspnet_Users AS USR ON UIR.UserId = USR.UserId
GROUP BY RLS.RoleId

Resources