Solved

Set money variable

Posted on 2010-11-23
5
175 Views
Last Modified: 2012-06-21
My attached code works and returns a single value.

I need to be able to store that result in a @variable for use later.

How do I do that?
Declare @list NVarchar(30)
Set @list = '190,191,192,193'

Declare @sql nvarchar(1000)
Set @sql='
select Sum(afsAmount) from proc_cfa.dbo.P_AvailableForSale where afsid in (' + @list + ') group by Left(afsSource,7)'
exec (@sql)

Open in new window

0
Comment
Question by:lrbrister
[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
5 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34198968
Declare @list NVarchar(30)
Set @list = '190,191,192,193'

declare @m money
Declare @sql nvarchar(1000)
Set @sql='
select @m = Sum(afsAmount) from proc_cfa.dbo.P_AvailableForSale where afsid in (' + @list + ') group by Left(afsSource,7)'
exec sp_executesql @sql, N'@m money output', @m output
select @m
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34198970
Personally, I would get rid of the dynamic sql. Then it would be easy. This article has some great ways to converting @lists to something you can use in a non-dynamical-sql query

http://www.experts-exchange.com/Database/Miscellaneous/A_1536-delimited-list-as-parameter-what-are-the-options.html

ie
DECLARE @YourVariable ...

SELECT @YourVariable = SUM(afsAmount)
FROM    proc_cfa.dbo.P_AvailableForSale
WHERE afsid in (SELECT VALUE FROM dbo.ParmsToList(@list,',') );
GROUP BY Left(afsSource,7)

0
 
LVL 52

Expert Comment

by:_agx_
ID: 34198999
>> Personally, I would get rid of the dynamic sql.

There's different ways to do it. But avoiding dynamic sql is safer. Especially if your @list is a user supplied..
0
 

Author Closing Comment

by:lrbrister
ID: 34199314
This works...thanks

I'll investigate the link provided for research
0
 
LVL 52

Expert Comment

by:_agx_
ID: 34199851
Yeah, it's a good read and the function's a good way to avoid sql injection. If you're interacting with user data that is .. ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

732 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