Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete query in sql server express

Posted on 2016-07-31
7
Medium Priority
?
81 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 66

Accepted Solution

by:
Jim Horn earned 2000 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 30

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 66

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

916 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