Author |
Topic |
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-20 : 03:05:59
|
Hi All Is it possible to return the rows as column... i think am confusing.. for examplei have a table with 2 columns col1 col2======================= 101 a 102 a 103 a 104 b 105 bi want to selct the col1 values if col2 value is 'a'select col1 from tbl where col2='a'it ll return o/p like this col1 =========== 101 102 103 but i dont want my o/p in this formati want display the result like thiscol1 col1 col1================================101 102 103is this possible...thats wat i told 'i want to display the result rows as columns'can anyone explain me how to do this.......Live while u r Alive |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-20 : 04:20:11
|
Where do you want to show the data?Use reports and its cross tab featuresRefer this an exampleDeclare @t table(num int)insert into @t select 101 union all select 102 union all select 103 Select num from @tDeclare @s varchar(8000)Select @s=COALESCE(@s+',','')+cast(num as varchar(10)) from @tExec('Select '+@s) Also referhttp://weblogs.sqlteam.com/jeffs/archive/2005/05.aspxMadhivananFailing to plan is Planning to fail |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-20 : 04:37:47
|
I want to get these result in Dataset and convert it to an XML file.. and i need to display all the columns with the same name...my XML format is like this<test> <date>10-02-2006</date> <time>11.00 AM</time> <tranCount>4</tranCount> <tranDetail> <tranID>001</tranID> <tranID>002</tranID> <tranID>003</tranID> <tranID>004</tranID> </tranDetail></test>i wrote that sample query for the <tranDetail> tablei have a problem converting the dataset to XML,becoz it displays the result like this <test> <date>10-02-2006</date> <time>11.00 AM</time> <tranCount>4</tranCount> <tranDetail> <tranID>001</tranID> </tranDetail></test> <tranDetail> <tranID>002</tranID> </tranDetail> <tranDetail> <tranID>003</tranID> </tranDetail> <tranDetail> <tranID>001</tranID> </tranDetail>this is because my SP returns the result as rows and my Dataset convert it as seperate table..so avoid this conflict i have to change my query to and get the results in a column format..But one thing every column shoul contain the same name like tranIDThanksRamLive while u r Alive |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-20 : 05:08:15
|
Thanks madhi ur query working fine.. But it returns the results without column nameThis is ur query result(3 row(s) affected)num ----------- 101102103(3 row(s) affected) ----------- ----------- ----------- 101 102 103but i need to show the column name(same).. so that XML file will be in the corrct formati try to give the column name by using 'as' alias but it throws an error.. where do i have to give so that it ll worktranID tranID tranID----------- ----------- ----------- this is the expected format101 102 103Thanks=====================RamLive while u r Alive |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-20 : 05:43:19
|
Modified from Madhivanan's codedeclare @t table(num int)insert into @t select 101 union all select 102 union all select 103 select num from @tdeclare @s varchar(8000)select @s = COALESCE(@s + ',', '') + cast(num as varchar(10)) + ' as tranID'from @texec('select '+@s) KH |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-20 : 06:53:35
|
Thanks Tan i got the expected results... now i am able to generate the structured XML..Thank u guys..=====================RamLive while u r Alive |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-20 : 07:45:49
|
Tan, your suggestion gives the column name TranId for all the columns. You cant retrieve the value by specifying column name in recordset in front end application. Rs("Tranid") will lead to error as there are more rows with the same column name. Instead make them uniquedeclare @t table(num int)insert into @t select 101 union all select 102 union all select 103 select num from @tdeclare @s varchar(8000)select @s = COALESCE(@s + ',', '') + cast(num as varchar(10)) + 'col'+cast(num as varchar(10))from @texec('select '+@s) MadhivananFailing to plan is Planning to fail |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-20 : 09:28:12
|
Actually this is my initial post declare @t table(num int)insert into @t select 101 union all select 102 union all select 103 select num from @tdeclare @s varchar(8000)declare @n intselect @n = 1select @s = COALESCE(@s + ',', '') + cast(num as varchar(10)) + 'TranID'+cast(@n as varchar(10)), @n = @n + 1from @texec('select '+@s) but later noticed the expected result asquote:
tranID tranID tranID----------- ----------- ----------- this is the expected format101 102 103
And hence make the changes to same column name. KH |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-22 : 08:11:27
|
Hi all,Ram - Assuming I understand you correctly, I can't help but get the feeling that the approach you're using to your (overall) problem isn't the best. Take a look at the code below as an example alternative approach - it should jump straight to the XML you're after...--dataset dateformat mdydeclare @tran table (id int, dtm datetime)insert @tran select 1, '2006-10-02 11:00'declare @tranDetail table (tranid int, col1 int, col2 varchar(10))insert @tranDetail select 1, 101, 'a'union all select 1, 102, 'a' union all select 1, 103, 'a'union all select 1, 104, 'b'union all select 1, 105, 'b'--calculationselect 1 as tag, null as parent, convert(varchar(10), dtm, 110) as 'test!1!date!element', stuff(right(convert(varchar(20), dtm, 100), 7), 6, 0, ' ') as 'test!1!time!element', (select count(*) from @tranDetail where col2 = 'a') as 'test!1!tranCount!element', null as 'tranDetail!2', null as 'tranID!3'from @tranunion allselect 2 as tag, 1 as parent, null, null, null, null, nullunion allselect 3, 2, null, null, null, null, col1 from @tranDetail where col2 = 'a'for xml explicit--results---------------------------------<test> <date>10-02-2006</date> <time>11:00:00</time> <tranCount>3</tranCount> <tranDetail> <tranID>101</tranID> <tranID>102</tranID> <tranID>103</tranID> </tranDetail></test> Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-22 : 08:47:37
|
Ryan This is great stuff.... Can u xplain me how this query works.. it may be useful for many guys who r seeking the solution to XML problemI really appriciate this....=====================RamLive while u r Alive |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-22 : 08:53:48
|
Thanks Ram - glad you like it Take a look at 'EXPLICIT mode' in BOL - there's some detailed explanation of this kind of stuff in there. It's quite tricky to get your head around at first, but can be used to create any shape of XML document when you do Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-23 : 01:56:17
|
Good Stuff Ryan MadhivananFailing to plan is Planning to fail |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-25 : 05:09:09
|
Thanks for the linksMadhivananFailing to plan is Planning to fail |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-25 : 06:52:46
|
Hi all I have faced another one problem while executin this SQLXML query.My Xml output is a large file.It nearly contains more than 500 tranIDs. but while executing this script only within 20 TranIDs are listed.Remainin are truncated.is there any restriction in string to hold the length of data (any restiction in size) ,becaus XML query returns the XMLoutput as string. half of the XML is getting truncated..Anyone know the solution for this...=====================RamLive while u r Alive |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-25 : 09:54:02
|
Ram - Where are you running the SQL from? If it's from Query Analyzer you won't be able to see more than 8000 characters. You'll need to do it from VB, say, as in the 15seconds article link you posted. That shouldn't have any size restrictions.Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-05-26 : 00:39:53
|
Thanks Ryan. I already got it.I was mistakenly post that query before accessin it through Frontend.. Now things are fine.. thanks for ur reply=====================RamLive while u r Alive |
|
|
raam_kimi
Yak Posting Veteran
80 Posts |
Posted - 2006-09-15 : 06:34:55
|
Hi All, I need help regarding this Open XML method.. i want to store the returned XML into the variable or temp table or table variable.. i tried this but i cant insert into an table variable.. it throws an error like this The FOR XML clause is not allowed in a INSERT statement... i dont want to return this back to front end.. my actual requirement is to pass this XML to another one SP as inputis this possible guyz...How can i do this....=====================Ramthink BIG |
|
|
|