Solved

SQL Server - Saving a result into var and updating another table

Posted on 2011-03-03
3
444 Views
Last Modified: 2012-05-11
Hi..
Need help with a stored procedure.  I need to query one table get the result and update another with it.

I need to query a Sales table  get the SALESMAN with the MOST sales    - MAX(SALES)

Then take that value and update the TOPSALES man Table with that amount for that given SALESMAN.

Need the SQL to save the result to a var ... take that var and update another table

thx
0
Comment
Question by:JElster
3 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 84 total points
ID: 35031742
try

declare @topsalesamount int
declare @salesman varchar

select @salesman= SALESMAN, @topsalesamount = MAX(SALES)
from Table1
group by SALESMAN
order by MAX(SALES)
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 83 total points
ID: 35031744
declare @m varchar(100), @s float
select top 1 @m = salesman, @s = sum(sales) from sales group by salesman order by sum(sales) desc
update topsales set sales = @s where salesman = @m
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 83 total points
ID: 35031913
Create your proc like this.
Create Procedure spUpdate(@SalesMan varchar(100))
as
begin
declare @MaxSales int
select @MaxSales = max(Sales) from Sales where SalesMan = @SalesMan
update TopSales set Sales = @MaxSales where SalesMan = @SalesMan
end

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question