Author |
Topic |
Naveed88
Starting Member
19 Posts |
Posted - 2009-02-09 : 23:30:34
|
Hello every onei want to create csv of data in specific column in Child Tableand insert it into one cell in Master tableexampleMasterTableID name1 Naveed2 Deevan3 JitendraChildTableID FK Items1 1 902 1 913 2 924 2 935 3 946 3 957 1 96Then i want to query for this resultResultTableID name ItemPurchased1 Naveed 90,91,962 Deevan 92,933 Jitendra 94,95 Thanks for Help-- (¨`·.·´¨) Always`·.¸(¨`·.·´¨) Keep(¨`·.·´¨)¸.·´ Smiling!`·.¸.·´ & ProgrammingRegards...."Deevan" [Naveed Anjum]Web Developer9867374437-Mumbai.4 |
|
Padmaja
Starting Member
6 Posts |
Posted - 2009-02-09 : 23:55:03
|
DECLARE @MasterTable TABLE(ID INT,name VARCHAR(24))INSERT INTO @MasterTableSELECT 1, 'Naveed' UNION ALLSELECT 2, 'Deevan' UNION ALLSELECT 3, 'Jitendra'DECLARE @ChildTable TABLE(ID INT, FK INT, Items INT)INSERT INTO @ChildTableSELECT 1, 1, 90 UNION ALLSELECT 2, 1, 91 UNION ALLSELECT 3, 2, 92 UNION ALLSELECT 4, 2, 93 UNION ALLSELECT 5, 3, 94 UNION ALLSELECT 6, 3, 95 UNION ALLSELECT 7, 1, 96SELECT DISTINCT mt.id, mt.name, STUFF((SELECT DISTINCT ','+CAST(items AS VARCHAR(128)) FROM @ChildTable AS ct WHERE ct.fk = mt.id FOR XML PATH('')),1,1,'')FROM @MasterTable mtpadmaja |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-10 : 00:50:34
|
quote: Originally posted by Padmaja DECLARE @MasterTable TABLE(ID INT,name VARCHAR(24))INSERT INTO @MasterTableSELECT 1, 'Naveed' UNION ALLSELECT 2, 'Deevan' UNION ALLSELECT 3, 'Jitendra'DECLARE @ChildTable TABLE(ID INT, FK INT, Items INT)INSERT INTO @ChildTableSELECT 1, 1, 90 UNION ALLSELECT 2, 1, 91 UNION ALLSELECT 3, 2, 92 UNION ALLSELECT 4, 2, 93 UNION ALLSELECT 5, 3, 94 UNION ALLSELECT 6, 3, 95 UNION ALLSELECT 7, 1, 96SELECT DISTINCT mt.id, mt.name, STUFF((SELECT DISTINCT ','+CAST(items AS VARCHAR(128)) FROM @ChildTable AS ct WHERE ct.fk = mt.id FOR XML PATH('')),1,1,'')FROM @MasterTable mtpadmaja
This won't work in SQL 2000. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-10 : 00:53:53
|
[code]--Create Function Like This:CREATE FUNCTION [dbo].[UDFNumberList](@FK varchar(200))RETURNS varchar(5000) ASBEGINDECLARE @Number varchar(8000)SELECT @Number= COALESCE(@Number+',','') + convert(varchar(200),Items)FROM dbo.CHILDWHERE FK=@FKRETURN @NumberENDThen use like this:Select M.ID,M.Name,dbo.GetNumberList(FK) as NumberListfrom MASTER M Inner join CHILD Con M.ID = C.FKGroup by M.ID,M.NAME,C.FK[/code] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-10 : 02:24:28
|
quote: Originally posted by sodeep
--Create Function Like This:CREATE FUNCTION [dbo].[UDFNumberList](@FK varchar(200))RETURNS varchar(5000) ASBEGINDECLARE @Number varchar(8000)SELECT @Number= COALESCE(@Number+',','') + convert(varchar(200),Items)FROM dbo.CHILDWHERE FK=@FKORDER BY ItemsRETURN @NumberENDThen use like this:Select M.ID,M.Name,dbo.GetNumberList(FK) as NumberListfrom MASTER M Inner join CHILD Con M.ID = C.FKGroup by M.ID,M.NAME,C.FK
to get results in expected order |
|
|
Naveed88
Starting Member
19 Posts |
Posted - 2009-02-10 : 04:46:21
|
wowquit straight forward thanks padmajaand Sodeep also-- (¨`·.·´¨) Always`·.¸(¨`·.·´¨) Keep(¨`·.·´¨)¸.·´ Smiling!`·.¸.·´ & ProgrammingRegards...."Deevan" [Naveed Anjum]Web Developer9867374437-Mumbai.4 |
|
|
|
|
|