Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a count of records in stored procedure

Status
Not open for further replies.

swilliams

Programmer
Aug 23, 2000
583
BM
Hello,
I want to do different things depending on the returned value of a sql statement which counts records, but keep getting syntax errors.
My ultimate goal is to return True or False back to an ASP page - If the count of records is > 0 return True, if the count of records = 0 return False.

This is the transact sql I am using:

CREATE PROCEDURE [s_Check_Duplicate_Article]
@bDuplicate varchar(5) OUTPUT,
@ID integer,
@Title varchar(50)
AS
DECLARE @iFound integer
-- set iFound to the number of records matching the title
SELECT @iFound = &quot;SELECT Count(ID) FROM t_Articles WHERE Title='&quot;+@Title+&quot;' AND ID<>&quot;+@ID
IF @iFound > 0
SELECT @bDuplicate = &quot;True&quot;
ELSE
SELECT @bDuplicate = &quot;False&quot;

and the error happens on the SELECT @iFound = .... line.

Can someone tell me the correct way to do this??

TIA [sig]<p>Simon<br>[/sig]
 
I don't think you need the quote marks around the SELECT statement.

Also, you might try using @@RowCount.
[sig]<p>Robert Bradley<br><a href=mailto: > </a><br><a href= - Visual FoxPro Development</a><br> [/sig]
 
Ommitting the quotes from around the SELECT statement produces another error - already tried that one!! [sig]<p>Simon<br>[/sig]
 
Solved.

Replace the SELECT @iFound = ... line with:

SELECT @iFound = (SELECT Count(ID) FROM t_Articles WHERE Title='+@Title+' AND ID<>+@ID) [sig]<p>Simon<br>[/sig]
 
Try this:
SELECT @iFound = Count(ID) FROM t_Articles WHERE Title = @Title AND ID <> @ID

This is much easier... [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top