Solved

delete query in sql server express

Posted on 2016-07-31
7
55 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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