Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
I have another field in my database table called FileName, I want the record to be selected by FileName. What should I add to the code

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spx_Pager]
	@PageNo int = 1,
	@ItemsPerPage int = 2,
	@TotalRows int out,
	@f_name nvarchar(50)
AS
BEGIN
  SET NOCOUNT ON
  DECLARE
    @StartIdx int,
    @SQL nvarchar(max),  
    @SQL_Conditions nvarchar(max),	 
    @EndIdx int
	
	IF @PageNo < 1 SET @PageNo = 1
	IF @ItemsPerPage < 1 SET @ItemsPerPage = 10

	SET @StartIdx = (@PageNo -1) * @ItemsPerPage + 1
	SET @EndIdx = (@StartIdx + @ItemsPerPage) - 1
	SET @f_name = (@f_name)
	SET @SQL = 'SELECT FilePath
                FROM (
                SELECT  ROW_NUMBER() OVER(ORDER BY ID) AS Row, * 
                      FROM  tblFiles ) AS tbl WHERE  Row >= ' 
						+ CONVERT(varchar(9), @StartIdx) + ' AND
                       Row <=  ' + CONVERT(varchar(9), @EndIdx)
	EXEC sp_executesql @SQL

	SET @SQL = 'SELECT @TotalRows=COUNT(*) FROM tblFiles' 
	EXEC sp_executesql 
        @query = @SQL, 
        @params = N'@TotalRows INT OUTPUT', 
        @TotalRows = @TotalRows OUTPUT 
END

What I have tried:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spx_Pager]
	@PageNo int = 1,
	@ItemsPerPage int = 2,
	@TotalRows int out,
	@f_name nvarchar(50)
AS
BEGIN
  SET NOCOUNT ON
  DECLARE
    @StartIdx int,
    @SQL nvarchar(max),  
    @SQL_Conditions nvarchar(max),	 
    @EndIdx int
	
	IF @PageNo < 1 SET @PageNo = 1
	IF @ItemsPerPage < 1 SET @ItemsPerPage = 10

	SET @StartIdx = (@PageNo -1) * @ItemsPerPage + 1
	SET @EndIdx = (@StartIdx + @ItemsPerPage) - 1
	SET @f_name = (@f_name)
	SET @SQL = 'SELECT FilePath
                FROM (
                SELECT  ROW_NUMBER() OVER(ORDER BY ID) AS Row, * 
                      FROM  tblFiles ) AS tbl WHERE  Row >= ' 
						+ CONVERT(varchar(9), @StartIdx) + ' AND
                       Row <=  ' + CONVERT(varchar(9), @EndIdx) + ' AND FileName = img1'
	EXEC sp_executesql @SQL

	SET @SQL = 'SELECT @TotalRows=COUNT(*) FROM tblFiles' 
	EXEC sp_executesql 
        @query = @SQL, 
        @params = N'@TotalRows INT OUTPUT', 
        @TotalRows = @TotalRows OUTPUT 
END
Posted
Updated 12-Aug-16 1:54am

1 solution

SQL
SET @SQL = 'SELECT FilePath   
FROM (SELECT  ROW_NUMBER() OVER(ORDER BY ID) AS Row, * 
FROM  tblFiles ) AS tbl WHERE  Row >= ' 
+ CONVERT(varchar(9), @StartIdx) + ' AND
Row <=  ' + CONVERT(varchar(9), @EndIdx) + ' AND FileName = img1'


Has a serious problem, the only file it will ever look for will be named "img1"

Where did you get his procedure? Cut, paste, and ask for us to fix it for you doesn't usually work well. Where, in fact, did img1 even come from in order for you to select for it? Is it a table column? Specific value you chose but didn't declare? What?

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900