Solved

Transact-SQL MAX Function.

Posted on 1998-11-26
7
986 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

813 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

15 Experts available now in Live!

Get 1:1 Help Now