Solved

delete query in sql server express

Posted on 2016-07-31
7
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 66

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

631 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