Skip to content

Commit

Permalink
Fix spelling, grammar, typo and markdown errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Baldwin committed Aug 1, 2024
1 parent 6cd305e commit c4cdda2
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 79 deletions.
2 changes: 1 addition & 1 deletion _posts/2020-12-30-only-update-rows-that-changed.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ The other thing to note is that the `EXCEPT` operator treats the comparison of `

----

## Let's set up some sample data:
## Let's set up some sample data

```tsql
IF OBJECT_ID('tempdb..#Customer','U') IS NOT NULL DROP TABLE #Customer; --SELECT * FROM #Customer
Expand Down
14 changes: 7 additions & 7 deletions _posts/2021-01-07-use-cross-apply-to-clean-up-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ tags: T-SQL

[DRY...Don't Repeat Yourself.](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself){:target="_blank"}

There are multiple ways to re-use code in SQL, such as [subqueries](https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries){:target="_blank"} and [CTE's](https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql){:target="_blank"}; But I'd like to show you another way utilizing `CROSS APPLY`.
There are multiple ways to re-use code in SQL, such as [subqueries](https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries){:target="_blank"} and [CTEs](https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql){:target="_blank"}; But I'd like to show you another way utilizing `CROSS APPLY`.

Subqueries and CTE's are great, but they're not exactly easy to daisy chain. What if you wanted to declare some kind of "inline variable" that you can assign a formula to, and then reference multiple times?
Subqueries and CTEs are great, but they're not exactly easy to daisy chain. What if you wanted to declare some kind of "inline variable" that you can assign a formula to, and then reference multiple times?

A lot of people who are new to SQL think that you can write something in the `SELECT` clause, assign an alias, and then re-use that alias throughout the query. They soon realize that will throw an error. This is because aliases assigned in the `SELECT` clause are only accessible in the `ORDER BY`.

But, using `CROSS APPLY`, you can sort of achieve this.

----

## Sample data:
## Sample data

```tsql
IF OBJECT_ID('tempdb..#Contact','U') IS NOT NULL DROP TABLE #Contact; --SELECT * FROM #Contact
Expand All @@ -43,7 +43,7 @@ VALUES ('Tyler Durden' , '1973-03-06', 'Wilmington' , 'DE', '(210) 658-5511')

----

## The Challenge...
## The Challenge

Let's try to do something really ugly just for the sake of teaching.

Expand All @@ -57,7 +57,7 @@ You can probably imagine how ugly this is going to get...but think about how you

----

## The conventional way:
## The conventional way

```tsql
SELECT c.ContactID, c.FullName, c.DateOfBirth, c.City, c.[State]
Expand Down Expand Up @@ -92,9 +92,9 @@ WHERE LEN(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(c.PhoneNumber,'(',''),

----

## Using CTE's
## Using CTEs

CTE's are a great tool, allowing you to re-use a table expression multiple times. But they're not great about allowing you to re-use a column expression.
CTEs are a great tool, allowing you to re-use a table expression multiple times. But they're not great about allowing you to re-use a column expression.

```tsql
WITH cte_1 AS (
Expand Down
22 changes: 10 additions & 12 deletions _posts/2021-01-15-raiserror-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags: T-SQL

<style>
pre {
overflow-x: auto;
overflow-x: auto;
}
.note-wrapper {
text-align: left;
Expand Down Expand Up @@ -42,7 +42,6 @@ Documentation Links:
* [Database Engine Error Severities](https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-error-severities){:target="_blank"} - Explains what each of the severities mean and how they should be used depending on the context of the error
* [Database engine errors](https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors){:target="_blank"} - List of all database engine errors by error number. Warning - this page will take a while to load, it's huge


Most common usages (log a message, but don't throw an error):

```tsql
Expand Down Expand Up @@ -158,7 +157,7 @@ Column descriptions:

----

## Lets do a couple demos:
## Lets do a couple demos

### *Flush output buffer using WITH NOWAIT*

Expand Down Expand Up @@ -367,14 +366,14 @@ As expected, the code in the `CATCH` block was never run.
```tsql
-- Severity levels 11 - 19 will switch control to the catch block:
CREATE TABLE #caughterrors (SeverityLevel int)
BEGIN TRY RAISERROR('Raiserror severity 11', 11, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 12', 12, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 13', 13, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 14', 14, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 15', 15, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 16', 16, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 17', 17, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 18', 18, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 11', 11, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 12', 12, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 13', 13, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 14', 14, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 15', 15, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 16', 16, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 17', 17, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 18', 18, 1) WITH NOWAIT END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
BEGIN TRY RAISERROR('Raiserror severity 19', 19, 1) WITH LOG END TRY BEGIN CATCH INSERT INTO #caughterrors VALUES (ERROR_SEVERITY()) END CATCH;
SELECT * FROM #caughterrors c ORDER BY c.SeverityLevel
```
Expand All @@ -383,7 +382,6 @@ SELECT * FROM #caughterrors c ORDER BY c.SeverityLevel

No messages were logged because they were all caught and inserted into the temp table instead.


```tsql
-- Anything above severity level 19 will just kill the connection
-- I'm only including this for completeness
Expand Down
6 changes: 3 additions & 3 deletions _posts/2021-01-21-ssms-keyboard-query-shortcuts.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ If that doesn't make any sense...I don't blame you, it's a difficult thing to ex

If you want to know how to set this up, you can [skip to that section](#setting-it-up), but I wanted to show you some demos on how to use it first. The last thing you want to do is accidentally run something you didn't mean to because some dudes blog showed you a new trick.

### The simple use case:
### The simple use case

Let's say you assign the text: `EXEC sp_whoisactive;` to `Ctrl`+`3`.

Expand All @@ -40,9 +40,9 @@ I think that's pretty simple to understand, so we can probably just move on to t

----

### The more complex use case:
### The more complex use case

Take this string `SELECT TOP(100) * FROM ` (make sure there's a space at the end) and let's say that you've assigned it to `Ctrl`+`7`...
Take this string `SELECT TOP(100) * FROM` (make sure there's a space at the end) and let's say that you've assigned it to `Ctrl`+`7`...

Type this query into a query window and only highlight `sys.columns`

Expand Down
2 changes: 1 addition & 1 deletion _posts/2021-01-31-certifications-and-learning.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Exam day came...and to keep it short...I passed!! And the effort I put into stud

Now that I passed the first exam, I could focus on the second exam. My exam was scheduled for Jan 29th, which gave me 11 days to prepare. This exam, however, I was not nearly as confident taking. All of my work experience was on development...not DBA type work, and this exam covered a lot of areas I knew nothing about. Optimistically, I'd say my experience covered about 30% of the "skills measured" PDF.

Things I knew nothing about prior to this exam: monitoring, extended events, Azure, Resource Governor, columnstore indexes, in-memory OLTP, index maintenance, troubleshooting with system DMV's....Let's just say I had a lot to cover.
Things I knew nothing about prior to this exam: monitoring, extended events, Azure, Resource Governor, columnstore indexes, in-memory OLTP, index maintenance, troubleshooting with system DMVs....Let's just say I had a lot to cover.

I knew in order to have a chance at passing at this exam, I was going to need to put in a lot more work. I ordered the [official 70-762 Exam reference](https://amzn.to/35Re73h){:target="_blank"} from Amazon. It's 368 pages and was expected to arrive in 2 days, giving me 10 days to read it at about 37 pages a day.

Expand Down
2 changes: 1 addition & 1 deletion _posts/2021-02-08-use-merge-to-split-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Let's go through this. For the most part, it's just a simple boring merge statem

First, we've hard coded `1 = 0` in for the join logic. This is to force the merge to **always** jump to the `WHEN NOT MATCHED BY TARGET` part of the merge, since all we care about is performing inserts.

Second, we've added an `OUTPUT` statement returning both the original identity PK value from `#Source` *as well as* the new identity value from `Table1`. Thus giving us a perfect mapping of our source data to the new identity values!
Second, we've added an `OUTPUT` statement returning both the original identity PK value from `#Source` _as well as_ the new identity value from `Table1`. Thus giving us a perfect mapping of our source data to the new identity values!

Now that we've got `Table1` populated, lets finish it up and populate `Table2` using our ID Mapping table to include the FK values.

Expand Down
2 changes: 1 addition & 1 deletion _posts/2021-02-17-theres-no-way-that-will-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ The goal was to write a piece of code that will generate a tree...like so...

If N=3 then the result should be:

```
```plaintext
*
***
*****
Expand Down
24 changes: 13 additions & 11 deletions _posts/2021-03-13-how-to-build-a-sql-blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ Thanks!

This was me, 3 months ago. I played around with various blogging platforms and they seemed okay, but for some reason, none of them really did what I wanted. Eventually I learned about [GitHub Pages](https://pages.github.com/){:target="_blank"}. It's a service provided by GitHub; They allow you to host a static website (no backend code), and they host it **for free**.

GitHub Pages is great, _but_ when you start learning "how to blog using Jekyll", things get pretty overwhelming...You start learning about ruby and Jekyll and how to get them working on Windows (which is a pain), learning languages like YAML and Liquid...It gets complicated real quick.
GitHub Pages is great, *but* when you start learning "how to blog using Jekyll", things get pretty overwhelming...You start learning about ruby and Jekyll and how to get them working on Windows (which is a pain), learning languages like YAML and Liquid...It gets complicated real quick.

### Here's what I've done

I've taken _this_ blog, stripped out unnecessary extras and converted it into a template you can use. I've left in features that I think are helpful for a blogger who wants to cover SQL Server. For example, I built this template to support T-SQL specific syntax highlighting using the SSMS theme.
I've taken *this* blog, stripped out unnecessary extras and converted it into a template you can use. I've left in features that I think are helpful for a blogger who wants to cover SQL Server. For example, I built this template to support T-SQL specific syntax highlighting using the SSMS theme.

Your first blog doesn't need to be a fancy, professionally designed website. You just need good content, and setting one up should be as simple as a few clicks, maybe filling in some personal info, and you're done.

Expand Down Expand Up @@ -130,21 +130,23 @@ That's about all you need to know to get started.
### Lets create a new blog post

1. Navigate to the `_posts` folder on GitHub
2. Click `Add file` > `Create new file`
3. Name your file `{{ site.time | date: '%Y-%m-%d' }}-your-new-blog-post.md`
4. Set the title of your blog post by using a markdown header
* Write this as the first line `## This is my first blog post`
5. Add some content...write some random things, whatever you want
6. Throw in a code block (code blocks are created by surrounding your code snippet with three backticks at each end and an optional "language hint"), copy paste this in:
1. Click `Add file` > `Create new file`
1. Name your file `{{ site.time | date: '%Y-%m-%d' }}-your-new-blog-post.md`
1. Set the title of your blog post by using a markdown header
* Write this as the first line `## This is my first blog post`
1. Add some content...write some random things, whatever you want
1. Throw in a code block (code blocks are created by surrounding your code snippet with three backticks at each end and an optional "language hint"), copy paste this in:

````plaintext
```tsql
SELECT *
FROM sys.tables
WHERE [name] = 'SomeTable'
```
````
* **Important note**, if you're using T-SQL code, make sure to use the `tsql` tag. This will tell your site that you want to use the SSMS style formatting.
7. Click the `Preview` tab so you can see what it looks like so far.

* **Important note**, if you're using T-SQL code, make sure to use the `tsql` tag. This will tell your site that you want to use the SSMS style formatting.
1. Click the `Preview` tab so you can see what it looks like so far.

Here's a full playback of what it would look like:

Expand Down Expand Up @@ -172,4 +174,4 @@ If you're interested in diving deeper into Jekyll, how it works behind the scene

There's lot of things you can do to this template, fairly easily, to add some fun features. Things like a comments section on your posts, adding a dark theme to your entire site or maybe you want to add some custom pages, like [my book tracking page]({{ 'books' | relative_url }}){:target="_blank"} or [my resume]({{ 'resume' | relative_url }}){:target="_blank"}. The possibilities are endless...but, it comes at a cost, it all has to be done manually by writing or copy/pasting code, learning new languages like HTML, CSS, Liquid, YAML...this is where platforms like WordPress starts to shine.

I realize this particular setup may not work for everyone, and in the long run, it may even be more complicated than just sticking with a popular blogging platform, with their fancy UI's, plugins, etc. But I've had a lot of fun learning about GitHub Pages, Jekyll, Markdown, etc, so I figured I'd share how I got started, and try to make it easier for you if this is the route you're thinking of going.
I realize this particular setup may not work for everyone, and in the long run, it may even be more complicated than just sticking with a popular blogging platform, with their fancy UIs, plugins, etc. But I've had a lot of fun learning about GitHub Pages, Jekyll, Markdown, etc, so I figured I'd share how I got started, and try to make it easier for you if this is the route you're thinking of going.
4 changes: 3 additions & 1 deletion _posts/2021-03-23-quick-parse-csv-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ I killed notepad++.exe and moved on to try another method. It probably would hav
PowerShell only requires two commands to do this. The downside is, it loads the whole file into memory, and generally doesn't release that memory. So if it's a huge 2GB file, it's not a great option. In this case, I'm okay with loading a 50MB file into memory.

1. Load the data into a local variable using the `Import-Csv` cmdlet

```ps
$data = Import-Csv -Delimiter '|' -Path .\file.txt -Header 'c1','c2','c3','c4','c5','c6','c7','c8','c9'
```

2. Get list of unique values from column 3

```ps
$data | select c3 -Unique
```
Expand All @@ -114,7 +116,7 @@ In this case, I only had about 10 unique values, so I did it manually.
>
> In my example above, I'm writing the contents to a variable, so it definitely will allocate enough memory **at least** the size of the file. I did this to make it easier to explain, but I should include a clean one-liner to show how to do it.
>
> u/bis also pointed out that you can use `Set-Clipbard` to save the result to the clipboard directly.
> u/bis also pointed out that you can use `Set-Clipboard` to save the result to the clipboard directly.
With regard to the notes above...and using some additional PowerShell syntactic shortcuts, this is a nifty one liner that will do the job:

Expand Down
2 changes: 1 addition & 1 deletion _posts/2021-05-24-powershell-pipelinevariable.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PS> 1,2,3 | Invoke-AddOne

Looks good, we passed in 1,2,3 and got back 2,3,4.

Now, lets test using `-PipeLinevariable`
Now, lets test using `-PipelineVariable`

```powershell
PS> 1,2,3 | Invoke-AddOne -PV test | % { $test }
Expand Down
Loading

0 comments on commit c4cdda2

Please sign in to comment.