Solved

SQL Server Select not returning correct values

Posted on 2014-01-16
10
285 Views
Last Modified: 2014-01-16
This should be pretty straight forward and easy...I need another set of eyes to look at this as I'm not getting any where.

I have the following code:
Declare  @Market Table(High int, low int, Question_Num smallint, limit int)
Declare @intQuestion int, @intBuy int, @intSell int, @fltLImit float, @Count int,@high float, @low float

 Insert into @Market (High,low,Question_Num,limit)
 Values
 (115,114,1,5),
 (115,114,2,5),
 (113,112,3,5),
 (111,110,4,5)
 

Set @intQuestion =4
Set @fltLimit= 5
Set @Count=0
		
 Select TOP 1 High +@Count
,Low+@Count
,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Num Desc

Open in new window


It should return the results as (111, 110, 4,5); however it returns (115,114,4,5,).  If I modify my code so @intQuestion is commented out:
 Select TOP 1 High +@Count
,Low+@Count
--,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Num Desc

Open in new window


I get the correct values of (111,110,5).  Anyone have any idea's what's going on here...?

Thanks!
0
Comment
Question by:badrhino
  • 5
  • 4
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39785869
<knee-jerk reaction>

>Insert into @Market
Change this temp table to #Market, and rerun.

>High +@Count
Not that it affects the return set, as @Count = 0, but what's the purpose of this?
0
 
LVL 1

Author Comment

by:badrhino
ID: 39785882
Knee-jerk
@Market is actually a table in my db (dbo.Market), I just made it a table variable for ease of posting...Correct me if I'm wrong, it shouldn't matter if the data is coming from a table, temp table or a table variable...

@Count normally isn't zero, but I zeroed it out to try and debug things....This is a segment of my code that is causing me the problem.
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785889
Change
--,@intQuestion as Question_Num to any other variable
0
 
LVL 1

Author Comment

by:badrhino
ID: 39785902
ravikantninave
Changed @intQuestion to @fltLimit  and it does the same thing.
Select TOP 1 High +@Count
,Low+@Count
,@fltLimit as Question_Num
,Limit=@fltLimit
,high
,@low
From Market_Simulation 	
Order by Question_Num Desc

Open in new window

0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785907
Change this
as Question_Num to Question_abcd
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:badrhino
ID: 39785914
Ravik....
That worked.  What is going on?  I know that the order by clause is the last to execute, but why is this causing a problem?
0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 500 total points
ID: 39785919
conflict with your db field Question_Num
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785927
Try to Change the db field Question_Num to anything else and try your first code it will run perfectly

Declare  @Market Table(High int, low int, Question_Numx smallint, limit int)
Declare @intQuestion int, @intBuy int, @intSell int, @fltLImit float, @Count int,@high float, @low float

 Insert into @Market (High,low,Question_Numx,limit)
 Values
 (115,114,1,5),
 (115,114,2,5),
 (113,112,3,5),
 (111,110,4,5)
 

Set @intQuestion =4
Set @fltLimit= 5
Set @Count=0
		
 Select TOP 1 High +@Count
,Low+@Count
,@intQuestion as Question_Num
,Limit=@fltLimit
From @Market 	
Order by Question_Numx Desc

Open in new window

0
 
LVL 1

Author Closing Comment

by:badrhino
ID: 39785936
Thanks!  Learn something every day!
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39785940
):
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

23 Experts available now in Live!

Get 1:1 Help Now