Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete query in sql server express

Posted on 2016-07-31
7
Medium Priority
?
75 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

715 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