I am trying to delete oldest records after the last n records where n could be 5 or 6 (this would be passed in).
I tried using the SET ROWCOUNT @Top to set the row count but that doesn't work since it would set it for the whole query where I only want it for the subquery in my query.
I am trying to do something like:
SELECT *
FROM OldPassword
WHERE UserID = @UserId AND
OldPasswordId NOT IN (
SELECT TOP 4 OldPasswordId
FROM OldPassword
WHERE UserID = @UserId
ORDER BY InsertDate)
This works fine.
But I need the Top 4 to be dynamic and I don't want to write dynamic SQL.
Can this be done?
Thanks,
Tom
I tried using the SET ROWCOUNT @Top to set the row count but that doesn't work since it would set it for the whole query where I only want it for the subquery in my query.
I am trying to do something like:
SELECT *
FROM OldPassword
WHERE UserID = @UserId AND
OldPasswordId NOT IN (
SELECT TOP 4 OldPasswordId
FROM OldPassword
WHERE UserID = @UserId
ORDER BY InsertDate)
This works fine.
But I need the Top 4 to be dynamic and I don't want to write dynamic SQL.
Can this be done?
Thanks,
Tom