Jan 03, 2017 05:52 PM|PatriceSc|LINK
Is this your actual code ? I don"t see @AssetID anywhere in the code you are showing. I would keep @PeriodEnding as a datetime (rather than as nvarchar(75)).
For the date criteria I would do something such as :
DECLARE @YearStart DATETIME
SET @YearStart=CAST(YEAR(@PeriodEnding) AS VARCHAR)+'0101' -- January 1st for the same year
And then something such as :
Set @sql = 'select *
FROM ' + @TableName +
WHERE (ID = @ID)
AND nueDate >= @YearStart
AND nueDate <= @PeriodEnding
exec sp_executesql @sql,
@PeriodEnding = @PeriodEnding,
@ID = @ID
It should be more efficient if you have an index on the nuedate column (maybe a typo for duedate ?)
Edit: you don't pass @AssetID as a table name parameter ???!!!