Hello,
Does anyone know of a way to loop thru a SQL table using code in a stored
procedure?
I need to go thru each record in a small table and build a string using
values from the fields associated with a part number, and I can't find any
way to process each record individually. The string needs to be initialized
with the data associated with the 1st record's part number, and I need to
build the string until a new part number is incurred. Once a new part number
is found in the table, the string is written to a different table and reset
for this next part number in the table. Need to repeat until all records in
the table have been processed.
I use ADO in access 2000 to work thru local recordsets, I just can't find
anyway to do this in a stored SQL procedure.
Thanks for any suggestions, Eric. 1 28241
Thanks Erland, that worked in the procedure.
"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn**********************@127.0.0.1... [posted and mailed, please reply in news]
Eric Martin (er********@cox.net) writes: Does anyone know of a way to loop thru a SQL table using code in a
stored procedure?
Most people here know that you should not do this kind of thing, but one should always strive for set-based solutions. Then again...
I need to go thru each record in a small table and build a string using values from the fields associated with a part number, and I can't find any way to process each record individually. The string needs to be initialized with the data associated with the 1st record's part number, and I need to build the string until a new part number is incurred. Once a new part number is found in the table, the string is written to a different table and reset for this next part number in the table. Need to repeat until all records in the table have been processed.
This sounds like it be one of the few cases where you need an iterative solution. Yet, then again:
I use ADO in access 2000 to work thru local recordsets, I just can't
find anyway to do this in a stored SQL procedure.
Doing this client-side might be just as effecient. So if there is no compelling reason for doing this in a stored procedure, you may keep the ADO solution - even if it means that the data needs to do some extra roundtrips.
Here is a sample of how such a procedure would look like:
DECLARE @partno varchar(10), @lastpartno varchar(10), @otherdata varchar(10), @output varchar(8000), @err int
DECLARE part_cur CURSOR LOCAL STATIC FOR SELECT partno, otherdata FROM tbl ORDER BY partno SELECT @err = @@error IF @err <> 0 BEGIN DEALLOCATE part_cur RETURN @err END
OPEN part_cur
WHILE 1 = 1 BEGIN FETCH part_cur INTO @partno, @otherdata IF @@fetch_status <> 0 BREAK
IF @partno <> coalesce(@lastpartno, '') BEGIN IF @lastpartno IS NOT NULL BEGIN INSERT othertbl (col1) VALUES (@output) SELECT @err = @@error IF @err <> 0 BREAK END SELECT @lastpartno = @partno, @output = @partno END
SELECT @output = @output + ', ' + @otherdata END
DEALLOCATE part_cur
IF @err <> 0 RETURN @err
INSERT othertbl (col1) VALUES (@output) SELECT @err = @@error IF @err <> 0 RETURN @err
-- Erland Sommarskog, SQL Server MVP, so****@algonet.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Wilson |
last post by:
Hello,
I have the following code which populates as table data from a SQL
Server 2000 stored proc (RSByDemoID2). Below that is the view and
stored procedure which takes @DemoID as input to match...
|
by: Andrew Young |
last post by:
How do I loop thru a result set Without using a curosr?
|
by: Alex |
last post by:
I have a "source" table that is being populated by a DTS bulk import
of a text file. I need to scrub the source table after the import
step by running appropriate stored proc(s) to copy the source...
|
by: .:mmac:. |
last post by:
I have a bunch of files (Playlist files for media player) and I am trying to
create an automatically generated web page that includes the last 20 or 30
of these files. The files are created every...
|
by: Robin Tucker |
last post by:
Hi,
I'm trying to determine with my program whether or not a given database
supports a given feature set. To do this I'm querying for certain stored
procedures in the sysobjects table and if...
|
by: hubert.trzewik |
last post by:
Hello,
Is it possible to EXEC stored procedure from a query?
I want to execute stored procedure for every line of SELECT result
table.
I guess it's possible with cursors, but maybe it's...
|
by: Radu |
last post by:
Hi. It seems to be very simple, actually, but I don't know if it is
feasible in TSQL. I have a sproc which gathers in one place many calls
to different other sprocs, all of them taking a...
|
by: satishchandra999 |
last post by:
I have SP, which has a cursor iterations. Need to call another SP for
every loop iteration of the cursor. The pseudo code is as follows..
Create proc1 as
Begin
Variable declrations...
...
|
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= |
last post by:
I need to loop in a stored procedure for the variable @hdnIncomeIDNO1 and
@IncomeType_CODE11 to @hdnIncomeIDNO10 and @IncomeType_CODE10
Instead of writing the upadate statement 10 time(as I did...
|
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: 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,...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |