Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:23:52+00:00 2026-06-12T14:23:52+00:00

I have this query, Declare @Prcocessrate float declare @item varchar(20) declare @process varchar(20) declare

  • 0

I have this query,

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 itemname=@Item and Process=@process and branchid=@branch

when I run it single handed, the execution plan only shows 3 steps, see for youself..

enter image description here

but I have this 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 branchid=@BranchId
            select @ItemId= DefaultItemId from mstconfigsettings where branchid=@BranchId
            select @ItemName=  ItemName from itemmaster where itemid=@ItemId and branchid=@BranchId
            select @ProcessRate =  ProcessPrice from itemwiseprocessrate where itemname=@ItemName and ProcessCode=@ProcessCode and branchid=@BranchId
            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

Now!, when I run this

exec sp_newbooking
@flag = 32,
@Branchid = 1

The execution plan is showing 6 steps! Here’s the picture..!
See the query 4

Why is it taking 6 steps to perform the same query when executing from the procedure, while its taking 3 steps when executing alone? Wtf is this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:23:53+00:00Added an answer on June 12, 2026 at 2:23 pm

    There are lot of reasons SQL can use different execution plans. It could be :

    • Different parameters for the same queries (you use constants in your first example, the queries in the 2nd might have different values)
    • Different data (meaning you’re running queries on dev & production)
    • Parameter sniffing — more below, but first pass the procedure might have the ‘desired’ parameters
    • Different data types – as @MartinSmith points out, we don’t see the parameter declarations. You could have a variable that doesn’t match the type of the field it’s matched up against.

    Parameter Sniffing
    Stored procedures do ‘parameter sniffing‘ which is a blessing (if it works for you) and a curse (if it works against you). First pass someone searches on Zebr% for zerbrowski. The last name index realizes this is very specific and will return, lets say, 3 rows from a million — so one execution plan is built. With the proc compiled for a low row result, the next search is for S%. Well, S is your most common name and matches 93,543 rows out of 1 million.

    So what can you do?
    There are lots of steps you can take to inspect this….

    • Look carefully at the variable data types, comparing your ad hock query, the proc, and the underlying table (sp_columns mytable).
    • Isolate the moving parts
      • Make sure you’re running in both queries in the same system with the same data
      • Have a trace running to make sure first run of the proc uses the expected parameters
      • Try running your ad hock query with a few different parameters and see how the execution plan changes.
      • If you can’t isolate activity, temporarily add WITH RECOMPILE to the proc to compare execution plans. Alternately, do a DBCC FREEPROCCACHE just before running. (DISCLAIMER – if this is a live system make sure you understand what these will do).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this SQL query: declare @input varchar(20) select * from myTable
I have this SQL query (in T-SQL): --DECLARE @strTerm varchar(300) --SET @strTerm = 'c'
I have query like this DECLARE @A INT SET @A = 22 SELECT Moo,
I have this SQL Server query: DECLARE @HoursUsed float SELECT COLUMN_A ,(SELECT (@HoursUsed =
I have this query... SELECT Distinct([TargetAttributeID]) FROM (SELECT distinct att1.intAttributeID as [TargetAttributeID] FROM AST_tblAttributes
I have a query that works: DECLARE @ProductID int SET @ProductID = '1234' SELECT
I am trying to execute this query: declare @tablename varchar(50) set @tablename = 'test'
Say I have this great query in my stored procedure. Select * from Temp
I have a query: declare @Code nvarchar(100) select @Code=BMW select name from NewCars where
I have the following query: DECLARE @str VARCHAR(500) SET @str = 'Barcode: 22037 CaseSize:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.