Friday, February 10, 2012

Cant get ROW_NUMBER() working

I'm trying to create this stored Procedure:

SET

ANSI_NULLSON

GO

SET

QUOTED_IDENTIFIERON

GO

CREATEPROCEDURE [dbo].[iumm_PagerData]

@.PagerIndexINT,

@.NumRows

INT,

@.PicsCount

INTOUTPUT

AS

BEGIN

SELECT @.PicsCount=(SELECTCOUNT(*)FROM pics)Declare @.startRowIndexINT;set @.startRowIndex=(@.PagerIndex* @.NumRows)+ 1;

With PicEntriesas(SELECTROW_NUMBER()OVER(ORDERBY picIDDESC)AS Row, picID, userID, picFileName, votes, dateFROM pics)-- Insert statements for procedure hereSELECT picID, userID, picFileNameFROM picsWHERE Rowbetween @.startRowIndexand @.StartRowIndex+@.NumRows-1

END

GO

--

However, I'm always getting this error:

Msg 207, Level 16, State 1, Procedure iumm_PagerData, Line 22

Invalid column name 'Row'.

Msg 207, Level 16, State 1, Procedure iumm_PagerData, Line 22

Invalid column name 'Row'.

Anyone knows what could be the reason for that? Thanks.

You're not referencing your 'new' table in the second select statement. Try:

SELECT picID, userID, picFileNameFROMPicEntriesWHERE Rowbetween @.startRowIndexand @.StartRowIndex+@.NumRows-1
|||

ps2goat:

SELECT picID, userID, picFileName
FROMPicEntries
WHERE Rowbetween @.startRowIndexand @.StartRowIndex+@.NumRows-1

Oh yea, thanks, that's it.. Cheers!

No comments:

Post a Comment