Solved

delete query in sql server express

Posted on 2016-07-31
7
39 Views
Last Modified: 2016-08-12
I have a select query (was easier to show it that way)  but i would like to be able to delete these rows from TblWordTags but I dont know how to construct the delete query.

I would like to create a stored procedure and pass the variables of [word] (in this case 'BM') and also the [vehCategory] in this case 'Motorcycle'

I have to take the vehcategory into account.

If you need me to write down the columns etc then let me know, I havnt found a quick way to do that yet.

use Dictionary

SELECT TblWords.ClientCodeWordPosition,TblWords.ClientCode, TblWords.Word, [TblVehCat].ClientCode, TblVehCat.VehCategory 
FROM TblWords LEFT JOIN TblWordTags ON TblWords.ClientCodeWordPosition = TblWordTags.ClientCodeWordPosition  LEFT JOIN [TblVehCat] ON [TblWords].ClientCode = [TblVehCat].ClientCode
WHERE  (((TblWordTags.ClientCodeWordPosition) Is Not Null)) and ((TblWords.Word='BM'))  And ((TblVehCat.VehCategory ='Motorcycle' ))
ORDER BY WordPosition;

Open in new window

ee.JPG
ee2.JPG
0
Comment
Question by:PeterBaileyUk
  • 4
  • 2
7 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41736635
Give this a whirl..
CREATE PROC yourproc(@word varchar(10), @vehCategory varchar(10)) AS

-- Meaningful code comments here

DELETE tblWords
FROM tblWords w
	LEFT JOIN TblWordTags wt ON w.ClientCodeWordPosition = wt.ClientCodeWordPosition  
	LEFT JOIN TblVehCat vc ON w.ClientCode = vc.ClientCode
WHERE  wt.ClientCodeWordPosition Is Not Null and w.Word=@word  And vc.VehCategory =@vehCategory

GO

Open in new window


  • Note the use of table aliases for easier readability
  • Deleted unnecessary parentheses marks and square brackets
  • The Delete with a JOIN syntax looks odd at first, but you can google for many descriptions
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41736661
The solution is fine.

I just want to add a general recipe for turning a SELECT query to a DELETE query. The main idea is, you only need to know IDs of records to be able to delete the rows you want to delete and only those.

Step 1: Modify the select field list to only contain the main tables ID.
Step 2: DELETE FROM theTable WHERE id in (SELECT id FROM theTable ...joins... where...)

Especially if you only have few rows to delete this is a) easy to do and b) fast enough.

Deleting many rows has other rules to look for, eg process that in smaller batches of rows only.

Bye, Olaf.
0
 

Author Comment

by:PeterBaileyUk
ID: 41737018
Could I just check will this delete words in TblWordTags, that was the target table for row deletion
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:PeterBaileyUk
ID: 41749927
referring to ID: 41736635 I wanted to delete the words that appear in tblwordtags I think this code will delete from tblwords.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41750398
>I wanted to delete the words that appear in tblwordtags
You need to be clear in your terms.
  • DELETE means to remove a row from a table
  • UPDATE means change a value to something else, maybe in your case to make it NULL or an empty string '', and to not delete/remove the row.

Which do you want to do?
0
 

Author Comment

by:PeterBaileyUk
ID: 41753442
Ok I sat down and learnt by your example, here is the code that works, you are right I thought i had said which table to delete from but the sentence could have meant anything, looking back:

USE Dictionary
GO
CREATE PROCEDURE [dbo].[usp_DeleteTagWordsVcat]
(
@Word varchar(100),
@VCat varchar(50)
)
AS
BEGIN
DELETE TblWordTags
FROM TblWordTags wt
	LEFT JOIN TblWords w ON w.ClientCodeWordPosition = wt.ClientCodeWordPosition  
	LEFT JOIN TblVehCat vc ON w.ClientCode = vc.ClientCode
WHERE  w.ClientCodeWordPosition is not null And w.Word =@word And vc.VehCategory =@VCat
END

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 41753444
I havnt marked mine as the best as that is petty, your example allowed me to sit down and understand what was going on and as my question wasnt so clear in my book that makes yours right. So thanks again for your assistance.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now