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
Solved

Transact-SQL MAX Function.

Posted on 1998-11-26
7
987 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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