Stored Procedures and Dynamic SQL

Developing and debugging stored procedures, dynamic SQL, and related code.

34 associated posts33 primary posts

[Video] Office Hours in Osaka, Japan

Kon'nichiwa! My vacation tour continues in Osaka, Japan, a city famous for its street food, nightlife, and host and hostess clubs. I'm usually the kinda guy who does classy dinners at 5PM and then hits the sack by 9PM at the latest, so you might wonder how I got here. The answer is that Yves planned this trip out, hahaha, so here we are. As long as we're here, let's go through your top-voted questions from https://pollgab.com/room/brento.

Read more about [Video] Office Hours in Osaka, Japan 9 comments — Join the discussion

Office Hours: Read This, Watch This Edition

This week, a lot of the questions you posted at https://pollgab.com/room/brento just need pointers to resources at other places. Welcome to the Read This, Watch This series of answers.

Don't Blame Anthony: Our developers prefer writing TSQL queries directly in their .NET code rather than writing sprocs and calling them. What are the pros / cons to this approach?

Read more about Office Hours: Read This, Watch This Edition 1 comment — Join the discussion
T-SQL & Development

#tsql2sday: Start Your Dynamic SQL with a Comment.

When you write dynamic SQL, start like this:
[crayon-6a6ecdeae2c82504667066/]
Right after the SELECT (or INSERT or UPDATE or whatever), immediately put a comment - using /*, of course, because you're not a terrible person.

That way, when you're looking at the plan cache or monitoring tools, you can see what generated the dynamic SQL, and where you need to go if you need to performance tune or fix it.

Read more about #tsql2sday: Start Your Dynamic SQL with a Comment. 6 comments — Join the discussion
Production DBA

Office Hours: Gif Reactions Edition

Some of the questions y'all post at https://pollgab.com/room/brento are open-and-shut cases. I was going to do a one-word-answers edition, but I think this is even better: gifs.

GI Joe DBA: Thoughts about IT professionals Unionizing? IT professionals are in demand. Because of our demand, should we use this leverage to unionize, raise the bar, force better health benefits, retirement benefits, etc for ALL.. US Citizens? Ever discuss this with colleagues?

Read more about Office Hours: Gif Reactions Edition 4 comments — Join the discussion
T-SQL & Development

[Video] Fundamentals of Stored Procedures at SQLBits

Anybody can write a stored procedure with a little help from Google. This session is about how to write stored procedures that have a high likelihood of performing well and are easy to troubleshoot.

This fast-paced, all-demo session from SQLBits will NOT cover how to write a query, syntax, or performance tuning. This is about good best practices after you've written the first one - things like how to catch errors, how to pass in multiple values, how to debug without the debugger, and more.

Read more about [Video] Fundamentals of Stored Procedures at SQLBits 12 comments — Join the discussion

[Video] Office Hours: Ask Me Anything About SQL Server in Stokkseyri

Before Fjöruborðið opens for lunch, I answer a few of your questions from https://pollgab.com/room/brento including:

00:00 Introductions
00:29 Peter: What is your opinion about the SQL generated by Entity Framework?
02:00 Peter: When would you switch from EF to stored procedures?
02:45 Always Learning DBA: What are free resources to help me transition into an architect or data scientist role?
04:43 How is the auditing and monitoring different for Azure SQL DB?
06:33 Recap

Read more about [Video] Office Hours: Ask Me Anything About SQL Server in Stokkseyri 2 comments — Join the discussion
T-SQL & Development

Forced Parameterization Doesn’t Work on Partially Parameterized Queries.

Over and over again, I run into applications that don't parameterize their queries, and as a result: Compilations/sec is nearly as high as Batch Requests/sec CPU is high due to all those compilations Plans rarely last for more than a few hours in the cache Monitoring tools that track resource-intensive queries are useless because every…

Read more about Forced Parameterization Doesn’t Work on Partially Parameterized Queries. 3 comments — Join the discussion
T-SQL & Development

Should we use stored procedures or queries built in the app?

A client asked a great architecture question that deserved its own blog post:
Should we use more "stored procedures" for select, insert, update, delete or should that just be sent as queries from the application?
If you ask a junior database administrator where to put something, she'll probably say "in the database" because that's the thing she has control over. That's fair - DBAs love having control over stuff. However, let's step back a little.

Read more about Should we use stored procedures or queries built in the app? 71 comments — Join the discussion
T-SQL & Development

Tuning Dynamic SQL by Hand with Short Circuits

When we think about building dynamic SQL, we usually think about a stored procedure like this that takes input parameters, builds a string, and then executes that string. Here's a simple example: [crayon-6a6ecdeae3960130923475/] The more parameters you have, the crazier the code gets, and the harder it becomes to produce really fast plans across all…

Read more about Tuning Dynamic SQL by Hand with Short Circuits 7 comments — Join the discussion
T-SQL & Development

What Is SQL Injection?

Say we have a stored procedure that queries the Stack Overflow database. We have two separate parameters, @DisplayName and @Location, so folks can search for people by name, location, or both.

For performance reasons, we decide to build dynamic SQL:
[crayon-6a6ecdeae3c43995333914/]
When we run it, it works, and we can see the query on the Messages tab in SSMS:

Read more about What Is SQL Injection? 4 comments — Join the discussion

#TSQL2sday: +

For this month's T-SQL Tuesday, He of Perfect Hair, Bert Wagner asked, "What code have you written that you would hate to live without?"

There are a lot of pieces of code I frequently need to reuse when working on the First Responder Kit, like string splitting, XML PATH-ing, and converting milliseconds to some human consumable elements.

Read more about #TSQL2sday: + 5 comments — Join the discussion