Solved

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

Posted on 2011-03-03
3
426 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
     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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now