473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

help on a sql request

Sam
Hi,
I have quite a complicated request to do in sql.
I've got on table with 3 fields (id, field1, field2) and I have to
process a request on each of the records, one by one, and then
determine if the status is OK for each records
For example, I would check if the sum of field1 and field2 is greater
than 10.
What is the best way to do this ? It has to be done in a stored
procedure. I can't return the status for each one of the records, so
should I store it in a temporary table and do a select on it from my
tier application (vb.net) ?
Thx
Sam

Jul 23 '05 #1
7 1141
What does "process a request" mean? If it's just a data manipulation
process then probably it can be done with a SELECT statement or other
SQL DML statements. You seem to be describing a cursor-type of process
but on the information you've given there is no evidence that that
would be necessary or desirable.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
Sam
Yes that's pretty much it. I would like to have a cursor-like
behaviour. Maybe not using cursors as I'm aware of their poor
performances.
By "process a request" I meant that on each row, I proceed to
operations (what I do there is not important). So I want a way to be
able to process each row one by one and to actually store the result
somewhere.

Jul 23 '05 #3
> By "process a request" I meant that on each row, I proceed to
operations (what I do there is not important).


What you do there IS important - it's the whole issue! If your process
just modifies some data in the database for example then you probably
won't need to do it row-by-row because usually it will be possible to
write set-based code to do the whole thing. That might mean replacing a
stored procedure with a new piece of code that doesn't have to be
called for each row but it's probably possible and it may well be worth
doing.

If you are doing something inherently procedural (probably something
outside the database such as sending an email or exporting a file) then
yes, you will need a cursor. Other alternative loops are just cursors
by another name and don't offer much advantage over the regular DECLARE
CURSOR... WHILE constructs. The problem isn't "cursors" per-se, it is
row-by-row processing that you should aim to avoid.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #4
"Sam" <sa**************@voila.fr> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi,
I have quite a complicated request to do in sql.
I've got on table with 3 fields (id, field1, field2) and I have to
process a request on each of the records, one by one, and then
determine if the status is OK for each records
For example, I would check if the sum of field1 and field2 is greater
than 10.
What is the best way to do this ? It has to be done in a stored
procedure. I can't return the status for each one of the records, so
should I store it in a temporary table and do a select on it from my
tier application (vb.net) ?
Thx
Sam


CREATE PROCEDURE junk
AS
SELECT id, Case When field1+field2 > 10 then 'Y' else 'N' END as Status
GO

Regards,
Jim
Jul 23 '05 #5
Sam
thanks, but it was just for the example ;) what I do is much more
complicated than field1 + field2.

David> I know I should avoid doing row by row processing but I think I
have no other way around. I will use a loop on the primary key.
thx

Jul 23 '05 #6
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
thanks, but it was just for the example ;) what I do is much more
complicated than field1 + field2.

David> I know I should avoid doing row by row processing but I think I
have no other way around. I will use a loop on the primary key.
thx


Sorry, I was being a bit facetious. Your question indicted that you needed
to return a result set that included a status based on a calculation. I
presented a method for doing that using CASE. You are presuming that you
need row by row processing but haven't really indicated why that might be
so. How do you determine the status for each row?

Regards,
Jim
Jul 23 '05 #7
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.
I've got on table with 3 fields [sic] (id, field1, field2) and I have to process a request on each of the records [sic], one by one, and
then determine if the status is OK for each records [sic] <<

Rows are not records; columns asre not fields; these are totally
different concepts. You are still carrying a mental model of a
sequential file system and not an RDBMS. You even say "process" instead
of "query".

After many years with SQL and six books on the language, I have written
five cursors in actual applications. Three of them could hav been
avoided if we had a CASE expression back them. The other two should
have been done in another language and were basically version of the
traveling salesman problem where I just needed the first answer under a
certain value, rather thant he st of all answers.
What is the best way to do this ? <<
Since we have no idea what the desired results are, over even what the
data looks like, it is hard to say. I tend to use VIEWs when I have a
computation to display to the users. Stored procedures are required if
I have to make changes to the base tables. In this case, it sounds like
you would have a computed column named "foobar_status" or whatever and
the rule for computing it.
It has to be done in a stored procedure. <<
You have already decided on the method! Don't do that; start with
specs and a goal, then pick a method.
I can't return the status for each one of the records [sic], so

should I store it in a temporary table and do a select on it from my
tier application (vb.net) ? <<

In the 1950's, ths would have been a scratch tape instead of a temp
table. Same design, mimicked in SQL.

Also, doesn't VB, along with most other host languages, have a way to
get a full result set to the front end?

Jul 23 '05 #8

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

Similar topics

0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
9
by: Peter | last post by:
My problem is the last bit of coding below, the like statement does not work. what I have is a product options field and in it is stored characters i.e. "avcy" etc what the query does is...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: Michael Kujawa | last post by:
I am using the following to create an SQL statement using the names and values from request.form. The loop goes through each item in request.form The issue comes in having an additional "and" at...
10
by: 60325 | last post by:
This is the page where I collect the data in drop-down boxes with values of 1-10 and send it to a submitted page to do calculations. Example: Employee1 TeamScore(1-10) Employee2 ...
10
by: pmarisole | last post by:
This is the first page...... <select size="1" name="Q1_<%=i%>" onChange="calc(<%=i%>)"> <option selected value="<% =rsScores("Q1") <option value="1">1</option> <option value="2">2</option>...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

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.