Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL - Updating Variable Values

Posted on 2013-01-17
7
Medium Priority
?
203 Views
Last Modified: 2013-01-17
Please can someone advise if it is possible to update a variable after the initial SET statement.  I want the variable to be updated with a single value based on several other values added together.

SET @TotalDuration = (SELECT Averages.Repair + Averages.Invoiced FROM Averages)

Receiving "Incorrect syntax near SET..."

The variable will then be used in a subsequent query as opposed to repeating the calculation over and over.
0
Comment
Question by:Damozz
[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
  • 4
  • 3
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38787240
that syntax IS correct inside T-SQL in MS SQL Server.
please clarify where/how you are using this?
did you dod the DECLARE variable?
please provide more of the code ...
0
 

Author Comment

by:Damozz
ID: 38787274
The averages table is a CTE... so I'm trying to set the variable immediately after

DECLARE @TotalDuration INT;

WITH

AVERAGES (Repair, Invoiced) AS

(
SELECT .....


)

SET @TotalDuration = (SELECT Averages.Repair + Averages.Invoiced FROM Averages)

The query works until I add the SET variable line which I intend to use for other calculations....
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38787392
then it needs to be a SELECT and not a SET:
DECLARE @TotalDuration INT;
WITH
AVERAGES (Repair, Invoiced) AS
(
SELECT .....

)
SELECT @TotalDuration = Averages.Repair + Averages.Invoiced FROM Averages)

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:Damozz
ID: 38787466
Thanks that worked, can you clarify?
0
 

Author Comment

by:Damozz
ID: 38787522
Does this prevent me from referencing the Averages CTE after this statement

e.g.

SELECT Averages.Repair/@TotalDuration

Also should the closing bracket on the above solution (line 8) be removed or is there an opening bracket missing?

Thanks
0
 

Author Comment

by:Damozz
ID: 38787614
Sorry the message I now receive is

Msg 208, Level 16, State 1, Line 158
Invalid object name 'Averages'.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38787868
you can use the CTE only once.
but you can use it like this:

DECLARE @TotalDuration INT;
DECLARE @repairs INT
WITH
AVERAGES (Repair, Invoiced) AS
(
SELECT .....

)
SELECT @TotalDuration = a.Repair + a.Invoiced 
   , @repairs = a.Repaid
  FROM Averages a

select @totalduration
   , cast(@repairs as decimal(10,2)) / @totalduration                                            

Open in new window

0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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