I have a table 'student' in which I have five attributes {id,first,last,age,marks}.
The following are the two tuples inserted.
1,Suresh,Kumar,35,95
2,ramesh,Kapoor,21,90
When I execute the query with mistake
select * from stdent where first='Suresh'
I get the alert message - no such table stdent.Very fair!
But when I execute the following query
select * from student where first='sachin'
I get no alerts or error message even though sachin is not present in any of the tuple.
What is the reason?
It's because the query below is correct. No issues with the query itself. However, no tuple exists with first=sachin. Therefore, nothing is returned.
select * from student where first='sachin'
If you try
select * from student where first='Suresh'
then the query will successfully return 1 row, as a tuple with first=Suresh exists.
Related
The query I'd like to execute:
SELECT *
FROM qryReportView
WHERE ((ID = 719)
AND (Name Like "*x"));
However when I run this query I get no records returned. Each criterion works on its own:
SELECT *
FROM qryReportView
WHERE ID = 719;
and
SELECT *
FROM qryReportView
WHERE Name Like "*x";
Both queries return records as expected but when I combine them something goes wrong. I know there is at least one record for which both criteria are true.
NOTE: When I replace the * wildcard with an explicit name, I get the correct record returned. This is not a viable solution for me though because my query needs to select records ending in "x" with a number of possible prefixes.
Thank you so much for the help.
Your issue is a logical operator issue.
When you ask an RDBMs for a samich and a coke, if it has a samich but not a coke, you get nothing. Which is what is happening here, you need an 'or'
SELECT *
FROM qryReportView
WHERE (ID = 719
OR
Name Like '*x');
I have this:
Query q = em.createNativeQuery("select * from TRANSAKTION",Transaktion.class);
List<Transaktion> trans = q.getResultList();
While this works, using a column name like id doesn't although it exists in database and generated entity class.
Other tables work fine it's just that one and with all columns. When I use * and .getId() it returns the correct id.
What could possibly be the problem?
The error message is:
SQL Exception: invalid column name
I have two tables in my database, bill_datail and bill_log. I want to delete one record from table bill_log and after that trigger an action to do something in table bill_detail. My code for delete is the following:
DELETE FROM [mydatabase].[dbo].[Bill_Log]
WHERE [mydatabase].[dbo].[Bill_Log].[CU_BILL_ID] in
(SELECT
FROM [mydatabase].[dbo].[Bill_Log],[mydatabase].[dbo].[Bill_Detail]
where [mydatabase].[dbo].[Bill_Log].bill_id=37
and [mydatabase].[dbo].[Bill_Log].bill_id=[mydatabase].[dbo].[CU_Bill_Detail].cu_bill_id
and [mydatabase].[dbo].[Bill_Detail].Pay_date>20130206
and [CL_Com_Rec_Description] like '%اoffpage%'
and [mydatabase].[dbo].[Bill_Log].amount<0
and [mydatabase].[dbo].[Bill_Log].[Com_Act_Date]='2013/02/07')
go
CREATE TRIGGER [mydatabase].[dbo].[Bill_Log]
ON [mydatabase].[dbo].[Bill_Log]]
AFTER Delete
AS
---
BEGIN
-- get 'amount' from deleted record and sum it to field 'amount' of bill detail
END
But in delete action I get the following error:
'Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
And I don't know how to fix the error and do the second part.
You only need to get a list of CU_BILL_ID to search. So remove all other fields from inner query and just select CU_BILL_ID.
DELETE FROM [mydatabase].[dbo].[CU_Bill_Log]
WHERE [mydatabase].[dbo].[CU_Bill_Log].[CU_BILL_ID] in
(SELECT cu_bill_id
FROM [mydatabase].[dbo].[CU_Bill_Detail]
where Pay_date>13930206)
and [mydatabase].[dbo].[CU_Bill_Log].cu_bill_id=37
and [mydatabase].[dbo].[CU_Bill_Log].cu_bill_id=
and [mydatabase].[dbo].[CU_Bill_Detail].
and [CL_Com_Rec_Description] like '%اoffpage%'
and [mydatabase].[dbo].[CU_Bill_Log].amount<0
and [mydatabase].[dbo].[CU_Bill_Log].[CL_Com_Act_Date]='2013/02/07'
go
Try this please.
if you want use of "in" keyword in your main query ,
the subquery must return just one column as result
Select ID,F_Name,L_Name
From Clients
Where ID in(
Select ClientID
From Orders
Where OrderNo > 120
)
Suppose I have a table with following records
value text
company/about about Us
company company
company/contactus company contact
I have a very simple query in sql server as below. I am having problem with the 'or' condition. In below query, I am trying to find text for value 'company/about'. If it is not found, then only I want to run the other side of 'or'. The below query returns two records as below
value text
company/about about Us
company company
Query
select
*
from
tbl
where
value='company/about' or
value=substring('company/about',0,charindex('/','company/about'))
How can I modify the query so the result set looks like
value text
company/about about Us
A bit roundabout, but you can check for the existence of results from the first where clause:
select
*
from
tbl
where
value='company/about' or
(
not exists (select * from tbl where value='company/about')
and
value=substring('company/about',0,charindex('/','company/about'))
)
Since your second condition can be re-written as value = 'company' this would work (at least for the data and query you've presented):
select top(1) [value], [text]
from dbo.MyTable
where value in ('company/about', 'company')
order by len(value) desc
The TOP() ignores the second row if both are found, and the ORDER BY ensures that the first row is always the one with 'company/about', if it exists.
I have 2 similar queries
select *
from openquery(powerschool,
'select *
from TEACHERS
where teachernumber is not null
and schoolid=''1050''
and teacherloginid is not null
order by teachernumber')
and
SELECT *
from openquery(powerschool,
'SELECT NVL(teachernumber,'''')
from TEACHERS
where teachernumber is not null
and schoolid=''1050''
and teacherloginid is not null
order by teachernumber')
The first one is giving me 182 rows while the second one gives me 83.
What's wrong with the queries?
Second query never would return a null for the teachers table because of the NVL() so it could return more records depending on the data.
basically the " and teacherloginid is not null " never gets hit because you replace the nulls with ""
Just thoughts...
Same server? That is, linked server is different in target or credentialss o you are reading a different "TEACHERS" table
What does running both linked SQL statement actually on the linked server (not locally) give you?