473,597 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stored procedure make string from table field

Hallo !

I have a Table with a column "ordernumbe r"

ordernumber
A12
A45
A77
A88

Is it possible to create a stored procedure which makes a string of these column ?

Result: string = ('A12','A45','A 77','A88')

Thanks !

aaapaul
Jul 20 '05 #1
3 9320
ng
Hello Paul,
try this Code. It defines a Table in the current database and
creates as stored proc that does the work.

The stored proc uses a cursor to loop thru all entries in
the table.

--Code start
IF EXISTS( SELECT *
FROM sysobjects
WHERE name = 'tblOrderNumber s'
AND type = 'U'
) BEGIN
DROP TABLE tblOrderNumbers

END

CREATE TABLE tblOrderNumbers (OrderNumber varchar(5))

INSERT INTO tblOrderNumbers VALUES ('A12')
INSERT INTO tblOrderNumbers VALUES ('A45')
INSERT INTO tblOrderNumbers VALUES ('A77')
INSERT INTO tblOrderNumbers VALUES ('A88')
GO


IF EXISTS( SELECT *
FROM sysobjects
WHERE name = 'spOrderNumbers String'
) BEGIN
DROP PROCEDURE spOrderNumbersS tring
END
GO

/*
=============== =============== =============== =============== =============== ===
Syno:
Author: www.zankl-it.de, 2003
=============== =============== =============== =============== =============== ===
*/
CREATE PROCEDURE spOrderNumbersS tring
AS
SET NOCOUNT ON

DECLARE @OrderNumber varchar(5),
@Result varchar(1024)
SELECT @Result = '('
DECLARE cOrderNumbers CURSOR FAST_FORWARD READ_ONLY FOR
SELECT * FROM tblOrderNumbers

OPEN cOrderNumbers
FETCH NEXT FROM cOrderNumbers INTO @OrderNumber
WHILE (@@FETCH_STATUS = 0) BEGIN

SET @Result = @Result + '''' + @OrderNumber + ''','

FETCH NEXT FROM cOrderNumbers INTO @OrderNumber

END

CLOSE cOrderNumbers
DEALLOCATE cOrderNumbers

IF LEN(@Result) > 1 BEGIN
SET @Result = LEFT(@Result, LEN(@Result)-1) + ')'
END ELSE BEGIN
SET @Result = NULL
END
SELECT @Result

RETURN 0
GO

EXEC spOrderNumbersS tring
--Code End

Zankl-IT, Berlin
www.zankl-it.de

pa************* *@vit.linhardt. com (aaapaul) wrote in message news:<9d******* *************** ****@posting.go ogle.com>...
Hallo !

I have a Table with a column "ordernumbe r"

ordernumber
A12
A45
A77
A88

Is it possible to create a stored procedure which makes a string of these column ?

Result: string = ('A12','A45','A 77','A88')

Thanks !

aaapaul

Jul 20 '05 #2
I think maybe this should work (Tablename = tOrders):

CREATE PROCEDURE spGetOrderNumbe rs AS
DECLARE @OrderIDs VARCHAR(250)
BEGIN
SELECT @OrderIDs = COALESCE(@Order IDs + ', ', '') + '''' + OrderNumber + ''''
FROM tOrders
SET @OrderIDs = '(' + @OrderIDs + ')'
SELECT @OrderIds
END
GO

Martin
Jul 20 '05 #3
>> Is it possible to create a stored procedure which makes a string of
this column ? <<

Yes, but it will be a proprietary kludge, using either proceudral code
or unpredictable behavior. The whole idea of a Client/Server system
is that the back end does the data retrieval and the front end does
the display and formatting.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4183
by: Tavish Muldoon | last post by:
What a pain trying to insert data into a table from a stored proc. My webform asks for 16 pieces of data - which then gets written to the database. I found this easier than the crap below (after the ***********). This was eaiser: get a list of all the parameters you want to pass - put them in a string:
4
8782
by: mingzhen | last post by:
Hi, I have a problem here when I code stored procedure to manipulate data in database. My idea is that if I can save all data I get in some arrays I should be able to manipulate the arrays directly. But I do not know how to declare arrays in SQL server stored procedure and do not know other ways to do the same thing. Please help me out. Thanks in advance. -mingzhen
5
2233
by: Rob Wire | last post by:
For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so that it would bring back all attachments at all companies or all locations at a company? Thank you, Rob. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then
5
2832
by: microsoft.private.windows.netserver.setup | last post by:
I have a very strange thing occurring in the program. I have a dataset retrieved from a stored procedure that just select * from a table. I then try to use the SQlCommandBuilder on the dataset, and fails. I try the same select statement directly and not using a stored procedure and use SQLCommandBuilder, the program works. This is a ASP.net page, and I am stumped. I would like to use the stored procedure rather than controlling it in...
9
5267
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT INTO Table ( ID, Cod, CodArt, Q1, DataUscita ) VALUES (pID, pCod, pCod, pQ1, pDataUscita);
7
3441
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
2
8083
by: karups | last post by:
Hi, I've got listbox in my .aspx page where the users can make multiple selection. So, Users can select any number of items in listbox, I have to take value from items and pass it to stored procedure to extract a dataset back. 1.What should i do while passing the multiple selected values? 2.Can i use 'in' clause in SQL procedure like
0
3166
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works fine. Now we decided to move from mainframe IMS-DB2 to Windows 2003 server-DB2 UDB for LUW 9.5.
7
8193
by: E11esar | last post by:
Hi there. I have written a C# web service that calls an Oracle stored procedure. The SP is a simple select-max query and the table it is getting the value from has about 2.8 million rows in it. The field being MAX'd is an index field in the table. I have further ensured that the table has had an analysis and rebuild run on it to ensure good integrity but still I am finding the stored procedure takes around 3 minutes to return the MAX...
0
7893
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6698
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.