Solved

Transact-SQL MAX Function.

Posted on 1998-11-26
7
984 Views
Last Modified: 2008-02-26
I'm using VB5 and T-SQL. I want to return the current biggest value of a column on a table back to VB using an SQL stored procedure as follows.

CREATE PROC ssp_max
AS
declare @num1 int
select max(clt_client) as "[@num1]" from clt
select * from clt where clt_client = @num1
GO

What I am doing wrong here? When I run ssp_max, @num1 has the correct value but the select statement doesnt read a row. I wish to read the row with the biggest clt_client and
return it as a resultSet.
0
Comment
Question by:eryan
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:mayhew
ID: 1091991
I use T-SQL mostly with Delphi and the result set of a stored proc is populated with the first query that the stored proc processes.  Further queries are ignored (for the purposes of the result set to your app).  I'm not sure if this is the problem with a VB app, but it's my first guess.

I would replace the whole snippet you posted with:

SELECT * FROM clt WHERE clt_client = max(clt_client)


Either that or replace your first select with:

SELECT @num1 = max(clt_client) FROM clt

so that your stored proc is treating the select as an assignment of value rather than a query that returns data.

Let me know if that helps.

Don
0
 

Author Comment

by:eryan
ID: 1091992
Don, Neither of these worked. I liked the first one but when I try to create the procedure it says
" An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference"
The second suggestion does not return a result set.

Please respond if you have any more ideas, thanks ED Ryan
0
 

Author Comment

by:eryan
ID: 1091993
Don, Neither of these worked. I liked the first one but when I try to create the procedure it says
" An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference"
The second suggestion does not return a result set.

Please respond if you have any more ideas, thanks ED Ryan
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:eryan
ID: 1091994
Don, Neither of these worked. I liked the first one but when I try to create the procedure it says
" An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference"
The second suggestion does not return a result set.

Please respond if you have any more ideas, thanks ED Ryan
0
 
LVL 1

Accepted Solution

by:
kponder earned 70 total points
ID: 1091995
Try this:

SELECT *
  FROM clt
  WHERE clt_client = (SELECT MAX(clt_client) from clt)
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1091996
eryan,

You are right, of course.  Kponder's suggestion will do the trick.  Sorry for the oversight.

I'm curious about the other option returning no data, though.  One thing to check is to make sure clt_client is an int.

I tested this with the pubs database that comes with SQL Server with the following code and it works fine.

create proc test as
declare @num1 float

select @num1 = max(price) from titles
select * from titles where price = @num1

go

exec test
go


Please let me know if this is helpful (or what I'm missing).  Thanks!  :)

Don
0
 

Author Comment

by:eryan
ID: 1091997
KPonder, Thanks for that and it works fine.
Don, I'm still not sure why you're other option does not work.
clt_client is definately int, etc, but an empty result set gets returned. Thanks for your interest anyway, and if I find out
the reason it didn't work, I'll drop a comment onto Experts Xchange.
If either of you have used VB and Crystal reports, by the way,
I'll be dropping a 150 point question onto the Xchange later today!!!!.
Ed.
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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

867 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