Solved

Transact-SQL MAX Function.

Posted on 1998-11-26
7
988 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
[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
  • 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 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