Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with stored proc

Author  Topic 

osirisa
Constraint Violating Yak Guru

289 Posts

Posted - 2009-08-12 : 11:26:49
Ok. I am going to make this explanation visual.


Resp_Code | Conflicts
________A______|_____B,D,C_____
________B______|______D,A______
________C______|______A__________
________D______|______A,B_________



Ok. Now I want to Delete the Resp Code "A" and all the Conflicts. In this case I already know how to delete B,D,C from that Resp_Code. I need the second part, I need to find Resp_Code B and delete conflicts "A", Find Resp_Code C and delete Conflicts "A", Find Resp_Code D and delete conflict A.


-----Easy summary---------

I need to mirror the delete. If I enter a record with certain conflicts, at the time of delete, I need to find where those conflicts where entered and delete them from the table.

-----------------------------
Remember all this are variables. ABCD are variables I assigning this values to make an easy representation. Any help is welcome.

Thank you.

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-12 : 11:51:00
So like a cascade delete... or recursive dekete...

So given that table

If you delete [resp_code] 'C' you should then delete [resp_code] 'A' and then delete [resp_codes] B,D,C (and so on until no conflicts exist?

Is that right?

So what datatypes are we dealing with here? Is the Conflicts column a comma separated string?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

osirisa
Constraint Violating Yak Guru

289 Posts

Posted - 2009-08-12 : 11:56:12
Resp_Code | Conflicts
________A______|_____B,D____
________B______|______D,A______
________C______|______A__________
________D______|______A,B_________

Ok. It's not quite a cascade delete. Maybe this example is more clear. For example First Row Resp_Code "A" has conflicts "B,D" I need to be able to update row B and delete Conflict with "A" and then update row D and delete conflict with "A". All because I delete all the conflicts on the First Row Resp_Code "A". I hope this better explain the dilema.

Thanks,
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-12 : 12:01:05
AHHHH I get it I think

So why don't we need to update row c? it says that it has a conflict with A also.

So if we were to do as you suggested we would end up with

Resp_Code | Conflicts
________B______|______D______
________C______|______A__________
________D______|______B_________

CorrecT?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

osirisa
Constraint Violating Yak Guru

289 Posts

Posted - 2009-08-12 : 12:04:55
Bingo !!!!! CORRECT !!!
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-12 : 12:28:42
OK -- I'd go for a string splitting function to return a table of [resp_code] from the conflicts. Then do an update to remove the original [Resp_code] from those rows. Finally do a delete for that [resp_code]

(I'm assuming that Resp_code is a primary key / unique)

so something like (using fn_split)


IF EXISTS (
SELECT * from dbo.sysobjects
WHERE
id = object_id(N'[dbo].[fn_Split]')
AND xtype IN (N'FN', N'IF', N'TF')
)
DROP FUNCTION [dbo].[fn_Split]
GO


CREATE FUNCTION fn_Split (@text VARCHAR(MAX), @delimiter VARCHAR(20) = ' ')

RETURNS @Strings TABLE (
position INT IDENTITY PRIMARY KEY
, value VARCHAR(8000)
)

AS BEGIN
DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0) BEGIN
-- Find the first delimiter
SET @index = CHARINDEX(@delimiter , @text)

-- No delimiter left?
-- Insert the remaining @text and break the loop
IF (@index = 0) AND (LEN(@text) > 0) BEGIN
INSERT INTO @Strings VALUES (@text)
BREAK
END

-- Found a delimiter
-- Insert left of the delimiter and truncate the @text
IF (@index > 1) BEGIN
INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
-- Delimiter is 1st position = no @text to insert
ELSE SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END
GO


DECLARE @targetResp CHAR(1) SET @targetResp = 'A'
DECLARE @conflicts VARCHAR(MAX)

DECLARE @foo TABLE (
[Resp_Code] CHAR(1) PRIMARY KEY
, [conflicts] VARCHAR(MAX)
)

INSERT @foo ([resp_code], [conflicts])
SELECT 'A', 'B,D,C'
UNION SELECT 'B', 'D,A'
UNION SELECT 'C', 'A'
UNION SELECT 'D', 'A,B'

SELECT * FROM @foo

-- Start the changes
SELECT @conflicts = [conflicts] FROM @foo WHERE [resp_code] = @targetResp
SELECT @conflicts

UPDATE f SET [conflicts] = REPLACE(REPLACE(REPLACE([conflicts], ',' + @targetResp, ''), @targetResp + ',', ''), @targetResp, '')
FROM
@foo f
JOIN (
SELECT
[value] AS [resp_code]
, [position] AS [pos]
FROM fn_Split(@conflicts, ',' )
)
u ON u.[resp_code] = f.[resp_code]

DELETE @foo WHERE [resp_code] = @targetResp

SELECT * FROM @foo



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

osirisa
Constraint Violating Yak Guru

289 Posts

Posted - 2009-08-12 : 12:30:04
WOW, CHARLIE, YOU ARE AWESOME...THANKS!!!!!! I WILL GIVE IT A TRY.
Go to Top of Page

osirisa
Constraint Violating Yak Guru

289 Posts

Posted - 2009-08-12 : 12:42:59
WOW, CHARLIE, I AM REALLY IMPRESSED, YOU ARE AWESON IN T-SQL.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-13 : 03:40:08
quote:
Originally posted by osirisa

WOW, CHARLIE, I AM REALLY IMPRESSED, YOU ARE AWESON IN T-SQL.



Thanks osirisa, I like it. If I ever get a custom title I will consider "AWESON IN T-SQL"

It's close to being pretentious but the complete fail "awesome" saves it!

All the best!


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-08-13 : 07:49:46
Sure you don't want "Enterpriseā„¢ SQL guy?"



http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-13 : 08:15:56
Thank you, kind sir, for that slur upon my reputation! WF really poisoned that word......



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-08-13 : 14:29:06
Its ok Charlie, i still respect you.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -