我正在尝试在 SQL Server 2008 中创建一个存储过程,该过程在(Chinook,如果有帮助的话)数据库的 Track 表中搜索与专辑标题(部分)匹配的曲目。例如,当参数为“rock”时,我想查看专辑中在某处具有“rock”标题的所有曲目。
硬编码“rock”按预期工作,但从 Management Studio 以“rock”作为参数运行该过程会返回许多非“rock”结果。
这是我用来修改存储过程的脚本:
ALTER PROCEDURE [dbo].[searchTrackAlbumTitle]
@trackAlbumTitle nvarchar
AS
BEGIN
SET NOCOUNT ON;
SELECT Track.TrackId
,Track.Name AS TrackName
,Artist.Name AS ArtistName
,Album.Title AS AlbumTitle
,Track.Composer
,Track.Milliseconds
,Track.Bytes
,MediaType.Name AS TypeName
,Genre.Name AS GenreName
,Track.UnitPrice
FROM Track
LEFT JOIN Album ON Track.AlbumId = Album.AlbumId
LEFT JOIN Artist ON Album.ArtistId = Artist.ArtistId
LEFT JOIN MediaType ON Track.MediaTypeId = MediaType.MediaTypeId
LEFT JOIN Genre ON Track.GenreId = Genre.GenreId
WHERE Album.Title LIKE ( '%' + @trackAlbumTitle + '%' ) -- doesn't work
--WHERE Album.Title LIKE ( '%' + 'rock' + '%' ) -- works
END