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.
| 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 tableIf 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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, |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-08-12 : 12:01:05
|
AHHHH I get it I thinkSo 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 withResp_Code | Conflicts________B______|______D______________C______|______A__________________D______|______B_________ CorrecT?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
osirisa
Constraint Violating Yak Guru
289 Posts |
Posted - 2009-08-12 : 12:04:55
|
| Bingo !!!!! CORRECT !!! |
 |
|
|
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]GOCREATE 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 RETURNENDGODECLARE @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 changesSELECT @conflicts = [conflicts] FROM @foo WHERE [resp_code] = @targetRespSELECT @conflictsUPDATE 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] = @targetRespSELECT * FROM @foo Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
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.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|
|
|
|
|