Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lindsayleigh
Starting Member
2 Posts |
Posted - 2010-07-26 : 10:34:18
|
Hi there, I am on the beginner level of SQL, but I got stuck editing another developer's complex code. I need to get the total number of records returned from the stored procedure below (total number of patients). Any advice would be vastly appreciated!set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[ARResult] @StartDate DateTime = null, @EndDate DateTime = null, @AgencyList varchar(1000) = '', @Requestor varchar(50) = '', @UserID INT, @AgencyID varchar(50) = nullASBEGIN SET NOCOUNT ON; DECLARE @SQL nvarchar(1200) DECLARE @ParamDefinition as NVARCHAR(2000) SET @SQL = 'SELECT min(h.DataHeaderID) as DataHeaderID, min(h.CollectionDate) as CollectionDate, (SELECT TOP 1 DataLine.TestAndApproveDate FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo) as ApproveDate, (SELECT TOP 1 DataLine.ResultFlag FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo ORDER BY ResultFlag DESC) as ResultFlag, min(h.SpecimenNo) as SpecimenNo, min(h.SSN) as SSN, min(h.LastName + '', '' + h.FirstName + '' '' + h.MiddleInitial) as FullName, (SELECT TOP 1 CASE WHEN DataLine.Outcome = ''Inconsistent'' THEN ''INCONSISTENT'' ELSE ''CONSISTENT'' END AS TMPOUT FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo ORDER BY TMPOUT DESC) as Outcome, min(h.Requestor) as Requestor, min(h.AgencyNumber) as AgencyNumber FROM RxToxDataHeader h WHERE (1=1) ' IF @AgencyList IS NOT NULL AND @AgencyList != '' SET @SQL = @SQL + ' AND h.AgencyNumber IN (' + @AgencyList + ')' IF @AgencyID IS NOT NULL AND @AgencyID != '' BEGIN SET @AgencyID = '%' + @AgencyID + '%' SET @SQL = @SQL + ' AND h.AgencyNumber LIKE @AgencyID' END IF @Requestor IS NOT NULL AND @Requestor != '' BEGIN SET @Requestor = '%' + @Requestor + '%' SET @SQL = @SQL + ' AND h.Requestor LIKE @Requestor' END IF (@StartDate <> '') AND (@EndDate <> '') SET @SQL = @SQL + ' AND h.CollectionDate BETWEEN @StartDate AND DATEADD(day,1,@EndDate)' SET @SQL = @SQL + ' GROUP BY h.SpecimenNo' SET @SQL = @SQL + ' ORDER BY h.SpecimenNo DESC' SET @ParamDefinition = ' @Requestor varchar(50), @StartDate DateTime, @EndDate DateTime, @AgencyList varchar(100), @UserID INT, @AgencyID varchar(50) ' PRINT @SQL EXEC sp_ExecuteSQL @SQL, @ParamDefinition, @Requestor, @StartDate, @EndDate, @AgencyList, @UserID, @AgencyIDEND |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-26 : 10:56:02
|
ChangeSET @SQL = 'SELECT min(h.DataHeaderID) as DataHeaderID, min(h.CollectionDate) as CollectionDate, (SELECT TOP 1 DataLine.TestAndApproveDate FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo) as ApproveDate, (SELECT TOP 1 DataLine.ResultFlag FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo ORDER BY ResultFlag DESC) as ResultFlag, min(h.SpecimenNo) as SpecimenNo, min(h.SSN) as SSN, min(h.LastName + '', '' + h.FirstName + '' '' + h.MiddleInitial) as FullName, (SELECT TOP 1 CASE WHEN DataLine.Outcome = ''Inconsistent'' THEN ''INCONSISTENT'' ELSE ''CONSISTENT'' END AS TMPOUT FROM DataLine WHERE h.SpecimenNo = DataLine.SpecimenNo ORDER BY TMPOUT DESC) as Outcome, min(h.Requestor) as Requestor, min(h.AgencyNumber) as AgencyNumber FROM RxToxDataHeader h toSET @SQL = 'SELECT COUNT(*) FROM RxToxDataHeader h ?? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-07-26 : 11:05:09
|
I'm pretty sure you can capture the @@ROWCOUNT after the EXEC. For Example:DECLARE @RowCount BIGINT--- Build Dynamic SQL HereEXEC(@SQL)SET @RowCount = @@ROWCOUNT |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-07-26 : 11:07:09
|
quote: Originally posted by Lamprey I'm pretty sure you can capture the @@ROWCOUNT after the EXEC. For Example:DECLARE @RowCount BIGINT--- Build Dynamic SQL HereEXEC(@SQL)SET @RowCount = @@ROWCOUNT
Dynamic sql is in a saparate thread.....I don't think (I hate when that happens) that it won't see the rowcountBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-07-26 : 11:18:54
|
or...I could be mistakenCREATE TABLE myTable99 (Col1 int IDENTITY(1,1), Col2 char(1))GOINSERT INTO myTable99(Col2) SELECT 'a' UNION ALL SELECT 'b' UNION ALL SELECT 'c'GODECLARE @sql varchar(8000)SET @sql = 'SELECT * FROM myTable99'EXEC(@sql)SELECT @@ROWCOUNTGODROP TABLE myTable99GO EDIT: Good all the way back to 2000Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-26 : 11:34:17
|
Or...BEGIN SET NOCOUNT ON; DECLARE @SQL nvarchar(1200) DECLARE @ParamDefinition as NVARCHAR(2000) DECLARE @intRowCount int SET @SQL = 'SELECT...... SET @SQL = @SQL + ' SELECT @intRowCount = @@ROWCOUNT' SET @ParamDefinition = ' @Requestor varchar(50), @StartDate DateTime, @EndDate DateTime, @AgencyList varchar(100), @UserID INT, @AgencyID varchar(50), @intRowCount int OUTPUT ' PRINT @SQL EXEC sp_ExecuteSQL @SQL, @ParamDefinition, @Requestor, @StartDate, @EndDate, @AgencyList, @UserID, @AgencyID, @intRowCount = @intRowCount OUTPUT' ... Do something with @intRowCount here ...END but that will still retrieve all the data rows, which is a bit wasteful if all you want is the count. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-07-26 : 12:23:59
|
quote: Originally posted by Kristenbut that will still retrieve all the data rows, which is a bit wasteful if all you want is the count.
I'm confused....OP:quote: I need to get the total number of records returned from the stored procedure
And btw Lindsay...I always have output variables in ALL my Sprocs that return the @@ROWCOUNT (or the sum of them), @@ERROR, and a standard Return CodeFor ExampleDECLARE @rc int, @Error int, @Error_Message varchar(255), @Rowcount int--Sample Execution EXEC usp_SEL_VENDOR_LIKE 'PR' -- CompanyName , null -- City , null -- State , null -- ZipCode , 'X002548' -- @App_User , @rc OUTPUT, @Error OUTPUT, @Error_Message OUTPUT, @Rowcount OUTPUTSELECT @rc AS int, @Error AS int, @Error_Message AS varchar, @Rowcount AS int Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-07-26 : 12:55:48
|
Yup, you are right, that's what the O/P said. But I think there is a chance that the O/P just wants the COUNT - e.g. for "If you click this you will get 1,234,567 records" - rather than "The list above contains 1,234,567 records" ....... but only the O/P can clarify for us |
 |
|
|
lindsayleigh
Starting Member
2 Posts |
Posted - 2010-07-26 : 14:13:31
|
| Thanks for all of the replies so far. :) Here is how I'm using it: There is a search page with optional criteria like dates, agency id, etc and a Search button. After clicking the button, I want to calculate several different things, including the total number of patients, number of consistent/inconsistent patients, number of positive/negative results...all WHERE those criteria are met. These values will be passed over to DynamicPDF to make some statistics pie charts and bar charts. I was trying to copy this stored procedure over from an existing page on the site that pulls the same data, but just displays all of the results in a gridview. I don't need this gridview at all on my page or else I'd just do that and count the rows. |
 |
|
|
|
|
|
|
|