Integrar la búsqueda de texto y los predicados de Transact-SQL
Los predicados CONTAINS y FREETEXT pueden combinarse con cualquier otro predicado de Transact-SQL, como LIKE y BETWEEN; también pueden utilizarse en una subconsulta. En este ejemplo se buscan descripciones cuyo Id. no sea igual a 5 y cuya descripción contenga las palabras "Aluminum" y "spindle".
USE AdventureWorks;
GO
SELECT Description
FROM Production.ProductDescription
WHERE ProductDescriptionID <> 5 AND
CONTAINS(Description, ' Aluminum AND spindle');
GO
En la siguiente consulta se usa CONTAINS dentro de una subconsulta. Si se utiliza la base de datos AdventureWorks, la consulta obtiene el valor de comentario de todos los comentarios de la tabla ProductReview en un determinado ciclo.
USE AdventureWorks;
GO
INSERT INTO Production.ProductReview
(ProductID, ReviewerName, EmailAddress, Rating, Comments)
VALUES
(780, 'John Smith', 'john@fourthcoffee.com', 5,
'The Mountain-200 Silver from AdventureWorks Cycles meets and exceeds expectations. I enjoyed the smooth ride down the roads of Redmond')
-- Given the full-text catalog for these tables is Adv_ft_ctlg,
-- with change_tracking on so that the full-text indexes are updated automatically.
WAITFOR DELAY '00:00:30'
-- Wait 30 seconds to make sure that the full-text index gets updated.
SELECT r.Comments, p.Name
FROM Production.ProductReview r
JOIN Production.Product p
ON
r.ProductID = p.ProductID
AND r.ProductID = (SELECT ProductID
FROM Production.ProductReview
WHERE CONTAINS (Comments,
' AdventureWorks AND
Redmond AND
"Mountain-200 Silver" '))
GO
Vea también
Otros recursos
CONTAINS (Transact-SQL)
FREETEXT (Transact-SQL)
WHERE (Transact-SQL)