[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Return specific items from a single row as output parameters

Posted on 2010-11-17
2
Medium Priority
?
225 Views
Last Modified: 2012-05-10
Hello Experts,

Using SQL Server 2005...

Here is a simplified case of what I'm trying to do:

I have a table that contains time stamped data recorded from remote sensors.  I would like to have a single stored procedure that can return some aggregate data for for a given sensor over a given time frame.

I'm wondering how I query for ONE row, and then take a few of the fields' data and put it in output parameters.

Can I get a little help with the best way to accomplish this?  This would be psudo code for what I want to do...
CREATE PROCEDURE GetSensorSummary
   @SensorID int,
   @BegDate datetime,
   @EndDate datetime,
   @SensorDescription nvarchar(127) out,
   @MaxValue double out,
   @MaxTime datetime out,
   @MinValue double out,
   @MinTime datetime out
AS
BEGIN
   -- put the description from this query in @SensorDescription
   -- which I could do like this if only getting one value (I actually want to grab more than one):

   SET @SensorDescription = SELECT Description FROM SensorDef WHERE SensorID=@SensorID

   -- the queries below would also contain the date criteria in the WHERE clause (left out for brevity)

   SELECT TOP 1 DataValue, DataTime FROM SensorData WHERE SensorID=@SensorID ORDER BY DataValue DESC
   -- want to set @MaxValue=DataValue and @MaxTime=DataTime

   SELECT TOP 1 DataValue, DataTime FROM SensorData WHERE SensorID=@SensorID ORDER BY DataValue ASC
   -- want to set @MinValue=DataValue and @MinTime=DataTime
END
GO

Open in new window

0
Comment
Question by:Jahelka
[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
2 Comments
 
LVL 17

Accepted Solution

by:
OriNetworks earned 2000 total points
ID: 34161877
The query you are using seems pretty accurate except you would use
SELECT @SensorDescription = description from...

You could also do multiple parameters in one select such as select @param1= field1, @param2=field2 from ....
0
 
LVL 1

Author Closing Comment

by:Jahelka
ID: 34161957
Man, and here I was thinking this was going to be complicated.  I've been using stored procedures for a long time, but rarely need to return anything but a data set.  Can't believe how easy this was, and how little I still know about the power of sprocs!  Thanks for not calling me an idiot.  Works GREAT!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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