0
我有这个疑问,sql在运行查询时以及在存储过程中运行该查询时使用不同的执行计划?
Declare @Prcocessrate float
declare @item varchar(20)
declare @process varchar(20)
declare @branch varchar(20)
set @item = 'shirt'
set @process = 'kt'
set @branch = '1'
select @Prcocessrate = ProcessPrice from itemwiseprocessrate where [email protected] and [email protected] and [email protected]
当我运行它单手,执行计划只显示3个步骤,请参阅youself ..
,但我有这个procedure
sp_newBooking
as
ALTER PROC sp_newbooking
-- other arguements--
AS
BEGIN
--OTHER FLAGS--
ELSE IF (@Flag = 32)
BEGIN
declare @ItemId varchar(max),@ProcessRate float
--set @BranchId='1'
select @ProcessCode = DefaultProcessCode from mstconfigsettings where [email protected]
select @ItemId= DefaultItemId from mstconfigsettings where [email protected]
select @ItemName= ItemName from itemmaster where [email protected] and [email protected]
select @ProcessRate = ProcessPrice from itemwiseprocessrate where [email protected] and [email protected] and [email protected]
if(@ProcessRate is not null)
select @ItemName as ItemName,@ProcessCode as ProcessCode,@ProcessRate as ProcessRate
else
select @ItemName as ItemName,@ProcessCode as ProcessCode,'0' as ProcessRate
END
-- OTHER FLAGS --
END
否当我运行这个
exec sp_newbooking
@flag = 32,
@Branchid = 1
执行计划显示6个步骤!这里是图片..!
参见查询4
为何需要6个步骤,以执行相同的查询从过程执行时,而它的单独执行时采取3个步骤是什么?这是什么?
可能是一种隐含的转换。第二个计划中的计算标量是什么? (在这个属性窗口中查看)。存储过程参数是否与第一个查询中的变量具有相同的数据类型? –
我认为@MartinSmith是正确的。当您在proc外部运行查询时,您正在使用值为'1'的@ branch参数的varchar(20)类型。当你调用proc时,你指定@branchid = 1。在那里看到1没有引号。在调用sp时,SQL Server将值1(它是一个int)转换为一个varchar。我怀疑,如果你将你的SP称为'EXEC sp_newbooking @ flag = 32,@ branchID ='1'',你会得到相同的执行计划。 – Tobsey