I have a table tblCustomers in a one-to-many relationship with table
tblProducts.
What I want to do is to create a stored procudure that returns a list
of each customer in tblCustomers but also creates a field showing a
string (separated by commas)of each matching record in tblProducts.
So the return would look like:
CustID Customer ProductList
1 Smith Apples, Oranges, Pears
2 Jones Pencils, Pens, Paper
etc...
Instead of:
CustID Customer Product
1 Smith Apples
1 Smith Oranges
1 Smith Pears
2 Jones Pencils
2 Jones Pens
2 Jones Paper
Which is what you get with this:
SELECT tblCusomers.CustID, tblCusomers.Customer,
tblProducts.Product
FROM
tblCusomers INNER JOIN
tblProducts ON
tblCustomers.CustID = tblProducts.CustID
I'd appreciate any help!
lq 6 3263
"Lauren Quantrell" <la*************@hotmail.com> wrote in message
news:47**************************@posting.google.c om... I have a table tblCustomers in a one-to-many relationship with table tblProducts. What I want to do is to create a stored procudure that returns a list of each customer in tblCustomers but also creates a field showing a string (separated by commas)of each matching record in tblProducts.
So the return would look like: CustID Customer ProductList 1 Smith Apples, Oranges, Pears 2 Jones Pencils, Pens, Paper etc...
Instead of:
CustID Customer Product 1 Smith Apples 1 Smith Oranges 1 Smith Pears 2 Jones Pencils 2 Jones Pens 2 Jones Paper
Which is what you get with this:
SELECT tblCusomers.CustID, tblCusomers.Customer, tblProducts.Product FROM tblCusomers INNER JOIN tblProducts ON tblCustomers.CustID = tblProducts.CustID
I'd appreciate any help! lq
Generally the best way to do this would be in a front end application, where
it's easier to handle string manipulation. But this thread may be useful if
you have no other choice than to do it in MSSQL: http://tinyurl.com/bib2
Simon la*************@hotmail.com (Lauren Quantrell) wrote in message news:<47**************************@posting.google. com>... I have a table tblCustomers in a one-to-many relationship with table tblProducts. What I want to do is to create a stored procudure that returns a list of each customer in tblCustomers but also creates a field showing a string (separated by commas)of each matching record in tblProducts.
So the return would look like: CustID Customer ProductList 1 Smith Apples, Oranges, Pears 2 Jones Pencils, Pens, Paper etc...
Instead of:
CustID Customer Product 1 Smith Apples 1 Smith Oranges 1 Smith Pears 2 Jones Pencils 2 Jones Pens 2 Jones Paper
Which is what you get with this:
SELECT tblCusomers.CustID, tblCusomers.Customer, tblProducts.Product FROM tblCusomers INNER JOIN tblProducts ON tblCustomers.CustID = tblProducts.CustID
I'd appreciate any help! lq
You can try the following code in same sequence to get the string of
concatinated records
/*Temp table */
drop table tb_view
Create table dbo.tb_View
(
CustID int,
Customer varchar(20),
Product varchar(20)
)
INSERT INTO tb_View values (1,'Smith','Apples')
INSERT INTO tb_View values (1,'Smith','Oranges')
INSERT INTO tb_View values (1,'Smith','Pears')
INSERT INTO tb_View values (2,'Jones','Pencils')
INSERT INTO tb_View values (2,'Jones','Pens')
INSERT INTO tb_View values (2,'Jones','Paper')
/*Create a function to do the job*/
Create function dbo.fn_concatinate(@CustId as int) returns
varchar(100)
as
begin
declare @ret_value varchar(100)
SET @ret_value=''
Select @ret_value=@ret_value + ',' + Product FROM dbo.tb_View where
CustID=@CustId
RETURN RIGHT(@ret_value,LEN(@ret_value)-1)
end
/*Use function in query */
select CustID,Customer,dbo.fn_concatinate(CustID) from tb_View group
by CustID,Customer
Amit Gupta (am****@hotmail.com) writes: /*Create a function to do the job*/ Create function dbo.fn_concatinate(@CustId as int) returns varchar(100) as begin declare @ret_value varchar(100) SET @ret_value='' Select @ret_value=@ret_value + ',' + Product FROM dbo.tb_View where CustID=@CustId RETURN RIGHT(@ret_value,LEN(@ret_value)-1) end
Not that this function relies on undefined behaviour. It may return
the expected result, or it may return something else. See http://support.microsoft.com/default.aspx?scid=287515.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp
Amit,
Thank you for your examples. I realize there is still a lot to learn
for with the SQL. I have never used "Create function" and don't know
where it goes. Sorry for the ignorance...
lq am****@hotmail.com (Amit Gupta) wrote in message news:<6e**************************@posting.google. com>... la*************@hotmail.com (Lauren Quantrell) wrote in message news:<47**************************@posting.google. com>... I have a table tblCustomers in a one-to-many relationship with table tblProducts. What I want to do is to create a stored procudure that returns a list of each customer in tblCustomers but also creates a field showing a string (separated by commas)of each matching record in tblProducts.
So the return would look like: CustID Customer ProductList 1 Smith Apples, Oranges, Pears 2 Jones Pencils, Pens, Paper etc...
Instead of:
CustID Customer Product 1 Smith Apples 1 Smith Oranges 1 Smith Pears 2 Jones Pencils 2 Jones Pens 2 Jones Paper
Which is what you get with this:
SELECT tblCusomers.CustID, tblCusomers.Customer, tblProducts.Product FROM tblCusomers INNER JOIN tblProducts ON tblCustomers.CustID = tblProducts.CustID
I'd appreciate any help! lq You can try the following code in same sequence to get the string of concatinated records /*Temp table */ drop table tb_view Create table dbo.tb_View ( CustID int, Customer varchar(20), Product varchar(20) )
INSERT INTO tb_View values (1,'Smith','Apples') INSERT INTO tb_View values (1,'Smith','Oranges') INSERT INTO tb_View values (1,'Smith','Pears')
INSERT INTO tb_View values (2,'Jones','Pencils') INSERT INTO tb_View values (2,'Jones','Pens') INSERT INTO tb_View values (2,'Jones','Paper')
/*Create a function to do the job*/ Create function dbo.fn_concatinate(@CustId as int) returns varchar(100) as begin declare @ret_value varchar(100) SET @ret_value='' Select @ret_value=@ret_value + ',' + Product FROM dbo.tb_View where CustID=@CustId RETURN RIGHT(@ret_value,LEN(@ret_value)-1) end
/*Use function in query */ select CustID,Customer,dbo.fn_concatinate(CustID) from tb_View group by CustID,Customer
Specifically...
When I try to cretae a stored procedure containing this Create
Function, I get the error:
"You cannot chnage the object type in a script."
lq am****@hotmail.com (Amit Gupta) wrote in message news:<6e**************************@posting.google. com>... la*************@hotmail.com (Lauren Quantrell) wrote in message news:<47**************************@posting.google. com>... I have a table tblCustomers in a one-to-many relationship with table tblProducts. What I want to do is to create a stored procudure that returns a list of each customer in tblCustomers but also creates a field showing a string (separated by commas)of each matching record in tblProducts.
So the return would look like: CustID Customer ProductList 1 Smith Apples, Oranges, Pears 2 Jones Pencils, Pens, Paper etc...
Instead of:
CustID Customer Product 1 Smith Apples 1 Smith Oranges 1 Smith Pears 2 Jones Pencils 2 Jones Pens 2 Jones Paper
Which is what you get with this:
SELECT tblCusomers.CustID, tblCusomers.Customer, tblProducts.Product FROM tblCusomers INNER JOIN tblProducts ON tblCustomers.CustID = tblProducts.CustID
I'd appreciate any help! lq You can try the following code in same sequence to get the string of concatinated records /*Temp table */ drop table tb_view Create table dbo.tb_View ( CustID int, Customer varchar(20), Product varchar(20) )
INSERT INTO tb_View values (1,'Smith','Apples') INSERT INTO tb_View values (1,'Smith','Oranges') INSERT INTO tb_View values (1,'Smith','Pears')
INSERT INTO tb_View values (2,'Jones','Pencils') INSERT INTO tb_View values (2,'Jones','Pens') INSERT INTO tb_View values (2,'Jones','Paper')
/*Create a function to do the job*/ Create function dbo.fn_concatinate(@CustId as int) returns varchar(100) as begin declare @ret_value varchar(100) SET @ret_value='' Select @ret_value=@ret_value + ',' + Product FROM dbo.tb_View where CustID=@CustId RETURN RIGHT(@ret_value,LEN(@ret_value)-1) end
/*Use function in query */ select CustID,Customer,dbo.fn_concatinate(CustID) from tb_View group by CustID,Customer
In the DAH! Department...
I realize I have to do the Create Function in Enterprise Manager. I've
been using Microsoft Access MSDE as the front end development tool...
lq
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn********************@127.0.0.1>... Amit Gupta (am****@hotmail.com) writes: /*Create a function to do the job*/ Create function dbo.fn_concatinate(@CustId as int) returns varchar(100) as begin declare @ret_value varchar(100) SET @ret_value='' Select @ret_value=@ret_value + ',' + Product FROM dbo.tb_View where CustID=@CustId RETURN RIGHT(@ret_value,LEN(@ret_value)-1) end
Not that this function relies on undefined behaviour. It may return the expected result, or it may return something else. See http://support.microsoft.com/default.aspx?scid=287515. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: CJM |
last post by:
I have page that starts a transaction and runs several StoredProcs before
committing or rollingback. An initial SP create a header records, and then
the code goes into a loop and runs 4 other SP's...
|
by: Lauren Quantrell |
last post by:
Is there a way to create a text file (such as a Windows Notepad file)
by using a trigger on a table? What I want to do is to send a row of
information to a table where the table: tblFileData has...
|
by: MD |
last post by:
I am trying to create a dynamic SQL statement to create a view.
I have a stored procedure, which based on the parameters passed calls
different stored procedures. Each of this sub stored procedure...
|
by: Ryan.Chowdhury |
last post by:
This is a general question regarding the use of view and stored
procedures. I'm fairly new to databases and SQL.
I've created a SQL database using an Access Data Project ("ADP") and
I'm...
|
by: Peter |
last post by:
Hello£¬everyone,
My program will collect a testing machine's data ,save the data and deal
with the data everyday. I want to use vb.net to create database, add and
delete tables or modify the...
|
by: max |
last post by:
Hello,
I am a newbye, and I'm trying to write a simple application.
I have five tables with three columns; all tables are identical; I need to
change some data in the first table and let VB...
|
by: etuncer |
last post by:
Hello All,
I have Access 2003, and am trying to build a database for my small
company. I want to be able to create a word document based on the data
entered through a form. the real question is...
|
by: raylopez99 |
last post by:
Keep in mind this is my first compiled SQL program Stored Procedure
(SP), copied from a book by Frasier Visual C++.NET in Visual Studio
2005 (Chap12). So far, so theory, except for one bug...
|
by: barmatt80 |
last post by:
I finally got my call to a stored procedure on our db2 to work. However i might have to change what the stored procedure does, if I cannot get it to work how we want. Which i would like to make it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |