Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server Select not returning correct values

Posted on 2014-01-16
10
Medium Priority
?
293 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
[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
  • 5
  • 4
10 Comments
 
LVL 66

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 
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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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