Solved

Transact-SQL MAX Function.

Posted on 1998-11-26
7
983 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

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

14 Experts available now in Live!

Get 1:1 Help Now