在 Scott 的大量帮助下,这就是我最终得到的结果:
CREATE PROCEDURE dbo.testProcedure
(
@searchPhrase nvarchar(500)
)
AS
DECLARE @id int
SET @id = 0;
BEGIN TRY
SET @id = CAST(@id AS int)
END TRY
BEGIN CATCH
END CATCH;
-- at this point, @id will be the primary key if it is only digits
-- otherwise it will default to zero (which is out of range of my identity PK)
WITH ftsTable AS (
SELECT RANK, [KEY] FROM FREETEXTTABLE(sourceTable, *, @searchPhrase)
UNION
SELECT 1001, (SELECT sourceTableID FROM sourceTable WHERE sourceTableID = @id)
)
SELECT sourceTable.*
FROM ftsTable JOIN sourceTable ON ftsTable.[KEY] = sourceTable.sourceTableID
ORDER BY ftsTable.RANK DESC