I’ll be honest, I thought I knew all there was to know about sub-queries. Turns out, there is something new (at least to me). You can modify your sub-query to allow you to pass multiple results. The way you do this is you add (ANY | SOME) or ALL to the query. Let me illustrate….
Tag: SQL301
Equals Sub Queries Versus Exists Sub Queries
I’d like to introduce you to the idea that you can replace an equal sub query with an exists sub query. Doing this can change a seek operation to a scan operation. This usually results in a faster query, with fewer resources used. Check out this example using the adventureworks database. USE AdventureWorks GO –…
Sorting Numbers in VarChar field
Over at Experts-Exchange, I saw a question I get every once in a while. It all has to do with how SQL Server orders data in a VARCHAR column. SQL Server tries to sort a VARCHAR column in a dictionary order, 0 comes before 1. The problem is you may have mixed numbers in your…
SQL 301 – GROUPING
In our previous lesson we covered WITH ROLLUP and WITH CUBE. During that lesson you may have noticed that when we saw the summary rows, the columns had NULL as the value. Hopefully you wondered to yourself, how do I know programatically which of the rows are summary lines and which are rows that just…
SQL 301 — WITH CUBE and ROLLUP
Have you been asked to summarize some data into a nice report for management? They want to see subtotals and grand totals you say? You don’t have to drop the results of your GROUP BY statement into a temp table, and summarize the subtotals yourself. The SQL ROLLUP and CUBE commands offer a valuable tool…
ASC, DESC, and Filtered Views
I know I’ve covered quite a bit of the CREATE INDEX statement, but there is even more to learn. Today I want to cover ASC, DESC, and the WHERE clause for indexes. ASC | DESC By default, indexes are created in ascending (ASC) order. This is fine most of the time. You’re going to be…
SET TRANSACTION ISOLATION LEVEL
Like I’ve mentioned before, unless you use SET TRANSACTION ISOLATION LEVEL, the server will default to READ COMMITTED. What this means is by default, your queries will only be able to interact with records that have been committed. If another query is affecting those records, you’re not able to see them in any way. I…
SQL 301 – Views
At this point I’m sure you know how to CREATE and ALTER a VIEW. But today I’d like to go into a little more detail on views. I’d like to cover updatable views, and some additional options you can add to your CREATE or ALTER VIEW statements. Updatable Views You can write insert s, updates,…
sp_executesql
I’m pretty sure this is the first time I’ve brought up dynamically generated SQL statements. But it’s time you learn about them. There are times where you’ll need to run the same statement against multiple tables. Or agains multiple databases. Sometimes, you may not know how many columns you’ll need to create or select during…
Intro to XML, Part The Second
Last time I gave you a short introduction into XML. I covered the very beginning of what it is, what you use it for, and I even showed you an example file. This time, I’d like to show you how to work with your XML in a web browser, Then I’d like to show you…