DELETE SQL: how delete only one value at a time - oracle

i have this sql string
"DELETE FROM project.cart WHERE isbn = ?";
If the table there are many isbn same, I can delete a single isbn every time I click on jbutton?
PS: isbn = ? -> takes its value from a txfield and it all works, but if there are equal isbn delete them all of course.
thanks to all.

Oracle inserts a hidden column called ROWNUM
DELETE FROM project.cart WHERE isbn = ? and ROWNUM=1;

Related

Trying to update a field in Oracle based on a select statement - getting subquery returns more than one row error

I am trying to update a field on a table the query needs to get the value from a 2nd table and use that to get the data from a 3rd table. I keep getting the "ORA-01427: single-row subquery returns more than one row" Any help is appreciated.
update ord_detail set cuser1 = (select c.email from contact c,ord_detail m join orders o on o.ID = m.orders_ID where c.email is not null)
where EXISTS (select email from contact,orders where orders.contact_id2 = contact.id)
Since you did not provide further information like the results of your sub selects, it's not 100% clear what exactly is your problem. You should check those results and if this does not answer your question, please provide the details.
My guess is that this sub query will give multiple rows because you are missing a join from the table "contact" to one of the other tables:
select c.email from contact c,ord_detail m
join orders o on o.ID = m.orders_ID where c.email is not null
Therefore, this sub query will always lead to many rows as result unless the table "contact" contains one row only whose column email is not null.

LINQ Query can return the counts of each column of DataTable

I have a query, I am using a data table and I want to see each variable how many rows have values without null and value(-1) in that particular column. If anyone can help me to use the LINQ query and check the numbers.
Example: I have a table (City, Age, Gender) and i want to show the user that which column has blanks.... I mean in my current dataset city & Age has 200 rows but gender has 170 due to punching error so I through the loop to check each column I want to show the counts.
Here is what I have tried but that is not working. And its Vb.net code.
Dim dtRaw As New DataTable
daGlobal = New OleDb.OleDbDataAdapter(CMD, MyConnection)
daGlobal.Fill(dtRaw)
daGlobal.Fill(dtGlobal)
For Each column As DataColumn In dtRaw.Columns
Dim xss As String = column.ColumnName
Dim MyCounts = (From xss In dtRaw.AsEnumerable()
Where (Not String.IsNullOrEmpty(xss)).Count())
Next

Find the same and replace it

There are 2 tables:
Now I want to query the title of the author table for the same value as the barcode field in the holding table. Then? Add the value of Auth in author to callNo in holding .
This is what I wrote:
update holding h set
h.CALLNO = (select a.author
from AUTHOR a
where a.TITLE = h.BARCODE)
This statement is wrong, it transformed my column into null.

oracle - UPDATE SQL: how to update several times the same line

I need to Update quantity of several books, before delete them.
the deleting happen with this code (oracle has a hidden column called ROWNUM)
DELETE FROM project.cart WHERE isbn = ? and ROWNUM=1;
to delete ISBN one at time (there are equal isbn in the table). but the update only works for one isbn. it should update several times the same line for all isbn found.
String sql3= "UPDATE PROject.book SET quantity=quantity +1 WHERE project.book.isbn in "
+ "(SELECT project.cart.isbn FROM project.cart) ";
// this code work perfectly, but for one time.
I hope you can help me. thanks
You can try this:
String sql3= "UPDATE PROject.book SET quantity=quantity + "
+ "(select count(*) FROM project.cart where project.cart.isbn = project.book.isbn) "
+ " WHERE project.book.isbn in (SELECT project.cart.isbn FROM project.cart)";
Do you mean that if a book is in the cart three times it should have 3 added its quantity column?
If so, you need to add the count. When a row is included by the WHERE logic, it's included only once, even if it matched more than one of the criteria. Try a correlated subquery instead:
UPDATE book
SET quantity = quantity + (SELECT COUNT(*) FROM cart WHERE book.isbn = cart.isbn)
This query will run very slowly if there are a lot of book rows, so you may want to add your original WHERE clause to the end to limit the number of book rows:
UPDATE book
SET quantity = quantity + (SELECT COUNT(*) FROM cart WHERE book.isbn = cart.isbn)
WHERE isbn IN (SELECT isbn FROM cart)

table subtraction challenge

I have a challenge that I haven’t overcome in the last two days using Stored Procedures and SQL 2008.
I took several approaches but must fell short.
One appraoch very interesting was using a table substraction.
It’s really all about table subtraction.
I was wondering if you could help me crack this one.
Here is the challenge:
Two tables 1Testdb y 2Testdb.
My first step was to select ID relationships ([2Testdb].Acc_id) on table 2Testdb for one given individual ([2Testdb].Bus_id). Then query table 1Testdb for records not mathcing my original selection from 2Testdb.
But other approaches are welcome.
Data and Structures:
USE [Challengedb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[1Testdb](
[Acc_id] [uniqueidentifier] NULL
[Name] [Varchar(10)] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[2Testdb](
[Acc_id] [uniqueidentifier] NULL,
[Bus_id] [uniqueidentifier] NULL
) ON [PRIMARY]
GO
Records on 1Testdb:
34455F60-9474-4521-804E-66DB39A579F3, John
C23523F6-2309-4F58-BB3F-EF7486C7AF8B, Pete
DC711615-3BE4-4B31-9EF2-B1314185CA62, Dave
E3AAB073-2398-476D-828B-92829F686A4C, Adam
Records on 2Testdb: (Relationship table, ex. Friend relationships)
Record #1: DC711615-3BE4-4B31-9EF2-B1314185CA62, 34455F60-9474-4521-804E-66DB39A579F3
Record #2: E3AAB073-2398-476D-828B-92829F686A4C, 34455F60-9474-4521-804E-66DB39A579F3
Record # 3: DC711615-3BE4-4B31-9EF2-B1314185CA62, E3AAB073-2398-476D-828B-92829F686A4C
Record # 4: E3AAB073-2398-476D-828B-92829F686A4C, DC711615-3BE4-4B31-9EF2-B1314185CA62
Challenge: Select from table 1Testdb only those records distinct that may not have a relationship with John [34455F60-9474-4521-804E-66DB39A579F3] on table 2Testdb.
Expected result should be (Who does John doesn’t have relationship with?):
C23523F6-2309-4F58-BB3F-EF7486C7AF8B, Pete
Thank you,
Valentin
Not sure exactly what you're asking; is it for all users in the first table that don't have a friendship with "John" based on the second table?
If so, use the "not exists" keyword to determine whether or not a record exists with the given query:
select a.*
from [1testdb] a
where not exists (
select * from [2testdb] b where a.acc_id = b.acc_id and b.subid = '34455F60-9474-4521-804E-66DB39A579F3'
)
and a.acc_id <> '34455F60-9474-4521-804E-66DB39A579F3'
I'm not sure how your columns are set up...looks like GUIDs, but this is the SQL Server syntax for getting it to work. I included a case where John could be in either the Acc_id or Bus_id column, so that's why there are 2 joins instead of one.
Declare #id NVarchar(50)
Set #id = '34455F60-9474-4521-804E-66DB39A579F3'
Select *
From 1Testdb
Left Outer Join 2Testdb As ForwardRelationship On ForwardRelationship.Acc_id = #id And ForwardRelationship.Bus_id = 1Testdb.Acc_id
Left Outer Join 2Testdb As ReverseRelationship On ReverseRelationship.Bus_id = #id And ReverseRelationship.Acc_id = 1Testdb.Acc_id
Where
ForwardRelationship.Acc_id Is Null And
ForwardRelationship.Bus_id Is Null And
ReverseRelationship.Acc_id Is Null And
ReverseRelationship.Bus_id Is Null And
1Testdb.Acc_id <> #id

Resources