Click here to Skip to main content
16,013,322 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello frnds,
SQL
CREATE PROCEDURE [dbo].[usp_DeleteDrafsMessages]
(
@DraftsDelID VARCHAR(500)
)
AS
BEGIN 
	IF (@DraftsDelID <> '')
	BEGIN 
		DELETE FROM tblMessage where MessageID= dbo.udfSplit(@DraftsDelID,',')
		
	END

END  

I want to delete multiple number of rows in once ,i have number of id in my string
@DraftsDelID,i willl get each id by dbo.udfSplit(@DraftsDelID,','),but my above code is not feasible ,plz help me out fast !

[Digimanus: added PRE tags]
Posted
Updated 6-Mar-12 23:35pm
v3
Comments
Herman<T>.Instance 7-Mar-12 5:40am    
Your biggest problem is dat MessageID is not unique. Does table tblMessage have a unique field?

Hello frnds,
Solution:-:)
CREATE PROCEDURE [dbo].[usp_DeleteDrafsMessages]
(
@DraftsDelID VARCHAR(500)
)
AS
BEGIN
IF (@DraftsDelID <> '')
BEGIN
DELETE FROM tblMessage where MessageID in (select ListMember from dbo.udfSplit(@DraftsDelID,','))

END

END
:)
 
Share this answer
 
v2
The structure to delete double or more occurences is to number the occurences.
You miss a real unique field that you should add in the example I give below.
SQL
CREATE PROCEDURE [dbo].[usp_DeleteDrafsMessages]
(
@DraftsDelID VARCHAR(500)
)
AS
BEGIN 
	IF (@DraftsDelID <> '')
	BEGIN 
		Declare @MessageID varchar(max)
		set @MessageID = dbo.udfSplit(@DraftsDelID,',')
		SELECT Rowno,MessageID, UniqueID
		INTO #TEMP
		FROM
		(
			SELECT Row_Number() over (partition by MessageID order by MessageID) as Rowno, MessageID, UniqueID
			FROM tbl_Message
		) as t
		
		DELETE FROM tblMessage where UniqueID in (select UniqueID from #temp where Rowno > 1)
		DROP TABLE #temp
	END
 
END
 
Share this answer
 
v2
Try something along these lines:
SQL
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab (id int not null)
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0 
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab (id) VALUES (@VALUE)
END
DELETE FROM myTable WHERE id IN (SELECT id FROM #tempTab)
DROP TABLE #tempTab
 
Share this answer
 
Comments
JayantiChauhan 7-Mar-12 5:49am    
MessageID is unique !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900