select @Sum= sum(Totalpay) from bcs_Sale;
if @strWhere <> ''
begin
    exec('select @Sum=sum(Totalpay) from bcs_Sale where '+@strWhere);
end
执行后报错:
消息 137,级别 15,状态 1,第 1 行
必须声明变量 '@Sum'。