dbTalk Databases Forums  

What is the best approach?

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss What is the best approach? in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andrew
 
Posts: n/a

Default What is the best approach? - 04-19-2005 , 01:30 PM






Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


Reply With Quote
  #2  
Old   
MGFoster
 
Posts: n/a

Default Re: What is the best approach? - 04-19-2005 , 01:46 PM






Andrew wrote:
Quote:
Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Create one query that returns the results of the calculations in the
result set. E.g.:

SELECT A.ColA * B.ColB As Result
FROM tableA As A INNER JOIN tableB As B
ON A.ID = B.ID
WHERE < criteria >
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQmVSDoechKqOuFEgEQIbkwCff49oHcM1n6LQJUbBfyTKZv uE7zUAoOYO
0GSmwyaPYPrgZLpipCKOwrnT
=VTo8
-----END PGP SIGNATURE-----


Reply With Quote
  #3  
Old   
CBretana
 
Posts: n/a

Default RE: What is the best approach? - 04-19-2005 , 01:50 PM



In the SP,
1) Update the columns in the table that need to be updated, and then
2) Select the updated values from the table to be returned to the client.


"Andrew" wrote:

Quote:
Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


Reply With Quote
  #4  
Old   
Andrew
 
Posts: n/a

Default RE: What is the best approach? - 04-19-2005 , 02:05 PM



those columns can not be updated. Although updated value need to be returned,
the original values must be kept.

"CBretana" wrote:

Quote:
In the SP,
1) Update the columns in the table that need to be updated, and then
2) Select the updated values from the table to be returned to the client.


"Andrew" wrote:

Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


Reply With Quote
  #5  
Old   
Andrew
 
Posts: n/a

Default Re: What is the best approach? - 04-19-2005 , 02:11 PM



Quote:
SELECT A.ColA * B.ColB As Result
FROM tableA As A INNER JOIN tableB As B
ON A.ID = B.ID
the calculation can not be done by a query, or it is very hard, e.g.,
currently we use 5 select statements to determine a value for a field
indicating if a package contains a new item, or if each item's property is
the same, or if this item is replaced with a new version, when comparing with
another package.

"MGFoster" wrote:

Quote:
Andrew wrote:
Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Create one query that returns the results of the calculations in the
result set. E.g.:

SELECT A.ColA * B.ColB As Result
FROM tableA As A INNER JOIN tableB As B
ON A.ID = B.ID
WHERE < criteria
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQmVSDoechKqOuFEgEQIbkwCff49oHcM1n6LQJUbBfyTKZv uE7zUAoOYO
0GSmwyaPYPrgZLpipCKOwrnT
=VTo8
-----END PGP SIGNATURE-----


Reply With Quote
  #6  
Old   
Rumbledor
 
Posts: n/a

Default Re: What is the best approach? - 04-19-2005 , 02:32 PM



=?Utf-8?B?QW5kcmV3?= <Andrew (AT) discussions (DOT) microsoft.com> wrote in
news:912F17CC-E359-457E-B2F3-6F15DE06034D (AT) microsoft (DOT) com:

Quote:
Hi, friends,

I need to write a stored procedure to return records selected from 2
tables. However, before returning those records, values of some fields
need to be recaculated with up-to-date data in some other tables of
the same database, and the original field values in that 2 tables
should keep the same. In another word, sp returned record values are
not the values in db tables, but recalculated ones, and their original
values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values,
return updated records, drop the view;
(2) Create a table in db especially for this purpose, say
volatileTable. Then, in sp, delete all records first, insert new
records with values from 2 tables, update values, return updated
records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.

How about using the query to populate a local temporary table
(##TableName), and then modifiying the values as necessary before selecting
them from the temp table to return to the user?

--
Rumble
"Write something worth reading, or do something worth writing."
-- Benjamin Franklin


Reply With Quote
  #7  
Old   
Andrew
 
Posts: n/a

Default Re: What is the best approach? - 04-19-2005 , 02:47 PM



#tempTableName is another way. What else?

"Rumbledor" wrote:

Quote:
=?Utf-8?B?QW5kcmV3?= <Andrew (AT) discussions (DOT) microsoft.com> wrote in
news:912F17CC-E359-457E-B2F3-6F15DE06034D (AT) microsoft (DOT) com:

Hi, friends,

I need to write a stored procedure to return records selected from 2
tables. However, before returning those records, values of some fields
need to be recaculated with up-to-date data in some other tables of
the same database, and the original field values in that 2 tables
should keep the same. In another word, sp returned record values are
not the values in db tables, but recalculated ones, and their original
values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values,
return updated records, drop the view;
(2) Create a table in db especially for this purpose, say
volatileTable. Then, in sp, delete all records first, insert new
records with values from 2 tables, update values, return updated
records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


How about using the query to populate a local temporary table
(##TableName), and then modifiying the values as necessary before selecting
them from the temp table to return to the user?

--
Rumble
"Write something worth reading, or do something worth writing."
-- Benjamin Franklin


Reply With Quote
  #8  
Old   
CBretana
 
Posts: n/a

Default RE: What is the best approach? - 04-19-2005 , 05:04 PM



Oh, then why can't you just Select the values you want the SP to return?
You know that the items returne in the output of a Select Statement do not
have to be cdirect references t ocolumns in a table, they can be arbitrarily
complex expressions based on the values of multiple columns plus constants or
input parameters...

"Andrew" wrote:

Quote:
those columns can not be updated. Although updated value need to be returned,
the original values must be kept.

"CBretana" wrote:

In the SP,
1) Update the columns in the table that need to be updated, and then
2) Select the updated values from the table to be returned to the client.


"Andrew" wrote:

Hi, friends,

I need to write a stored procedure to return records selected from 2 tables.
However, before returning those records, values of some fields need to be
recaculated with up-to-date data in some other tables of the same database,
and the original field values in that 2 tables should keep the same. In
another word, sp returned record values are not the values in db tables, but
recalculated ones, and their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update values, return
updated records, drop the view;
(2) Create a table in db especially for this purpose, say volatileTable.
Then, in sp, delete all records first, insert new records with values from 2
tables, update values, return updated records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


Reply With Quote
  #9  
Old   
Rumbledor
 
Posts: n/a

Default Re: What is the best approach? - 04-19-2005 , 08:19 PM



=?Utf-8?B?QW5kcmV3?= <Andrew (AT) discussions (DOT) microsoft.com> wrote in
news:166BD5A4-E97D-4D27-9820-B33E47E4C002 (AT) microsoft (DOT) com:

Actually, #tempTableName would be the correct way to create a *local*
temp table, right you are. Got my #'s mixed up. :P

I will say that I've seen some pretty complex queries rolled up into a
view or stored procedure, so you can quite likely meet your requirements
in either. Perhaps if you included some DDL we could confirm or
eliminate that possibility.

Quote:
#tempTableName is another way. What else?

"Rumbledor" wrote:

=?Utf-8?B?QW5kcmV3?= <Andrew (AT) discussions (DOT) microsoft.com> wrote in
news:912F17CC-E359-457E-B2F3-6F15DE06034D (AT) microsoft (DOT) com:

Hi, friends,

I need to write a stored procedure to return records selected from
2 tables. However, before returning those records, values of some
fields need to be recaculated with up-to-date data in some other
tables of the same database, and the original field values in that
2 tables should keep the same. In another word, sp returned record
values are not the values in db tables, but recalculated ones, and
their original values should not be updated.

So far I have two ways to do it:
(1) In sp, create a view with values in the 2 tables, update
values, return updated records, drop the view;
(2) Create a table in db especially for this purpose, say
volatileTable. Then, in sp, delete all records first, insert new
records with values from 2 tables, update values, return updated
records.

I am not sure which one is better, and any other better approaches?

Thanks a lot.


How about using the query to populate a local temporary table
(##TableName), and then modifiying the values as necessary before
selecting them from the temp table to return to the user?

--
Rumble
"Write something worth reading, or do something worth writing."
-- Benjamin Franklin



--
Rumble
"Write something worth reading, or do something worth writing."
-- Benjamin Franklin


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.