Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:51:31+00:00 2026-05-31T23:51:31+00:00

i have four tables like below in sql server 2008 : TABLE 1 ->

  • 0

i have four tables like below in sql server 2008 :

TABLE 1 -> Users

                     UserID        UserName
                     -----------------------
                       1              Jhon

TABLE 2 -> PhoneBook

                     PhonebookID        UserID            Name             MobileNumber
                     -------------------------------------------------------------------
                       1                  1               MyBrother              252848

TABLE 3 -> PhonebookExtraField

                     PhonebookExtraFieldID        UserID            ExtraFieldName    
                     -------------------------------------------------------------
                             1                      1               Age     
                             2                      1               Job
                             3                      1               Address    

TABLE 4 -> phoneBookExtraFieldData

phoneBookExtraFieldDataID    PhonebookExtraFieldID    PhonebookID    ExtraFieldValue    
-----------------------------------------------------------------------------------------
         101                             1                  1               30
         102                             2                  1               Web Developer
         103                             3                  1               A.V. Rose

how can i write a query for output below :
mean i am looking for a way for creating dynamic columns using Tables data…

UserName  Phonebook(Name)  Phonebook(MobileNumber)  Age       Job           Address
-------------------------------------------------------------------------------------
Jhon          MyBrother            252848            30    Web Developer    A.V. Rose

what is the best way for doing this job?
how should i change my tables for this purpose?
thanks for attention and advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:51:32+00:00Added an answer on May 31, 2026 at 11:51 pm

    Maybe something like this:

    Test data

    CREATE TABLE #User
    (
        UserID INT,
        UserName VARCHAR(100)
    )
    INSERT INTO #User
    VALUES(1,'Jhon')
    CREATE TABLE #PhoneBook
    (
        PhonebookID INT,
        UserID INT,
        Name VARCHAR(100),
        MobileNumber INT
    )
    INSERT INTO #PhoneBook
    VALUES(1,1,'MyBrother',252848)
    CREATE TABLE #PhonebookExtraField
    (
        PhonebookExtraFieldID INT,
        UserID INT,
        ExtraFieldName VARCHAR(100)
    )
    INSERT INTO #PhonebookExtraField
    VALUES(1,1,'Age'),(2,1,'Job'),(3,1,'Address')
    CREATE TABLE #PhoneBookExtraFieldData
    (
        PhoneBookExtraFieldDataID INT,
        PhonebookExtraFieldID INT,
        PhonebookID INT,
        ExtraFieldValue VARCHAR(100)
    )
    INSERT INTO #PhoneBookExtraFieldData
    VALUES(101,1,1,'30'),(102,2,1,'Web Developer'),(103,3,1,'A.V. Rose')
    

    The find the dynamic columns

    DECLARE @cols VARCHAR(MAX)
    SELECT  @cols = COALESCE(@cols + ','+QUOTENAME(ExtraFieldName),
                         QUOTENAME(ExtraFieldName))
    FROM 
        #PhonebookExtraField
    

    The execute some dynamic sql with a Pivot:

    DECLARE @query NVARCHAR(4000)=
    N'SELECT
        *
    FROM
    (
        SELECT
            #User.UserName,
            #PhoneBook.Name AS [Phonebook(Name)],
            #PhoneBook.MobileNumber AS [Phonebook(MobileNumber)],
            #PhonebookExtraField.ExtraFieldName,
            #PhoneBookExtraFieldData.ExtraFieldValue
        FROM
            #User
            JOIN #PhoneBook
                ON #User.UserID=#PhoneBook.UserID
            JOIN #PhonebookExtraField
                ON #PhoneBook.UserID=#PhonebookExtraField.UserID
            JOIN #PhoneBookExtraFieldData
                ON #PhonebookExtraField.PhonebookExtraFieldID=#PhoneBookExtraFieldData.PhonebookExtraFieldID
    ) AS p
    PIVOT
    (
        MAX(ExtraFieldValue)
        FOR ExtraFieldName IN('+@cols+')
    ) AS pvt'
    EXECUTE(@query)
    

    Then I will clean up after myself:

    DROP TABLE #User
    DROP TABLE #PhoneBook
    DROP TABLE #PhonebookExtraField
    DROP TABLE #PhoneBookExtraFieldData
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four tables: Users PrivilegeGroups rdPrivileges LinkPrivilege ----------- ---------------- --------------- --------------- userId(pk) privilegeGroupId(pk)
Using SQL Server 2005 I have a table with the following columns id name
I have four tables: characters arena_team arena_table_member arena_team_stats. characters table has guid, name arena_team
Say I have four tables, users , contacts , files , and userfiles .
I have four tables like the following : Cars: car_id | make_id 1 |
I have an sql query with inner joins of four tables that takes more
Ok, I have four tables: Table 1: f_withholdings Table 2: f_wh_list Table 3: f_rpayments
Imagine that I have UserId (actually we do have roughly four columns like userId,
I have a database column in a SQL Server 2008 database that contains VARCHAR
I have four Database Tables like these: Book ID_Book |ID_Company|Description BookExtension ID_BookExtension | ID_Book|

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.