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 3309852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:41:24+00:00 2026-05-17T21:41:24+00:00

I need to create a view from several tables. One of the columns in

  • 0

I need to create a view from several tables. One of the columns in the view will have to be composed out of a number of rows from one of the table as a string with comma-separated values.

Here is a simplified example of what I want to do.

Customers:
CustomerId int
CustomerName VARCHAR(100)

Orders:
CustomerId int
OrderName VARCHAR(100)

There is a one-to-many relationship between Customer and Orders. So given this data

Customers
1 'John'
2 'Marry'

Orders
1 'New Hat'
1 'New Book'
1 'New Phone'

I want a view to be like this:

Name     Orders
'John'   New Hat, New Book, New Phone
'Marry'  NULL

So that EVERYBODY shows up in the table, regardless of whether they have orders or not.

I have a stored procedure that i need to translate to this view, but it seems that you cant declare params and call stored procs within a view. Any suggestions on how to get this query into a view?

CREATE PROCEDURE getCustomerOrders(@customerId int)
AS
   DECLARE @CustomerName varchar(100)
   DECLARE @Orders varchar (5000)

   SELECT @Orders=COALESCE(@Orders,'') + COALESCE(OrderName,'') + ',' 
   FROM Orders WHERE CustomerId=@customerId

   -- this has to be done separately in case orders returns NULL, so no customers are excluded
   SELECT @CustomerName=CustomerName FROM Customers WHERE CustomerId=@customerId

   SELECT @CustomerName as CustomerName, @Orders as Orders
  • 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-17T21:41:25+00:00Added an answer on May 17, 2026 at 9:41 pm

    EDIT: Modified answer to include creation of view.

    /* Set up sample data */
    create table Customers (
        CustomerId int,
        CustomerName VARCHAR(100)
    )
    
    create table Orders (
        CustomerId int,
        OrderName VARCHAR(100)
    )
    
    insert into Customers
        (CustomerId, CustomerName)
        select 1, 'John' union all
        select 2, 'Marry'
    
    insert into Orders
        (CustomerId, OrderName)
        select 1, 'New Hat' union all
        select 1, 'New Book' union all
        select 1, 'New Phone'
    go
    
    /* Create the view */       
    create view OrderView as    
        select c.CustomerName, x.OrderNames
            from Customers c
                cross apply (select stuff((select ',' + OrderName from Orders o where o.CustomerId = c.CustomerId for xml path('')),1,1,'') as OrderNames) x
    go
    
    /* Demo the view */
    select * from OrderView
    go 
    
    /* Clean up after demo */
    drop view OrderView
    drop table Customers
    drop table Orders
    go
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a View that represents an x/y axis. I will read
I have a 'users' table with two columns, 'email' and 'new_email'. I need: A
I need to create an activity(view) that shows several lists, for example: (text)Fruits: (item)apple
I've to execute a complex query, selecting several columns from 7-8 tables. We don't
I have a log table which have several child tables inherit it 。but my
I need to create a custom view, which extends RelativeLayout and simply needs to
In my app, I create view controller objects as I need them. When a
I understand that we need to create MXML file to define a view. Suppose
UPDATED: I am using MySQL statement to create a view: I need to show
I have several places where I need to display an alert and handle the

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.