Search for empty fields on a table - asp.net

I tried the command below. It's a DataSource of a GridView and it's written on my .aspx page, so I can not use characters like "" or ''. This code does not return me anything. I also tried to pass (string.empty, "" and null) as parameters in the codebehind. Nothing worked and I don't know what else to do...
SelectCommand="SELECT ch.id, sit.descricao as situacao, resp.responsavel, ch.dt_cadastro, ch.previsao_termino, func.descricao as funcionalidade,
proj.descricao as projeto ,pr.id as prid, pr.prioridade, clb.clube
FROM chamados AS ch
INNER JOIN prioridades as pr ON ch.prioridade = pr.id
INNER JOIN clubes as clb ON ch.clube = clb.id
INNER JOIN responsaveis as resp ON ch.responsavel = resp.id
INNER JOIN situacoes as sit ON ch.situacao = sit.id
INNER JOIN projetos as proj ON ch.projeto = proj.id
INNER JOIN funcionalidades as func ON ch.funcionalidade = func.id WHERE ch.responsavel IS NULL"
Obs:the field I want to filter as null they are int32 fields.
( I know that int32 can't be null) But I just didn't know what to do, so I tried everything that came to mind.

This query will always return no results because you use INNER JOIN in this statement:
INNER JOIN responsaveis as resp ON ch.responsavel = resp.id
and use the WHERE statenent where you compare one of INNER JOIN fileds with NULL
WHERE ch.responsavel IS NULL
The INNER JOIN joins ONLY fields which are NOT NULL. So these two statement except each other.

I found out the problem, it was:
I'm using INNER JOINS and as #valex told me:
The INNER JOIN joins ONLY fields which are NOT NULL. So these two statement except each other.
The solution was change the INNER JOIN for LEFT JOIN and worked like a charm !
Thanks everyone !

Related

How do i join two tables and get data from both of the table to show to users on form field?

Table A (goradid) has a field which is a FK to Table B(Spriden). I need two sets of data (spriden_id and Goradid_additional_id) from Table A and Table B and insert it into one of the field called e.g. bannerid and partyid. please see my code below as i am also getting a error SQL command not properly ended in PLSQL.
SELECT spriden_id, goradid_additional_id
FROM goradid, spriden
INNER JOIN spriden ON spriden_pidm = goradid_pidm
INTO bannerid, partyid;
The correct syntax is SELECT <COLUMN LIST> INTO <VARIABLE LIST>. So the query should be:
SELECT spriden_id, goradid_additional_id
INTO bannerid, partyid
FROM goradid a INNER JOIN spriden b ON b.spriden_pidm = a.goradid_pidm;
you can use aliasing for this.
it's simple.
SELECT spriden_id as bannerid, goradid_additional_id as partyid FROM goradid INNER JOIN spriden ON spriden_pidm = goradid_pidm;
Note: if you want to use into then you have to initialize the variable.
we use into usually.
declare
v_author author%rowtype;
begin
select * into v_author from author where author_key = 'A103';
dbms_output.put_line('Name:'||v_author.author_first_name||' '|| v_author.author_last_name);
end;

SQL to DQ, use sub select

I'm trying to convert a SQL function to a DQL function.
I don't know how to do the following step:
Join on multiple class
Join on temporary table (created by doctrine)
Call a sub select in my function
Here my actual working SQL code.
SELECT ut.token
FROM user_token ut
INNER JOIN factory f ON f.id = ut.factory_id
INNER JOIN article_factory af ON af.factory_id = f.id
INNER JOIN article a ON a.id = af.article_id
WHERE ut.factory_id = (
SELECT f.id
FROM factory f
INNER JOIN article_factory af ON af.factory_id = f.id
INNER JOIN article a ON a.id = af.article_id
WHERE a.id = :article
)
GROUP BY ut.token
In case of, here a screen of my table relation.
article_factory is not an entity or a class. article_factory was created for my relation between article and factory, when I specified a ManyToMany relation.
EDIT: Tryed throught plain SQL, but got issue making link between article and fatory, since DQL only know entity, and Doctrine is creating a table, which I canno't use. With DQL, I still got the same issue than above, I don't know how to properly join my classes, and make my sub-select.

How to make JOIN with OR expression in DQL?

Here is the SQL equivalent of what I expect to get from Doctrine:
SELECT c.* FROM comments c
LEFT JOIN articles a
ON a.id = c.articles_id OR a.translation = c.articles_id
WHERE c.published = 1 AND c.language = a.language
The problem is that I cannot make Doctrine to generate the JOIN operation with OR as it is supposed to be. If we execute query from the following QueryBuilder object:
$qb->select('c')
->from('Project:Comment', 'c')
->leftJoin('c.article', 'a', 'WITH', 'a = c.article OR a.translation = c.article')
->where('c.published = true AND c.language = a.language');
we receive the following SQL statement:
SELECT
...
FROM comments c0_
LEFT JOIN articles a0_ ON c0_.articles_id = a0_.id
AND (
a0_.id = c0_.articles_id OR
a0_.translation = c0_.profiles_id
)
WHERE c0_.published = 1 AND c0_.language = a0_.language
which is obviously not the same as the initial query, as WITH operator seems to add additional conditions to the basic one instead of replacing the whole condition.
Is there any way to force Doctrine to output exactly what I need? I know that I may use native SQL but I doubt that it will be as convenient as QueryBuilder. Maybe there is a way to extend Doctrine with normal JOIN ON implementation instead of this odd JOIN WITH?
Doctrine doesn't implement this because it is (as far as I understand at least) not considered optimized enough for SQL.
See this SO post for precisions.
What you intend to do could appearantly be done using Union and other types of Join.

Massive Query with inner join not returning any data

I'm using the Massive Query method to write a simple join query against an Oracle database. This is my code with the query simplified even further by taking out some columns:
dynamic logTable = new DynamicModel("mydatabase", "table1");
var sb = new StringBuilder();
sb.Append("select CONTACT_ID from table1 inner join table2 on table1.ID = table2.ID ");
sb.Append("where table1.ID=:0");
dynamic dbResult = logTable.Query(sb.ToString(), id);
The following code gives me an error: 'object' does not contain a definition for 'CONTACT_ID'
string id = dbResult.CONTACT_ID.ToString();
If I take the exact query and run it through sqldeveloper, I get back the expected results. If I try to Query through Massive without a join, I get back an object I can work with.
Any ideas?
My mistake! I was expecting my query to return only one record, but forgot that Query returns IEnumerable. Solution is to take First() or loop over the results.

NHibernate Collection Left Outer Join Where Clause Issue

It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.

Resources