I have created a stored procedure which works perfectly fine when I execute it from from end i.e.C#.net or when I copy and past the query of stored procedure in separate SQL Query window but it neither give any result nor any error when I execute it in SQL server using EXEC sp_Name. I can not understand what is wrong in my code.
Below is my stored procedure
ALTER PROCEDURE sp_tbl_REQUEST_SelectAllByFilter_WithPagging
@PageIndex INT=NULL,
@PageRecord INT=NULL,
@SortExpression NVARCHAR (200)='int_Request_ID',
@SortDirection NVARCHAR (10)='ASC',
@int_Requester_ID INT=NULL,
@intProjectID INT=NULL
AS
BEGIN
SET NOCOUNT ON
DECLARE @StartRowIndex INT
SET @StartRowIndex=((@PageIndex-1)*@PageRecord)+1;
DECLARE @WhrClause VARCHAR(MAX)
SET @WhrClause= 'WHERE tr.int_Requester_ID = '+CONVERT(VARCHAR(MAX), @int_Requester_ID)
IF(@intProjectID>0)
BEGIN
SET @WhrClause=@WhrClause+' AND tr.int_Project_ID='+CONVERT(VARCHAR(MAX),@intProjectID)
END
DECLARE @SelectClause VARCHAR(MAX)
SET @SelectClause=';With AllRecords AS(SELECT Row_Number() OVER(ORDER BY '+CONVERT (VARCHAR (MAX), @SortExpression)+' '+CONVERT (VARCHAR (MAX), @SortDirection)+')AS ''RowNumber'',* FROM(SELECT tr.[int_Request_ID],
tr.[int_User_ID],
tr.[int_Project_ID],
tr.[str_Request_Type],
tr.[int_Account_ID],
tr.[int_Requester_ID],
tr.[int_User_Head_ID],
tr.[dt_Request_Date],
tr.[bln_IsApproved],
tr.[dt_Approval_Date],
tr.[str_Reject_Reason],
tr.[int_USER_ROLE],
tr.[int_AllocationType_ID],
tr.[int_NoofHourInMinute],
xyz.dbo.GetHourByMin(ISNULL(int_NoofHourInMinute,0)) as ''str_NoofHour'',
tr.[dt_StartDateToWork],
tr.[dt_EndDateToWork],
tr.[isAllowToAddTask],
tr.[isAllowToDeleteTask],
tr.[isAllowToAddCR],
tp.str_Project_Name,
tu.str_FullName AS str_User_Name,
tu1.str_FullName AS UserHeader,
tbl_Project_AllocationType.str_AllocationType,
tu2.str_FullName AS UserRequester,
tu2.str_EMAIL_ADDRESS as RequesterEmail
FROM [tbl_Requests] tr
INNER JOIN tbl_PROJECT tp ON tp.int_Project_ID = tr.int_Project_ID
INNER JOIN tbl_USER tu ON tu.int_USER_ID=tr.int_User_ID
LEFT JOIN tbl_USER tu1 ON tu1.int_USER_ID=tr.int_User_Head_ID
INNER JOIN tbl_USER tu2 ON tu2.int_USER_ID=tr.int_Requester_ID
inner join tbl_Project_AllocationType on tbl_Project_AllocationType.int_AllocationType_ID=tr.int_AllocationType_ID '+@WhrClause+'
)As Tmp)
SELECT * FROM
AllRecords WHERE RowNumber BETWEEN ' + CONVERT(VARCHAR(MAX), @StartRowIndex) + ' AND ' + CONVERT(VARCHAR(MAX), (@StartRowIndex + @PageRecord - 1)) + '
SELECT COUNT(TempTbl.int_Request_ID)As ''ReturnRecords'','+CONVERT(VARCHAR(MAX),@PageIndex)+'''PageIndex''
FROM (SELECT tr.[int_Request_ID]
FROM [tbl_Requests] tr '+@WhrClause+')as TempTbl;'
PRINT(@SelectClause)
DECLARE @SQL NVARCHAR(MAX)
SET @SQL=CONVERT(NVARCHAR(MAX),@SelectClause)
EXEC sp_executesql @SQL
END
I am executing it like this.
EXEC sp_tbl_REQUEST_SelectAllByFilter_WithPagging 1,20,NULL,NULL,74,591
You’re passing
NULLfor@SortExpressionand@SortDirection.NULLmeans that the defaults don’t apply – they’re null instead.So the whole concatenation into
@SelectClausebecomesNULL(because concatenating strings andNULLs producesNULLs)So nothing is executed.
Try:
Incidentally, you should avoid using
sp_as a prefix for stored procedures. Thesp_prefix is reserved by MS for system stored procedures, and SQL Server will prefer a system stored procedure frommastervs your own procedure, if there’s a name clash.(I’d generally recommend against using any prefixes in SQL Server, but that’s more of a matter for debate, rather than a strong rule)