Category: T-SQL

T-SQL & Development

Stupid T-SQL Tricks

Presented without comment:
[crayon-6a79da7df3e7f733242397/]
Next up, can you break up a query with spaces? Yep:
[crayon-6a79da7df3e89855658646/]
Well if you can do that - can you break up a query across lines? Sure you can:
[crayon-6a79da7df3e8b490604531/]
And now, brace yourself: this one is so weird that I can't even embed it in the blog. I'm just going to show you a picture of it first:

Read more about Stupid T-SQL Tricks 33 comments — Join the discussion
T-SQL & Development

Fifteen Things I Hate About ISNUMERIC

Yello!
[crayon-6a79da7e00c86915075917/]
Thanks for reading!

Brent says: the funny part to me is that if you just try to union all of them, SQL Server throws its hands up:
[crayon-6a79da7e00c8d067587124/]
Result:
[crayon-6a79da7e00c91065024156/]
COME ON SQL SERVER YOU JUST TOLD ME THEY WERE ALL NUMERIC, dammit, convert to numeric for me if you're so smart.

Read more about Fifteen Things I Hate About ISNUMERIC 22 comments — Join the discussion
T-SQL & Development

Using WITH (NOEXPAND) to Get Parallelism with Scalar UDFs in Indexed Views

Scalar functions are the butt of everybody's jokes: their costs are wrong, their STATS IO results are wrong, they stop parallelism when they're in check constraints, their stats are wrong in 2017 CU3, they stop parallelism in index rebuilds and CHECKDB, I could go on and on.

Recently, we ran across yet another scenario where scalar UDFs were killing performance.

Read more about Using WITH (NOEXPAND) to Get Parallelism with Scalar UDFs in Indexed Views 7 comments — Join the discussion
T-SQL & Development

Creating Insert Triggers to Silently Ignore Data You Don’t Want

Say you've got an application that insists on inserting data into the database, and...you don't want the data.

You want the application to THINK it inserted the data - you don't want to roll it back or return an error to the end user. You just don't want the data, and you don't want the hassle of deleting it later.

Read more about Creating Insert Triggers to Silently Ignore Data You Don’t Want 19 comments — Join the discussion
T-SQL & Development

Using LIKE on Integers Gets You Implicit Conversion

Using the Stack Overflow public export, take these two queries looking for a particular user by Id (the clustering key):
[crayon-6a79da7e02638319867336/]
The first one (=) gets a clustered index seek, does just 3 logical reads, and correctly estimates that only 1 row will be returned.

The second one (LIKE) does a clustered index scan, reads the entire table, and wildly overestimates that 475,005 rows will be returned - even though only 1 row will.

Read more about Using LIKE on Integers Gets You Implicit Conversion 5 comments — Join the discussion
T-SQL & Development

Don’t Use Scalar Functions in Views.

The short story: if your view has a scalar user-defined function it it, any query that calls the view will go single-threaded, even if the query doesn't reference the scalar function. Now for the long story.

Quite often people will inherit and rely on views written back in the dark ages, before people were aware of the deleterious effects that scalar valued functions can have on performance.

Read more about Don’t Use Scalar Functions in Views. 3 comments — Join the discussion
T-SQL & Development

No seriously, don’t use SQL_VARIANT for that

I'd been meaning to write this for a while I half-stumbled on the weirdness around SQL_VARIANT a while back while writing another post about implicit conversion. What I didn't get into at the time is that it can give you incorrect results. When I see people using SQL_VARIANT, it's often in dynamic SQL, when they don't…

Read more about No seriously, don’t use SQL_VARIANT for that 13 comments — Join the discussion
Performance Tuning

Inline Table Valued Functions: Parameter Snorting

You've probably heard about parameter sniffing
But there's an even more insidious menace out there: Parameter Snorting.

It goes beyond ordinary parameter sniffing, where SQL at least tried to come up with a good plan for something once upon a compile. In these cases, it just plain gives up and throws a garbage number at you. You've seen it happen countless times with Table Variables, Local Variables, non-SARGable queries, catch-all queries, and many more poorly thunked query patterns.

Read more about Inline Table Valued Functions: Parameter Snorting 21 comments — Join the discussion
Performance Tuning

High Compiles and Multi-Statement Table Valued Functions

Way back in 2016 I wrote about what to do if sp_BlitzFirst warns about high compiles. During GroupBy, Adam Machanic gave a great talk on new features in SQL Server 2016. It reminded me of a blog post I wanted to write about one common culprit of high compiles: Multi-Statement Table Valued Functions. Leaving aside…

Read more about High Compiles and Multi-Statement Table Valued Functions 2 comments — Join the discussion
T-SQL & Development

Your Favorite Bugs & Enhancement Requests: #TSQL2sday 86 Roundup

I dunno about you, but I got a big stocking full of coal. Next year, I'm gonna be better, and I plan on asking Santa for a whole bunch of Connect requests. For T-SQL Tuesday, I asked you to name your favorite SQL Server bugs & enhancement requests, and here's what you want in your stocking next year.

Read more about Your Favorite Bugs & Enhancement Requests: #TSQL2sday 86 Roundup 18 comments — Join the discussion