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 |
|
spencer
Starting Member
5 Posts |
Posted - 2008-05-06 : 05:25:46
|
Hi Guys,I have to work with a poorly designed table :(, that has columns ID INT, thisID varchar(50) null, parentID varchar(50) null, Title varchar(255) null, Description varchar(8000) null, ProductType varchar(255) null,The reason it is poorly designed is the table is used to hold questions and answers, all with a 1:1 relationship. Instead of having ID, ProductType, Question, Answer they have unfortunately adopted the approach of the above i.e:id 1thisID 3parentid nuLLDESCRIPTION: this is a questionid 20thisID 3_1parentID 3DESCRIPTION: this is the answer to the question aboveSo I am writing a sproc that does this using a temp table. I got this far:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Spencer-- =============================================ALTER PROCEDURE [dbo].[GetFAQs] -- Add the parameters for the stored procedure here @ProductType varchar(255)ASBEGIN SET NOCOUNT ON; -- Insert statements for procedure hereCREATE TABLE TEMP( ID INT, thisID varchar(50) null, parentID varchar(50) null, Title varchar(255) null, DescriptionQ varchar(8000) null, DescriptionA varchar(8000) null, ProductType varchar(255) null,)SELECT ID, thisID, parentID, Title, DescriptionQ, DescriptionA, ProductTypeFROM A2ZWHERE ProductType = @ProductType AND parentID IS NULL ENDGOThis gets all my questions for that product type.What I need to do is load the questions into my temp table and then run through the a2z table again gaining the answers to the questions (the parentid holds the question ID). The answers then will also get loaded into the temp table.Any bright sparks out there that can help me?Cheers |
|
|
Vadivu
Starting Member
31 Posts |
Posted - 2008-05-06 : 05:41:37
|
| CREATE TABLE #TEMPUS(ID INT,thisID varchar(50) null,parentID varchar(50) null,Title varchar(255) null,DescriptionQ varchar(8000) null,DescriptionA varchar(8000) null,ProductType varchar(255) null,)INSERT INTO #TEMPUS(ID ,thisID ,parentID ,Title ,DescriptionQ ,DescriptionA ,ProductType)selectID,thisID,parentID,Title,Description,NULL,ProductTypeFROMA2ZWHEREProductType = 1 AND parentID IS NULL declare @qid varchar(50)set @qid = (select thisID from #TEMPUS)insert into #TEMPUS(ID ,thisID ,parentID ,Title ,DescriptionQ ,DescriptionA ,ProductType )select ID,thisID,parentID,Title,@qid,Description,ProductTypeFROMA2ZWHEREProductType = 1 AND parentID =@qidselect * from #TEMPUSCHeck and tell me if this is the way u need the ouput |
 |
|
|
spencer
Starting Member
5 Posts |
Posted - 2008-05-06 : 06:33:51
|
| Thanks alot.The error meesage I get is:Msg 512, Level 16, State 1, Procedure GetFAQs, Line 54Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.However, with further investigation the upper part of the query works, namely:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Spencer Barriball-- Create date: 06/05/08-- Description: Gets questions and assoicated answers -- from the a2z table to match product type -- =============================================ALTER PROCEDURE [dbo].[GetFAQs] -- Add the parameters for the stored procedure here @ProductType varchar(255)ASBEGIN SET NOCOUNT ON; -- Insert statements for procedure hereCREATE TABLE #TEMPUS( ID INT, thisID varchar(50) null, parentID varchar(50) null, Title varchar(255) null, DescriptionQ varchar(3500) null, DescriptionA varchar(3500) null, ProductType varchar(255) null, --note varchar)INSERT INTO #TEMPUS( ID , thisID , parentID , Title , DescriptionQ , DescriptionA , ProductType)select ID, thisID, parentID, Title, Description, NULL, ProductTypeFROM A2ZWHERE ProductType = @ProductType AND parentID IS NULL AND Description IS NOT NULLThis gets me this table ( just a little of teh table shown here as the actual table is large):id, thisid, parentid, title, descriptionq, descriptiona, producttype1 Panel1 NULL facilities <B>FACILITIES</B> NULL euparks2 Panel3 NULL alterations to bookings <B>ALTERATIONS TO BOOKINGS</B> NULL euparks3 Panel4 NULL arrival and departure <B>ARRIVAL AND DEPARTURE</B> NULL euparks4 Panel5 NULL bed linen and towels <B>BED LINEN & TOWELS</B> NULL euparks5 Panel6 NULL cancellations <B>CANCELLATIONS</B> NULL euparks-------------------------------------------------When adding:DECLARE @qid varchar(50)SET @qid = (SELECT thisID FROM #TEMPUS)-----INSERT INTO #TEMPUS( DescriptionA )SELECT DescriptionFROM A2ZWHERE parentID = @qid AND Description IS NOT NULLto the end of the query i get the error message shown at start.Thank you very much for your help so far, any ideas? |
 |
|
|
Vadivu
Starting Member
31 Posts |
Posted - 2008-05-06 : 06:45:31
|
| i thought there will be only only one question id for one producttype... made a few changes to my original query... pls make the necessary changes, try it out and tell meCREATE TABLE #TEMPUS(rowId INT IDENTITY(1,1),ID INT,thisID varchar(50) null,parentID varchar(50) null,Title varchar(255) null,DescriptionQ varchar(8000) null,DescriptionA varchar(8000) null,ProductType varchar(255) null,)INSERT INTO #TEMPUS(ID ,thisID ,parentID ,Title ,DescriptionQ ,DescriptionA ,ProductType)selectID,thisID,parentID,Title,Description,NULL,ProductTypeFROMA2ZWHEREProductType = 1 AND parentID IS NULL DECLARE @curRow INT DECLARE @finalRow INTdeclare @qid varchar(50)SET @curRow = 1 SET @finalRow = (select count(*) from #TEMPUS) IF(@finalRow > 0) BEGIN WHILE (@curRow <= @finalRow) BEGINset @qid = (select thisID from #TEMPUS WHERE rowId = @curRow)insert into #TEMPUS(ID ,thisID ,parentID ,Title ,DescriptionQ ,DescriptionA ,ProductType )select ID,thisID,parentID,Title,@qid,Description,ProductTypeFROMA2ZWHEREProductType = 1 AND parentID =@qidSET @curRow = @curRow +1 ENDENDselect * from #TEMPUSdrop table #TEMPUS |
 |
|
|
spencer
Starting Member
5 Posts |
Posted - 2008-05-06 : 07:02:48
|
Thanks again, almost there I now recieve (again a much shorter version):1 1 Panel1 NULL facilities <B>FACILITIES</B> NULL euparks2 2 Panel3 NULL alterations to bookings <B>ALTERATIONS TO BOOKINGS</B> NULL euparks3 3 Panel4 NULL arrival and departure <B>ARRIVAL AND DEPARTURE</B> NULL euparks....47 7 Panel1_1 Panel1 NULL Panel1 Whilst we have done our utmost to ensure that the facilities ... inform you. euparks48 8 Panel3_1 Panel3 NULL Panel3 Once a booking has been confirmed ... ticket. euparks49 9 Panel4_1 Panel4 NULL Panel4 Start days are clearly ... confirmation. euparksWhat I require is on one row the question (descriptionq) AND the answer (descriptiona), so to show what i mean from above:1 1 Panel1 NULL facilities <B>FACILITIES</B> we have done our utmost to ensure that the facilities ... inform you. euparksSo each one row corresponds to a question AND its assiocated answer.Thanks alot |
 |
|
|
Vadivu
Starting Member
31 Posts |
Posted - 2008-05-06 : 07:19:16
|
| will there be only one answer for one question? |
 |
|
|
spencer
Starting Member
5 Posts |
Posted - 2008-05-06 : 07:21:30
|
quote: Originally posted by Vadivu will there be only one answer for one question?
Yes there will be |
 |
|
|
Vadivu
Starting Member
31 Posts |
Posted - 2008-05-06 : 07:24:01
|
| then i guess, instead of the insert statement within the while loop, the following update statement can be used.update #TEMPUSset DescriptionA =(select Description FROMA2ZWHEREProductType = 1 AND parentID =@qid)where thisid=@qid |
 |
|
|
spencer
Starting Member
5 Posts |
Posted - 2008-05-06 : 07:42:44
|
| Thank you very much, the issue was solved by Vadivu. The final solution (for achieve purposes) is:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Spencer -- =============================================ALTER PROCEDURE [dbo].[GetFAQs] -- Add the parameters for the stored procedure here @ProductType varchar(255)ASBEGIN SET NOCOUNT ON; -- Insert statements for procedure hereCREATE TABLE #TEMPUS( rowId INT IDENTITY(1,1), ID INT, thisID varchar(50) null, parentID varchar(50) null, Title varchar(255) null, DescriptionQ varchar(8000) null, DescriptionA varchar(8000) null, ProductType varchar(255) null,)INSERT INTO #TEMPUS( ID , thisID , parentID , Title , DescriptionQ , DescriptionA , ProductType)select ID, thisID, parentID, Title, Description, NULL, ProductTypeFROM A2ZWHERE ProductType = @ProductType AND parentID IS NULL DECLARE @curRow INTDECLARE @finalRow INTdeclare @qid varchar(50)SET @curRow = 1SET @finalRow = (select count(*) from #TEMPUS)IF (@finalRow > 0)BEGIN WHILE (@curRow <= @finalRow)BEGINset @qid = (select thisID from #TEMPUS WHERE rowId = @curRow)update #TEMPUSset DescriptionA =(select Description FROMA2ZWHEREProductType = @productType AND parentID =@qid)where thisid=@qidSET @curRow = @curRow +1ENDENDselect * from #TEMPUSdrop table #TEMPUSENDGO |
 |
|
|
Vadivu
Starting Member
31 Posts |
Posted - 2008-05-06 : 07:55:31
|
| i started with the procedure u posted, and also was confused ( i also missed that it was 1:1 relationship)... but i guess this can be done in a simple way... try this simple query alsoselect q.id, q.thisid, q.title, q.description as question,a.description as answer, q.producttypefrom a2z q inner join a2z a on q.thisid = a.parentidwhere q.producttype=@productType |
 |
|
|
|
|
|
|
|