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

  • SEARCH
  • Home
  • 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 8625795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:52:05+00:00 2026-06-12T07:52:05+00:00

I am using SQL Server 2008. I have two tables User ( UserID, Name,

  • 0

I am using SQL Server 2008.

I have two tables

  1. User (UserID, Name, Link)
  2. UserNotes (NoteID, UserID, Title, Description)

This is the sample data structure.

INSERT INTO [User]
    ([UserID], [Name], [Link])
VALUES
    (1, 'John', 'L1'),
    (2, 'Steve', 'L234');

INSERT INTO [UserNotes]
    ([NoteID], [UserID], [Title], [Description])
VALUES
    (1, 1, 'AboutJohn', 'This is about john'),
    (2, 1, 'John Work', 'This is where John work'),
    (3, 1, 'John Education', 'This is the uni where John go'),
    (4, 2, 'Steve Note1', 'Des1 about Steve'),
    (5, 2, 'Steve Note2', 'Des2 about Steve');

Here is SQL Fiddle

I want to create view (User_view) as follows and when I execute this command the output should be as follows.

SELECT * FROM User_view WHERE UserID IN (1)

UserID    Name   AboutJOhn              JohnWork                  JohnEducation
1         John   This is about john     This is where Johnwork    This is the uni where John go

Title column of child table should become column name and Description should become value of that column and we do not know how many rows we will have. I am aware of the issue when we select two users and which name to use column name. In that case we can use (Note1, Note2, Note3, etc for multiple User), otherwise use title field as Column name. Is it possible to do so? Cheers!

  • 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-06-12T07:52:06+00:00Added an answer on June 12, 2026 at 7:52 am

    this is not a view.. This is a procedure which will return the result as you want

    you can call it as

    proc_UserNotes 1
    proc_UserNotes 2

    etc

    CREATE procedure proc_UserNotes (@UserID int)
    as
    begin
    
    DECLARE @cols AS NVARCHAR(MAX),
            @cols_WITH_MAX AS NVARCHAR(MAX),
            @query  AS NVARCHAR(MAX);
    
    select @cols_WITH_MAX = STUFF((SELECT distinct ',MAX(' 
                  + QUOTENAME(Title) +') AS ' + QUOTENAME(Title)
                        from [UserNotes] where UserID =@UserID
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Title) 
                        from [UserNotes] where UserID =@UserID
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')        
    
    
    set @query = 'SELECT [UserID], '+ @cols_WITH_MAX +' FROM(
                 SELECT [UserID], ' + @cols + ' from 
                (
                SELECT * FROM 
                [UserNotes]   where UserID ='+cast(@UserID as varchar(20)) +'
                )X
    
                pivot 
                (
                    MAX([Description])
                    for [Title] in (' + @cols + ')
                ) p )a GROUP BY [UserID]'
    
    
    print(@query)
    execute(@query)
    
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
I am using SQL Server 2008, and I have two tables Table1 contains 3.5
Using SQL Server 2008 R2 (Version 10.50.4000.0): I have two tables. I want to
Very new SQL Server, am using SQL server 2008. I have two tables, Table
I'm using SQL Server 2008. I have two tables like this: OrderItems OrderItemID InventoryItemID
I am using SQL Server 2008 R2. I have two tables, ShoppingList and ShoppingListFood
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I am using SQL Server 2008 R2. I have created two different databases Master
I have configured Service Broker communication between two SQL Server 2008 instances using Windows
I have SQL Server 2008 database with two tables. The first table is called

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.