Author |
Topic |
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2014-01-17 : 14:21:01
|
Hi AllDont know whether this is something possible but want to bring it up to the experts for some adviseI have a table A where we store all the requests submitted by customers . Few requests are recurring every year . So we started a new process where we create a draft request and save in Table B for the requestors conveneince . this draft request will be an exact copy of the previous year request with a new requestid . we save the previous year requestid in a column called prevrequestid to track and when this happens for a period of time how do i track all the requests associated to the original requestidSample Data Requestid PrevRequestidRequest Submitted in 2009 - 123456 NULLRequest Submitted in 2010 - 123457 123456Request Submitted in 2011 - 123458 123457Request Submitted in 2012 - 123459 123458Request Submitted in 2013 - 123460 123459Request Submitted in 2013 - 123461 123460 when we create a draft requestid in Table B we take the current year request to create draft request for the next year.when the requestor submits the draft it gets into the Table AMy query should be able to pull all the requests so for example if i give select * from Table A where requestid is 123456 it should be able to identify all the connected requests and be able to pull everything Is this something possible or what should i do to make it possible?Thanks |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2014-01-17 : 18:47:07
|
You could use a recursive CTE. BOL has details. I'd give some code but I don't have your schema.HTH=================================================A man is not old until regrets take the place of dreams. - John Barrymore |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-18 : 04:39:04
|
something like thisDECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestID)SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2014-01-21 : 09:51:02
|
thanks Visakh . tested your code and ran into an issueI submitted 4 test requests each using the previous request for examplefirst requestid is 149490Second requestid is 149551 submitted using 149490third requestid is 149564 submitted using 149551fourth requestid is 149565 subitted using 149564When i input 149565 in line 2 of your code which is the last request submitted i get all the previous requests but when i input 149490 or 149551 i dont get all of themHope i made senseThanksquote: Originally posted by visakh16 something like thisDECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestID)SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-22 : 06:42:09
|
quote: Originally posted by jim_jim thanks Visakh . tested your code and ran into an issueI submitted 4 test requests each using the previous request for examplefirst requestid is 149490Second requestid is 149551 submitted using 149490third requestid is 149564 submitted using 149551fourth requestid is 149565 subitted using 149564When i input 149565 in line 2 of your code which is the last request submitted i get all the previous requests but when i input 149490 or 149551 i dont get all of themHope i made senseThanksquote: Originally posted by visakh16 something like thisDECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestID)SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
do you mean even if youstart from intermediate request you need to get all on both directtions?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-22 : 06:43:56
|
something like this?DECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.PrevRequestID = c.RequestIDWHERE NOT EXISTS (SELECT 1 FROM CTE WHERE RequestID = t.RequestID AND PrevRequestID = t.PrevRequestID))SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2014-01-22 : 09:14:30
|
VisakhGet the below error messageMsg 253, Level 16, State 1, Line 3Recursive member of a common table expression 'CTE' has multiple recursive references.quote: Originally posted by visakh16 something like this?DECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.PrevRequestID = c.RequestIDWHERE NOT EXISTS (SELECT 1 FROM CTE WHERE RequestID = t.RequestID AND PrevRequestID = t.PrevRequestID))SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-23 : 05:57:38
|
what about this?DECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestIDOR t.PrevRequestID = c.RequestID)SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2014-01-23 : 08:16:00
|
The query runs endlessly and is returning the same records multiple timesThanksquote: Originally posted by visakh16 what about this?DECLARE @RequestID int SET @RequestID = 123456 ;With CTEAS(SELECT *FROM Table WHERE RequestID = @RequestIDUNION ALLSELECT t.*FROM CTE cINNER JOIN Table tON t.RequestID = c.PrevRequestIDOR t.PrevRequestID = c.RequestID)SELECT *FROM CTEOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-24 : 00:55:40
|
ok here you go with logic to get both side infodeclare @test table(Requestid int, PrevRequestid int ) insert @testvalues(123456, NULL),(123457, 123456),(123458, 123457),(123459, 123458),(123460, 123459),(123461, 123460)declare @StartID int=123459;With CTEAS(SELECT *FROM @testWHERE Requestid = @StartIDUNION ALLSELECT t.*FROM CTE cJOIN @test tON t.Requestid = c.PrevRequestid),CTE1AS(SELECT *FROM @testWHERE PrevRequestid = @StartIDUNION ALLSELECT t.*FROM CTE1 cJOIN @test tON t.PrevRequestid = c.Requestid)SELECT *FROM CTEUNION ALLSELECT *FROM CTE1OPTION (MAXRECURSION 0)Requestid PrevRequestid123459 123458123458 123457123457 123456123456 NULL123460 123459123461 123460 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2014-01-24 : 08:32:09
|
Thanks so much Visakh . It worked |
|
|
|