Solved

Return a value from a query inside a store procedure

Posted on 2007-04-03
5
177 Views
Last Modified: 2010-03-19
Hello there experts,

How can i do something like this:
A store procedure where i send a query and the store procedure returns the value from the query i sent into a variable.

Exe.:
declare @MyVal as float
SET @MyVal = sp_execSqlValue('SELECT SUM(value) FROM table')
SELECT @MyVal

Is this possible?
0
Comment
Question by:justaphase
  • 3
5 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 18845402
I don't understand.  This does the same as your example:

Create Procedure MyProc
AS
 SELECT SUM(Value) From table
GO
0
 
LVL 42

Expert Comment

by:dqmq
ID: 18845428
Or, maybe this is what you are thinking:

CREATE PROCEDURE myProc (@Query)
AS
EXEC (@Query)
GO

But, it is risky, and I still don't understand the benefit of doing such a thing.  
0
 
LVL 1

Author Comment

by:justaphase
ID: 18849402
The benefit is that i have a field in table that contains a query and i need to execute it and retrieve the value into a variable.
New example:
DECLARE @MyQuery AS nchar(200)
DECLARE @MyVal AS float

SET @MyQuery = (SELECT queryfield FROM mytable WHERE id=1) <-- The query inside the queryfield returns one numeric value
SET @MyVal = EXEC (@MyQuery) <-- This is the problem, i can't do it like this
0
 
LVL 39

Expert Comment

by:appari
ID: 18862536
try this

DECLARE @MyQuery AS nchar(200)
DECLARE @MyVal AS float

SET @MyQuery = (SELECT queryfield FROM mytable WHERE id=1) <-- The query inside the queryfield returns one numeric value

create table #testTab(col1 varchar(200))
insert into #testTab
EXEC (@MyQuery)

select * from #testTab
select @MyVal  = col1 from #testTab

drop table #testTab

0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 19002501
It's a long-shot, but maybe this:

DECLARE @MyResult VARCHAR(8000)
EXEC ('SET @MyResult =(' + @Query + ')')


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now