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
Solved

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

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSSQL Speen Degradation 4 31
MS SQL Server - Looking to filter rows based on column value 3 46
SQL BULK INSERT Comma Delimited Issue 8 72
SQL Error - Query 6 40
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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