question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Unpreparing a statement after use

See original GitHub issue

The documentation suggests calling ‘unprepare’ after you execute the prepared statement; but doing this removes the prepared statement from SQL Server thus negating the point of preparing it in the first place.

Preparing the statement, creates an execution plan for the query thus making future calls much quicker (3 to 4 times MS says) as it can reuse the plan; unpreparing it actually makes the query slower as it has to do more roundtrips than if you just called it as a normal query.

SQL Server Documentation https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-prepare-transact-sql?view=sql-server-2017

Is my understanding flawed?

-- Prepare query
DECLARE @P1 int;  
EXEC sp_prepare @P1 output,   
    N'@Param int',  
    N'SELECT *
FROM Sales.SalesOrderDetail AS sod
INNER JOIN Production.Product AS p ON sod.ProductID = p.ProductID
WHERE SalesOrderID = @Param
ORDER BY Style DESC;'; 

EXEC sp_execute 1, 49879;  
GO

EXEC sp_execute 1, 48766;
GO

EXEC sp_unprepare 1; 
GO

Software versions

  • NodeJS: v10.15.0
  • node-mssql: v4.3.0
  • SQL Server: Microsoft SQL Server 2016 (SP1) - 13.0.4001.0 (X64)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
danpetittcommented, Jan 29, 2019

Totally understood. Thx for the help

1reaction
dimisuscommented, Jan 23, 2019

I was also confused by the unprepare and I ended up using only prepare and execute… I just push different params inside the prepared query by using input … it brought me up to 5x performance.

Maybe it is a lack of knowledge but I think unprepare is in the docs to show it can be used, but is optional.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DB::unprepared() does no raise exception on 2nd or later ...
I noticed that if I put each statement to its own DB::unprepared() call, it will raise the exception as expected, however, as soon...
Read more >
3 Practice CASPer Questions and Best Responses
It's good to use conditional statements in your CASPer answers. For example: “If the group discovered a serious personal issue in Sue's life,...
Read more >
How to Design an Agenda for an Effective Meeting
Another tactic for creating a better meeting agenda is listing topics as questions to be answered. Instead of writing “office space ...
Read more >
How to Answer The 64 Toughest Interview Questions | OHSU
Keep an interview diary. Right after each interview note what you did right, what could have gone a little better, and what steps...
Read more >
HURRICANE KATRINA: A NATION STILL UNPREPARED
and state officials have known since at least 1994 about the need to address this problem. For its part, the federal government, which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found