我想建立一個SQL查詢與給定的參數,但我得到一個奇怪的錯誤,不能理解爲什麼。這是我的SP,並導致動態SQL查詢搜索
ALTER PROCEDURE [dbo].[sp_Photographers_Select_Search]
@Date varchar(100),
@PriceMin int,
@PriceMax int,
@CityID int
AS
BEGIN
SET DATEFORMAT DMY
DECLARE @SQL as varchar(2000)
SET @SQL = 'SELECT *,
(SELECT TOP (1) Price FROM Packages WHERE PhotographerID = Photographers.PhotographerID ORDER BY Price) as PriceMin,
(SELECT TOP (1) Price FROM Packages WHERE PhotographerID = Photographers.PhotographerID ORDER BY Price DESC) as PriceMax,
(SELECT COUNT(GalleryID) FROM Galleries WHERE PhotographerID = Photographers.PhotographerID AND Status = 1) as GalleryCount,
(SELECT COUNT(CommentID) FROM Comments WHERE ContentID = Photographers.PhotographerID AND Status = 1 AND TypeID = 1) as CommentCount
FROM Photographers WHERE 1 = 1 '
IF @PriceMin <> 0 OR @PriceMax <> 0 BEGIN
SET @SQL = @SQL + ' AND PhotographerID IN(SELECT PhotographerID FROM Packages WHERE Price BETWEEN '[email protected]+' AND '[email protected]+') '
END
IF @CityID > 0
SET @SQL += ' AND CityID = '[email protected]+''
SET @SQL = @SQL + ' AND PhotographerID NOT IN (SELECT PhotographerID FROM Appointments WHERE Date = '''[email protected]+''')'
EXEC (@SQL)
END
EXEC @return_value = [dbo].[sp_Photographers_Select_Search]
@Date = N'23.05.2013',
@PriceMin = 0,
@PriceMax = 0,
@CityID = 34
和錯誤是
Msg 245, Level 16, State 1, Procedure sp_Photographers_Select_Search, Line 23
Conversion failed when converting the varchar value 'SELECT *,
(SELECT TOP (1) Price FROM Packages WHERE PhotographerID = Photographers.PhotographerID ORDER BY Price) as PriceMin,
(SELECT TOP (1) Price FROM Packages WHERE PhotographerID = Photographers.PhotographerID ORDER BY Price DESC) as PriceMax,
(SELECT COUNT(GalleryID) FROM Galleries WHERE PhotographerID = Photographers.PhotographerID AND Status = 1) as GalleryCount,
(SELECT COUNT(CommentID) FROM Comments WHERE ContentID = Photographers.PhotographerID AND Status = 1 AND TypeID = 1) as CommentCount
FROM Photographers WHERE 1 = 1 ' to data type int.
你能描述的錯誤?謝謝!
什麼類型是STATUS列的? – veljasije 2013-02-25 12:59:05
這是smallint,但錯誤行IF IF CityID> 0 SET @SQL + ='AND CityID ='+ CityID +'' – 2013-02-25 13:02:04