?
Solved

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

Posted on 2011-03-03
3
Medium Priority
?
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 336 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 332 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 41

Assisted Solution

by:Sharath
Sharath earned 332 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

777 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