Solved

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

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

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

696 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