Solved

SQL Server Select not returning correct values

Posted on 2014-01-16
10
283 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to import 7 53
SQL - combining multiple records into 1 record 5 33
SQL Server RDS clr assembly 4 35
SQl query 19 12
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

16 Experts available now in Live!

Get 1:1 Help Now