Solved

how to delete rows from table using table type

Posted on 2014-07-25
2
145 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
[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
2 Comments
 
LVL 70

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

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!

Question has a verified solution.

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

Suggested Solutions

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.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

751 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