i require to run seperate query statements using dynamic strings as such
@TOTALRECORDS INTEGER =0 OUTPUT,
@SubCategoryID INT =0
set @WHERECLAUSE =' (Products.CategoryID = @SubCategoryID) '
set @TOTALRECORDS ='count(*) FROM Products WHERE (Products.CATEGORYID=@SubCategoryID )'
SET @sql = 'SELECT *
FROM
(SELECT [ProductID],[VendorName],
Products.[CategoryID],Products.[Category],
ROW_NUMBER() OVER(ORDER BY @sortExpression ) as RowNum FROM Products ' + @WHERECLAUSE + ' ) AS EmpInfo '
Execute sp_Executesql @SQL
I cannot get it to return the output value ?

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 28, 2022, 5:02 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 15, 2022, 12:11 AM EST
peterRank #5,346 · 302 reputationPosted Jan 15, 2022, 10:33 AM EST
I am unable to respond?
peterRank #5,346 · 302 reputationPosted Jan 15, 2022, 10:49 AM EST
There is a problem with this site.It frequently does not allow editing and pasting text on reply?
i use dynamic string because of variable data set on condition of null statements and pagation as described.
Yes,its for pagation purpose therefore cannot get record count from returned datarows only a subset value.
Not certain of SQL version operating as i have multiple versions in existance on system. I think they are causing connection problems; for instance i am unable to test performance of SP from the designer window? As it says file not found or incompatible datafile?
I believe the location is displaces and i cannot reset using the configstring settings per norm.Although the Data retrieval operates fine!
If i remove the quote marks i cannot SET the code unless i use a SELECT clause. When i want to append to the query on a where filter on condition statement action.
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 15, 2022, 1:51 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 15, 2022, 11:46 PM EST
Is the above posted code part of a stored procedure? If yes, it is better to show entire stored procedure, may be removing irrelevant part to the question.
Where is @sortExpression coming from?
Which part of null you intend to handle? For example, do you want all products if @SubcategoryId is null? If yes, there is better way to handle it. You don't necessary need a dynamic SQL.
For example, you can have WHERE clause as follows:
where Products.CategoryId = isnull(@CategoryId, Products.CategoryId)
In above case all products will be selected if @CategoryId is null.
peterRank #5,346 · 302 reputationPosted Jan 18, 2022, 8:07 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 18, 2022, 11:51 PM EST