Solved

how to delete rows from table using table type

Posted on 2014-07-25
2
127 Views
Last Modified: 2014-07-31
I have two tables from which I need to remove rows.

I have created a table type which I want to use in SP to remove rows from these two table.

But I am not able to figure out how to use this table type  in the delete query.
0
Comment
Question by:yadavdep
2 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40220103
not really sure to fully understand what you want but would it be:

delete
from Table1 as T1
inner join table2 as T2
on t2.key = t1.key
and t2.tabletypefield = 'valueyouwanttodelete'
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 140 total points
ID: 40220185
If table type is used to decide which table to delete from, then you either will need to write Dynamic SQL (NOT recommended for this), or trigger two delete queries from one stored procedure, based on the table type value, something like this:

CREATE PROCEDURE deleteSomething 
	@tableType varchar(1),
	@itemID int
AS
IF @tableType = 'A'
BEGIN
	DELETE 
	FROM MyTableA
	WHERE itemID = @itemID
END
ELSE IF @tableType = 'Z'  -- always use an IF test. This allows for future expansion
BEGIN
	DELETE
	FROM MyTableZ
	WHERE itemID = @itemID
END

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

932 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