473,394 Members | 1,932 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,394 software developers and data experts.

Set variable based on result of procedure OR update columns fromsproc result

I need to send the result of a procedure to an update statement.
Basically updating the column of one table with the result of a
query in a stored procedure. It only returns one value, if it didnt I
could see why it would not work, but it only returns a count.

Lets say I have a sproc like so:

create proc sp_countclients
@datecreated datetime
as
set nocount on
select count(clientid) as count
from clientstable
where datecreated > @datecreated
Then, I want to update another table with that value:

Declare @dc datetime
set @dc = '2003-09-30'
update anothertable
set ClientCount = (exec sp_countclients @dc) -- this line errors
where id_ = @@identity
OR, I could try this, but still gives me error:

declare @c int
set @c = exec sp_countclients @dc
What should I do?

Thanks in advance!
Greg

Jul 20 '05 #1
4 12060

"Greg" <sy****@techgroupinc.com> wrote in message
news:3f***********************@news.twtelecom.net. ..
I need to send the result of a procedure to an update statement.
Basically updating the column of one table with the result of a
query in a stored procedure. It only returns one value, if it didnt I
could see why it would not work, but it only returns a count.

Lets say I have a sproc like so:

create proc sp_countclients
@datecreated datetime
as
set nocount on
select count(clientid) as count
from clientstable
where datecreated > @datecreated

An output parameter is usually the best way to get a scalar value back from
a proc:

create proc dbo.CountClients
@datecreated datetime,
@NumClients int OUTPUT
as
set nocount on
begin
select @NumClient = count(clientid)
from dbo.clientstable
where datecreated > @datecreated
end


Then, I want to update another table with that value:

Declare @dc datetime
set @dc = '2003-09-30'
update anothertable
set ClientCount = (exec sp_countclients @dc) -- this line errors
where id_ = @@identity

declare @dc datetime, @i int
set @dc = '20030930'
exec dbo.CountClients @datecreated = @dc, @NumClients = @i OUTPUT

update dbo.anothertable
set ClientCount = @i
where id = @@identity

OR, I could try this, but still gives me error:

declare @c int
set @c = exec sp_countclients @dc


You may be thinking of this:

exec @c = sp_countclients @dc

But that assigns the return value of the procedure to the variable, which is
not what you want. This is more useful:

exec @rc = dbo.CountClients @datecreated = @dc, @NumClients = @i OUTPUT

That assigns the return code to @rc and the count to @i, so you can check
for successful execution.

You should avoid using sp_ for stored procedure names - that's used for
system stored procedures only. And you may find that using @@identity as you
do above is unreliable (if you have triggers on your tables for example) -
if you need the identity value, it's usually best to assign it to a variable
immediately after it's generated, so you know you'll always be referring to
the same value throughout your code. Check out scope_identity() in Books
Online as well - it's often a better choice than @@identity.

Simon
Jul 20 '05 #2

"Greg" <sy****@techgroupinc.com> wrote in message
news:3f***********************@news.twtelecom.net. ..
I need to send the result of a procedure to an update statement.
Basically updating the column of one table with the result of a
query in a stored procedure. It only returns one value, if it didnt I
could see why it would not work, but it only returns a count.

Lets say I have a sproc like so:

create proc sp_countclients
@datecreated datetime
as
set nocount on
select count(clientid) as count
from clientstable
where datecreated > @datecreated

An output parameter is usually the best way to get a scalar value back from
a proc:

create proc dbo.CountClients
@datecreated datetime,
@NumClients int OUTPUT
as
set nocount on
begin
select @NumClient = count(clientid)
from dbo.clientstable
where datecreated > @datecreated
end


Then, I want to update another table with that value:

Declare @dc datetime
set @dc = '2003-09-30'
update anothertable
set ClientCount = (exec sp_countclients @dc) -- this line errors
where id_ = @@identity

declare @dc datetime, @i int
set @dc = '20030930'
exec dbo.CountClients @datecreated = @dc, @NumClients = @i OUTPUT

update dbo.anothertable
set ClientCount = @i
where id = @@identity

OR, I could try this, but still gives me error:

declare @c int
set @c = exec sp_countclients @dc


You may be thinking of this:

exec @c = sp_countclients @dc

But that assigns the return value of the procedure to the variable, which is
not what you want. This is more useful:

exec @rc = dbo.CountClients @datecreated = @dc, @NumClients = @i OUTPUT

That assigns the return code to @rc and the count to @i, so you can check
for successful execution.

You should avoid using sp_ for stored procedure names - that's used for
system stored procedures only. And you may find that using @@identity as you
do above is unreliable (if you have triggers on your tables for example) -
if you need the identity value, it's usually best to assign it to a variable
immediately after it's generated, so you know you'll always be referring to
the same value throughout your code. Check out scope_identity() in Books
Online as well - it's often a better choice than @@identity.

Simon
Jul 20 '05 #3
>> What should I do? <<

You can use a scalar subquery expression anywhere you would use any
other expression.

UPDATE Foobar
SET clientcount
= (SELECT COUNT(client_id)
FROM Clients
WHERE date_created < @search_date)
WHERE ..;

If the subquery is empty, you get a NULL; if the subquery is not
scalar, you get a cardinality violation.

Your real problem is that you are still thinking in terms of
procedures and not in SQL yet. Do not use process steps; if you need
to do an insert, write it all in one statement; if you need to do an
update, write it all in one statement; if you need to do a delete,
write it all in one statement.

Also, please tell that you know better than to use IDENTITY for a key.
That is a way to destroy data integrity, performance and all the
advantages of an RDBMS.
Jul 20 '05 #4
>> What should I do? <<

You can use a scalar subquery expression anywhere you would use any
other expression.

UPDATE Foobar
SET clientcount
= (SELECT COUNT(client_id)
FROM Clients
WHERE date_created < @search_date)
WHERE ..;

If the subquery is empty, you get a NULL; if the subquery is not
scalar, you get a cardinality violation.

Your real problem is that you are still thinking in terms of
procedures and not in SQL yet. Do not use process steps; if you need
to do an insert, write it all in one statement; if you need to do an
update, write it all in one statement; if you need to do a delete,
write it all in one statement.

Also, please tell that you know better than to use IDENTITY for a key.
That is a way to destroy data integrity, performance and all the
advantages of an RDBMS.
Jul 20 '05 #5

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

Similar topics

0
by: Greg | last post by:
I need to send the result of a procedure to an update statement. Basically updating the column of one table with the result of a query in a stored procedure. It only returns one value, if it didnt...
4
by: Shawn Fletcher | last post by:
Hi, I'm trying to work around a bug that our helpdesk software has. When a new issue is created, it cannot automatically default 2 fields to the value of No like we need it to. I have a...
5
by: Stanley Sinclair | last post by:
I have a need to return multiple result sets from a stored procedure. Want that SP to call others to get the data. Win2003, db2 8.1.5. Can't figure out how to handle open cursors, and return...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
1
by: islandtalker | last post by:
Hi, In a stored procedure (MS SQL) I want to update a table to Add columns to it, and then update those new columns with data from a variable. The problem is that, after updating the table I...
7
by: RachH | last post by:
I am using Access 2003 and need some guidance on the correct way to reference a variable in two different procedures when the variable's value is based on a SQL query. The database form has a...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
by: ssouhrada | last post by:
Hello, I have a stored procedure that looks similar to the one below: Select * from table a inner join table b
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
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
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: 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
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...
0
marktang
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,...
0
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...
0
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,...
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.