日期:2014-05-17  浏览次数:20506 次

跪求,这条语句为什么不能用
SQL code
-- 用完整的产品列表填充该TABLE变量
INSERT INTO @Product
SELECT ROW_NUMBER() OVER (ORDER BY Product.Id),
       Id, Nick,NumIid, TrackIiid,ContactPhone,CategoryId,CreateTime,ModifiedTime,Sorting,EffectiveIntegral,ShareIntegral
FROM Product
WHERE Id > 0
IF @Category > 0
AND CategoryId = @Category 
IF @Key <> ''
AND Nick = @Key 
ORDER BY Id DESC



------解决方案--------------------
大概是这样,我这里没环境,不知道会不会报错,但是思路就是这样了
SQL code
DECLARE @sql varchar(max)
SET @sql='
SELECT ROW_NUMBER() OVER (ORDER BY Product.Id),
       Id, Nick,NumIid, TrackIiid,ContactPhone,CategoryId,CreateTime,ModifiedTime,Sorting,EffectiveIntegral,ShareIntegral
FROM Product
WHERE Id > 0 '
IF @Category > 0
BEGIN
    SET @sql=@sql+' AND CategoryId = @Category '
END
IF @Key <> ''
BEGIN
    SET @sql=@sql+' AND Nick = @Key '
END
SET @sql=@sql+' ORDER BY Id DESC ' 

EXEC @sql