我写了这个存储过程:
CREATE PROCEDURE [dbo].[spGetCuisines]
@RestaurantID INT ,
@CuisineID NVARCHAR(200)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX)
SET @sql = 'SELECT CuisineID, CuisineName
FROM dbo.Cuisine1
WHERE CuisineID IN (
SELECT dbo.Dishes1.CuisineID
FROM dbo.Dishes1
WHERE DishID IN ( SELECT DishID
FROM dbo.RestaurantDish
WHERE RestaurantID = '
+ CAST(@RestaurantID AS NVARCHAR(MAX)) + ' ) )'
IF @CuisineID <> ''
BEGIN
SET @sql += 'AND Cuisine1.CuisineID IN('
+ CAST(@CuisineID AS NVARCHAR(MAX)) +')'
END
EXECUTE sp_executesql @sql;
END
我正在使用 3 个表,其列如下:
菜品1
DishID
DishName
CuisineID
Price
美食1
CuisineID
CuisineName
Type
DateCreated
DateModified
DateDeleted
餐厅Dish
RestaurantDishID
RestaurantID
DishID
但是我的存储过程在这一行给了我语法错误:
SET @sql += 'AND Cuisine1.CuisineID IN('+ CAST(@CuisineID AS NVARCHAR(MAX)) +')'
它说:
“+”附近的语法不正确
有人可以指导我吗?SQL Server 版本是否与此有关?