Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create function to concatenate unknown number of columns in sql server 2008

Ex:
if input is:
id        Name        family
1         a           b

output:
id=1,name=a,family=b

and if input is:
id        postalCode        address      telephone
1         111               ddd         12

output:
id=1,postalCode=111,address=ddd,telephone=12


how Can I do it?????
Posted
Updated 22-Feb-15 10:00am
v2
Comments
Kuthuparakkal 22-Feb-15 13:36pm    
Please check my solution

Try this:
SQL
CREATE TABLE [dbo].[UserInfo](
    [Id] INT IDENTITY (1,1),
    [Name] [nvarchar](256) NOT NULL,
    [Family] [nvarchar](256) NOT NULL,
    [PostalCode] [nvarchar](256) NOT NULL,
    [Address] [nvarchar](256) NOT NULL,
    [Telephone] [nvarchar](256) NOT NULL,
)


INSERT [dbo].[UserInfo]
SELECT  'Kuthuparakkal', 'KU', '30084', 'New York', '078-211-0000'
UNION
SELECT  'Uthuparakkal', 'UT', '30074', 'Old York', '068-211-0000'
UNION
SELECT 'Thuparakkal', 'TH', '30064', 'Bad York', '058-211-0000'
UNION
SELECT  'Huparakkal', 'HU', '30054', 'Better York', '048-211-0000'
UNION
SELECT  'Uparakkal', 'UP', '30044', 'Best York', '038-211-0000'
UNION
SELECT 'Parakkal', 'PA', '30034', 'Worst York', '028-211-0000'



GO


DECLARE @Query NVARCHAR(MAX)
SELECT @Query = COALESCE(@Query + '+ ', '') +
    ISNULL(Name, 'N/A')
FROM
(
SELECT ''',' + name + '=''+' + ' CAST(ISNULL([' + name + '], ''<Empty>'') AS NVARCHAR(300)) '  AS Name
FROM sys.columns WHERE object_id = OBJECT_ID('[dbo].[UserInfo]')
)AS X


SET @Query = 'SELECT ''' + SUBSTRING(@Query,3, LEN(@Query)) + 'FROM [dbo].[UserInfo] WHERE [Id] = '

PRINT @Query

DECLARE @Rows INT = (SELECT COUNT(1) FROM [dbo].[UserInfo])

DECLARE @QueryResults AS TABLE
(
Result NVARCHAR(MAX)
)

DECLARE @TempQuery NVARCHAR(MAX)

WHILE(@Rows > 0)
BEGIN

    SET @TempQuery = @Query + ''''+ CAST(@Rows AS NVARCHAR(300)) + ''''
    INSERT @QueryResults
    EXEC (@TempQuery)
SET @Rows = @Rows -1
END


SELECT * FROM @QueryResults


Thanks,

Kuthuparakkal
 
Share this answer
 
Comments
Wendelius 22-Feb-15 23:21pm    
I take it this is to concatenate all columns for a table and for all rows. Looks interesting.

I think you should have a cursor or something similar when looping through the table. Id's and row count do not necessarily match :)
Kuthuparakkal 23-Feb-15 6:45am    
Assumed no-gaps. In fact you need to create a table variable (SI, Id) and iterate it and pick Id
milad.z 2-Mar-15 15:15pm    
It was grate
You solved my problem
but dont like to use any field name in my query. you use Id Field name in your query:
SET @Query = 'SELECT ''' + SUBSTRING(@Query,3, LEN(@Query)) + 'FROM [dbo].[UserInfo] WHERE [Id] = '
If you're using SQL Server 2012 or above you can use the built-in CONCAT[^] since it accepts varying amount of parameters.

EDIT:

If you're using older versions then why not use concatenation operator +

For example
SQL
SELECT 'id=' + id + ', Name=' + Name + ', family=' + family
...
 
Share this answer
 
v2
Comments
Kuthuparakkal 22-Feb-15 13:36pm    
Pls check my answer
Wendelius 22-Feb-15 23:15pm    
Solution updated for older versions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900