Solved

how to delete rows from table using table type

Posted on 2014-07-25
2
132 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

776 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