SQl Query
-
Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
You have to use cursor. check this.[^]
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
Following one of the solution work for you but there must be better solution than this SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date, ,> -- Description: <Description, ,> -- ============================================= CREATE FUNCTION tableaValue ( @id int ) RETURNS varchar(50) AS BEGIN declare @name varchar(50) declare @tempString varchar(50) DECLARE tablea_cursor CURSOR FOR select name from tablea where id = @id -- Variable value from the outer cursor OPEN tablea_cursor FETCH NEXT FROM tablea_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @tempString= isnull(@tempString,'') + ',' + @name FETCH NEXT FROM tablea_cursor INTO @name END CLOSE tablea_cursor DEALLOCATE tablea_cursor -- Return the result of the function RETURN @tempString END GO select dbo.tableaValue(id),id from tablea group by id
-
You have to use cursor. check this.[^]
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
Thanks. I have used for Xml Path and I have done it in SQl 2005. but i want to do it only with pure T-Sql which I need the same thing in Ms Access also. any ideas for Ms Access 2003/2007
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Following one of the solution work for you but there must be better solution than this SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date, ,> -- Description: <Description, ,> -- ============================================= CREATE FUNCTION tableaValue ( @id int ) RETURNS varchar(50) AS BEGIN declare @name varchar(50) declare @tempString varchar(50) DECLARE tablea_cursor CURSOR FOR select name from tablea where id = @id -- Variable value from the outer cursor OPEN tablea_cursor FETCH NEXT FROM tablea_cursor INTO @name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @tempString= isnull(@tempString,'') + ',' + @name FETCH NEXT FROM tablea_cursor INTO @name END CLOSE tablea_cursor DEALLOCATE tablea_cursor -- Return the result of the function RETURN @tempString END GO select dbo.tableaValue(id),id from tablea group by id
-
You dont even need the cursor to build a CSV, you can use COALESCE
DECLARE @nameCsv VARCHAR(MAX)
SET @nameCSV = ''SELECT @nameCsv = COALESCE(name + ',','') + @nameCsv
FROM tableaSELECT LEFT(@nameCsv,LEN(@nameCsv)-1) AS NameCsv
its good but it nned it id wise see the question he need csv value which group by id i hope this will on id basis thanks for reply
-
its good but it nned it id wise see the question he need csv value which group by id i hope this will on id basis thanks for reply
-
Dear All, I am looking to query followings in Sql 2005 table as Table A ID Name 1 A 1 B 1 C 2 D 2 E 2 F I want the result as ID Name 1 A,B,C 2 D,E,F any ideas
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Thanks for reply. the query returns my table as it is, not the output which I am searching for
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Thanks for reply. the query returns my table as it is, not the output which I am searching for
Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan