473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Delete rows from one table using rows from another table

Using the the NumId from TitleData, I would like to delete the
corresponding row in Bookdata using pure SQL. I want it to delete all
rows in bookdata where the Titledata.NumID is a match to bookdata.id
The two tables are linked in that the NumId of table Titledata is
identical to the Id of table bookdata. I can, using ADO, loop thru
deleting one by one but I would like to do this in a pure SQL
statement. Is this possible? Any help is appreciated.

I was thinking something like this way :

"Delete from Bookdata where Titledata.NumID = Bookdata.id"
But of course it will error.

My current code is:
(frmlogon.tablename is really Titledata)

Dim rstry As New ADODB.Recordset
Dim values As Variant
SQLQuery = "Select Numid from " & frmLogon.Tablename
Set rstry = frmLogon.cnConnection.Execute(SQLQuery)
values = rstry.GetRows
Set rstry = Nothing
'now loop thru
Dim xx As Integer
xx = 0
Do Until xx > UBound(values, 2)
SQLQuery = "Delete from Bookdata where bookdata.Id = '" & values(0,
xx) & "'"
frmLogon.cnConnection.Execute (SQLQuery)
xx = xx + 1
Loop
'create statements for 2 tables involved are

conn.Execute "CREATE TABLE TitleData" & _
"(Id INT IDENTITY (1, 1) NOT NULL PRIMARY KEY," & _
"NumId INT DEFAULT 0 )"

conn.Execute "CREATE TABLE BookData" & _
"(Id INT IDENTITY (1, 1) NOT NULL," & _
"Titles TEXT DEFAULT ''," & _
"GeneralNote TEXT DEFAULT ''," & _
"Author VARCHAR(100) DEFAULT ''," & _
"Imprint VARCHAR(100) DEFAULT ''," & _
"ISBN VARCHAR(100) DEFAULT ''," & _
"Description VARCHAR(100) DEFAULT ''," & _
"CallNumberPre VARCHAR(5) DEFAULT ''," & _
"CallNumber VARCHAR(25) DEFAULT '',LOCNumber VARCHAR(30) DEFAULT '',"
& _
"Accession VARCHAR(25) DEFAULT ''," & _
"Bibliography VARCHAR(100) DEFAULT ''," & _
"Series VARCHAR(100) DEFAULT ''," & _
"MyStatus VARCHAR(70) DEFAULT ''," & _
"Barcode VARCHAR(50) DEFAULT ''," & _
"LocalData VARCHAR(100) DEFAULT ''," & _
"CheckoutPeriod VARCHAR(10) DEFAULT ''," & _
"CatalogCard TEXT DEFAULT ''," & _
"Summary TEXT DEFAULT ''," & _
"MyCount VARCHAR(10) DEFAULT ''," & _
"ItemDate DATETIME DEFAULT ''," & _
"MyUser VARCHAR(50) DEFAULT ''," & _
"MarcData TEXT DEFAULT ''," & _
"SdlsRecord TEXT DEFAULT '', LOSC VARCHAR(5) DEFAULT '', LOSN
Decimal(14,6) DEFAULT 0," & _
"Edits Char(1) DEFAULT '', TitleDuplicate VARCHAR(50) DEFAULT '')"

Jul 23 '05 #1
14 16227
DELETE BookData
WHERE EXISTS(SELECT * FROM TitleData WHERE TitleData.NumID=BookData.ID)

-- OR

DELETE BookData
FROM BookData
JOIN TitleData ON TitleData.NumID=BookData.ID

Method (1) is better because the server will not have to eliminate duplicate
results from the join (there may be 2 TitleData for a given BookData)

Mr Tea
http://mr-tea.blogspot.com/
<sd********@msn.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Using the the NumId from TitleData, I would like to delete the
corresponding row in Bookdata using pure SQL. I want it to delete all
rows in bookdata where the Titledata.NumID is a match to bookdata.id
The two tables are linked in that the NumId of table Titledata is
identical to the Id of table bookdata. I can, using ADO, loop thru
deleting one by one but I would like to do this in a pure SQL
statement. Is this possible? Any help is appreciated.

I was thinking something like this way :

"Delete from Bookdata where Titledata.NumID = Bookdata.id"
But of course it will error.

My current code is:
(frmlogon.tablename is really Titledata)

Dim rstry As New ADODB.Recordset
Dim values As Variant
SQLQuery = "Select Numid from " & frmLogon.Tablename
Set rstry = frmLogon.cnConnection.Execute(SQLQuery)
values = rstry.GetRows
Set rstry = Nothing
'now loop thru
Dim xx As Integer
xx = 0
Do Until xx > UBound(values, 2)
SQLQuery = "Delete from Bookdata where bookdata.Id = '" & values(0,
xx) & "'"
frmLogon.cnConnection.Execute (SQLQuery)
xx = xx + 1
Loop
'create statements for 2 tables involved are

conn.Execute "CREATE TABLE TitleData" & _
"(Id INT IDENTITY (1, 1) NOT NULL PRIMARY KEY," & _
"NumId INT DEFAULT 0 )"

conn.Execute "CREATE TABLE BookData" & _
"(Id INT IDENTITY (1, 1) NOT NULL," & _
"Titles TEXT DEFAULT ''," & _
"GeneralNote TEXT DEFAULT ''," & _
"Author VARCHAR(100) DEFAULT ''," & _
"Imprint VARCHAR(100) DEFAULT ''," & _
"ISBN VARCHAR(100) DEFAULT ''," & _
"Description VARCHAR(100) DEFAULT ''," & _
"CallNumberPre VARCHAR(5) DEFAULT ''," & _
"CallNumber VARCHAR(25) DEFAULT '',LOCNumber VARCHAR(30) DEFAULT '',"
& _
"Accession VARCHAR(25) DEFAULT ''," & _
"Bibliography VARCHAR(100) DEFAULT ''," & _
"Series VARCHAR(100) DEFAULT ''," & _
"MyStatus VARCHAR(70) DEFAULT ''," & _
"Barcode VARCHAR(50) DEFAULT ''," & _
"LocalData VARCHAR(100) DEFAULT ''," & _
"CheckoutPeriod VARCHAR(10) DEFAULT ''," & _
"CatalogCard TEXT DEFAULT ''," & _
"Summary TEXT DEFAULT ''," & _
"MyCount VARCHAR(10) DEFAULT ''," & _
"ItemDate DATETIME DEFAULT ''," & _
"MyUser VARCHAR(50) DEFAULT ''," & _
"MarcData TEXT DEFAULT ''," & _
"SdlsRecord TEXT DEFAULT '', LOSC VARCHAR(5) DEFAULT '', LOSN
Decimal(14,6) DEFAULT 0," & _
"Edits Char(1) DEFAULT '', TitleDuplicate VARCHAR(50) DEFAULT '')"

Jul 23 '05 #2
Thank so very much.
The first one works fine
The second one works with MySQL as well as SQLServer

Can anything done using ADO recordset type of action be performed
using pure SQL statements including something using say CHARINDEX in a
string manipulation type of query? Such as where you would be looking
in the string for something and then add in text or delete text at
specific positions in the string?

I

Jul 23 '05 #3
Probably, MS-TSQL is pretty good until the strings get above 8000 bytes,
then it gets messy.
UDFs on SQL2K can be used to encapsulate re-usable string manipulation code.

e.g.
http://msdn.microsoft.com/library/de...u-sus_6btz.asp

Mr Tea
http://mr-tea.blogspot.com

<sd********@msn.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Thank so very much.
The first one works fine
The second one works with MySQL as well as SQLServer

Can anything done using ADO recordset type of action be performed
using pure SQL statements including something using say CHARINDEX in a
string manipulation type of query? Such as where you would be looking
in the string for something and then add in text or delete text at
specific positions in the string?

I

Jul 23 '05 #4
Here is how I do it using ADO recordsets.
I look for 'Checked out by ' in the text field called marcdata
I then look in each row returned for '=904' and delete all text from
the marcdata string up to the next equal sign.
If another equal sign is not found then I just take out the whole part
of the string right to the end.

Can this be done using pure SQL?

SQLQuery = "Select Id, marcdata from Bookdata where
CHARINDEX('Checked out by ',marcdata) <> '0' "

Dim rsTry As New ADODB.Recordset
rsTry.CursorLocation = adUseClient
frmLogon.cnConnection.Open
rsTry.Open SQLQuery, frmLogon.cnConnection, adOpenForwardOnly,
adLockOptimistic

Do Until rsTry.EOF
'update local status
'remove it from marc if present
a = InStr(1, rsTry!MarcData, "=904")
If a <> 0 Then
b = InStr(a + 1, rsTry!MarcData, "=")
If b <> 0 Then rsTry!MarcData = Left(rsTry!MarcData, a - 1) &
Mid(rsTry!MarcData, b)
If b = 0 Then rsTry!MarcData = Left(rsTry!MarcData, a - 1) &
vbCrLf

rsTry.Update
End If
rsTry.MoveNext
Loop

Jul 23 '05 #5
This will do a similar job in SQL (char(13)+char(10) replicate the &vbCrLf
in your code):

UPDATE BookData SET marcdata=left(marcdata, charindex('=904',marcdata)-1)+
CASE WHEN charindex('=', marcdata, charindex('=904', marcdata)+1)>0
THEN substring(marcdata, charindex('=', marcdata,
charindex('=904',marcdata)+1), 8000)
ELSE char(13)+char(10) END
WHERE marcdata LIKE '%Checked out by%' AND marcdata LIKE '%=904%'
you could turn this into a function that makes it more readable and allows
re-use (SQL2K):

CREATE FUNCTION dbo.Strip (@Data varchar(8000), @Start varchar(50), @Finish
varchar(50), @Append varchar(50))
RETURNS varchar(8000) AS
BEGIN
DECLARE @Index int
SET @Index = CharIndex(@Start, @Data)
IF @Index>0
SET @Data = Left(@Data, @Index-1)+
CASE WHEN charindex(@Finish, @Data, @Index+1)>0
THEN substring(@Data, charindex(@Finish, @Data, @Index+1), 8000)
ELSE @Append END
RETURN @Data
END

GO

UPDATE BookData SET marcdata=dbo.STRIP(marcdata, '=904', '=',
char(13)+char(10)) WHERE marcdata LIKE '%Checked out by%' AND marcdata LIKE
'%=904%'
if you are just looking to remove an '=904' from the string then it becomes
much simpler:

UPDATE BookData SET markdata=replace(marcdata,'=904','') LIKE '%Checked out
by%' AND marcdata LIKE '%=904%'
Another way maybe to normalise that data into a checkout table and avoid the
need for string manipulation completely.

CREATE TABLE dbo.Checkout
(
NumID int,
data int
)
GO
DELETE Checkout WHERE data=904

Mr Tea
http://mr-tea.blogspot.com

<sd********@msn.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Here is how I do it using ADO recordsets.
I look for 'Checked out by ' in the text field called marcdata
I then look in each row returned for '=904' and delete all text from
the marcdata string up to the next equal sign.
If another equal sign is not found then I just take out the whole part
of the string right to the end.

Can this be done using pure SQL?

SQLQuery = "Select Id, marcdata from Bookdata where
CHARINDEX('Checked out by ',marcdata) <> '0' "

Dim rsTry As New ADODB.Recordset
rsTry.CursorLocation = adUseClient
frmLogon.cnConnection.Open
rsTry.Open SQLQuery, frmLogon.cnConnection, adOpenForwardOnly,
adLockOptimistic

Do Until rsTry.EOF
'update local status
'remove it from marc if present
a = InStr(1, rsTry!MarcData, "=904")
If a <> 0 Then
b = InStr(a + 1, rsTry!MarcData, "=")
If b <> 0 Then rsTry!MarcData = Left(rsTry!MarcData, a - 1) &
Mid(rsTry!MarcData, b)
If b = 0 Then rsTry!MarcData = Left(rsTry!MarcData, a - 1) &
vbCrLf

rsTry.Update
End If
rsTry.MoveNext
Loop

Jul 23 '05 #6
I am getting an error which reads out as:
Argument data type text is invalid for argument 1 of left function.

The marcdata column is a text column

I have to put this in a string to execute it like this:

SQLQuery = "UPDATE BookData SET marcdata=left(marcdata,
charindex('=904',marcdata)-1)+" & _
"CASE WHEN charindex('=', marcdata, charindex('=904',
marcdata)+1)>0" & _
"THEN substring(marcdata, charindex('=', marcdata,
charindex('=904',marcdata)+1), 8000)" & _
"ELSE char(13)+char(10) END WHERE marcdata LIKE '%Checked
out by%' AND marcdata LIKE '%=904%'"

The checkout data is stored in its own table, this is mostly for backup
and or viewing
if someone happens to browse thru the marc field tag
information that is stored for each item.

Jul 23 '05 #7
heh, LEFT wont work on text, youll have to do a substring instead:

UPDATE BookData SET marcdata=substring(marcdata, 1,
charindex('=904',marcdata)-1)+
CASE WHEN charindex('=', marcdata, charindex('=904', marcdata)+1)>0
THEN substring(marcdata, charindex('=', marcdata,
charindex('=904',marcdata)+1), 8000)
ELSE char(13)+char(10) END
WHERE marcdata LIKE '%Checked out by%' AND marcdata LIKE '%=904%'

Keep in mind that SQL has problems with data much longer than 8000 bytes.

Mr Tea
http://mr-tea.blogspot.com

<sd********@msn.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I am getting an error which reads out as:
Argument data type text is invalid for argument 1 of left function.

The marcdata column is a text column

I have to put this in a string to execute it like this:

SQLQuery = "UPDATE BookData SET marcdata=left(marcdata,
charindex('=904',marcdata)-1)+" & _
"CASE WHEN charindex('=', marcdata, charindex('=904',
marcdata)+1)>0" & _
"THEN substring(marcdata, charindex('=', marcdata,
charindex('=904',marcdata)+1), 8000)" & _
"ELSE char(13)+char(10) END WHERE marcdata LIKE '%Checked
out by%' AND marcdata LIKE '%=904%'"

The checkout data is stored in its own table, this is mostly for backup
and or viewing
if someone happens to browse thru the marc field tag
information that is stored for each item.

Jul 23 '05 #8
That works very well.
Is a text column size ever larger than 8000 bytes?
The data in this column is nowhere near that large and will at most be
4000 bytes or less.

Jul 23 '05 #9
I found out I dont need the VbCrLf added into the string after all
How would this line be coded leaving out the ELSE char(13)+char(10)
part of
the statement?

Jul 23 '05 #10
I got it,
You leave out 'ELSE chr(10)+Chr(13)'. The END ends the case part of the
query.
It is working and even I can comprehend it
Thanks very much for all the help.

Jul 23 '05 #11
hang on a sec, anything concat NULL yields NULL.

you will need ELSE '' END

Mr Tea
http://mr-tea.blogspot.com

<sd********@msn.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I got it,
You leave out 'ELSE chr(10)+Chr(13)'. The END ends the case part of the
query.
It is working and even I can comprehend it
Thanks very much for all the help.

Jul 23 '05 #12
Yes, I just found out, it nulls it to nothing

Jul 23 '05 #13
Ok, I put it in as '' and its is working.
Thanks for taking the time to look at this.

Jul 23 '05 #14
sd********@msn.com (sd********@msn.com) writes:
Is a text column size ever larger than 8000 bytes?
That's the whole point with using text. A single text value can accomdate
up to 2 GB of data.
The data in this column is nowhere near that large and will at most be
4000 bytes or less.


In such case, I would considering to change the data type of the column to
varchar(4000), since text is quite cumbersome to handle.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gert Schumann | last post by:
I want to delete just parts of tables, so I can't use 'TRUNCATE'. As I want to delete about millions of lines, I need a very big rollback segment. The best way would be to delete without using...
7
by: Philip Mette | last post by:
I have been searching many postings and I cant seem to find anyone that has this answer so I decided to post. I am using SQL (Transact-SQL).If I have 2 tables with columns...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
2
by: Zarrin | last post by:
Hello, I read several articles of newsgroup about the bulk delete, and I found one way is to: -create a temporary table with all constraints of original table -insert rows to be retained into...
5
by: Neil | last post by:
I am getting time-out errors when I try to perform a simple delete on a linked server. The command is: Delete From MyTable Where PKID=12345 I have tried executing this command directly from...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
3
by: Michel Esber | last post by:
Hello, Environment: DB2 LUW v8 FP15 / Linux I have a table with 50+ Million rows. The table structure is basically (ID - Timestamp). I have two main applications - one inserting rows, and the...
29
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.