Content

SQL Category

Setting a Column to Itself in a Where Clause

17 Aug

+ 0

Many developers don't realize how handy it can be to set a column to itself in a WHERE clause in SQL.

  Leave Comment, 0 Comments

Export your Data In Sql Server Using A Stored Procedure

23 Jul

+ 0

This is seriously one of the coolest stored procedures there is.

  Leave Comment, 0 Comments

Find All Children of Parent in Stored Procedure

20 Jul

+ 3

Given a Parent ID, what is the best way to return all child records, their child records, ad inifitum?

I needed this to find all employees beneath a given manager no matter how deep.

  Leave Comment, 3 Comments

How To Debug Stored Procedures

17 Jun

+ 1

Post in

  Leave Comment, 1 Comment

Sql Server Templates

10 Jun

+ 1

I keep saying this, but I can't believe I have been programming in Sql Server for this many years, and never heard of templates.

While in Query Analyzer, hit Ctrl-Shift-Ins.

  Leave Comment, 1 Comment

Shrink Sql Server Database and Transaction Log

8 Jun

+ 0

Shrinking the transaction log was so very easy once I figured it out.  All you need to do:

1. Backup your database just in case.
2. Run the following. If your database is called “Vampires”, then Database name will be “Vampires” and most likely your log file will be “Vampires_Log”:

USE <DATABASE NAME>
BACKUP LOG <DATABASE NAME> WITH TRUNCATE_ONLY
DBCC SHRINKFILE(<LOG FILE NAME >, 2)

3. To shrink my database, I first reindexed all the tables, then ran the following. If your database is called “Vampires”, then your database file name will most likely be “Vampires_Data” :

  Leave Comment, 0 Comments

Sql Server Reindex Tables

8 Jun

+ 4

During my attempt to shrink my database so that it would fit on my laptop (it was 20 gigs), I figured out that I needed to reindex all my tables.  Usually the format of your data and log files will follow this pattern – just replace the {DATABASE_NAME} with the name of your database.

DBCC SHRINKFILE ({DATABASE_NAME}_data,5)

DBCC SHRINKFILE (
{DATABASE_NAME}_log, 1);
BACKUP LOG
{DATABASE_NAME} WITH TRUNCATE_ONLY;
DBCC SHRINKFILE (
{DATABASE_NAME}_log, 1);

  Leave Comment, 4 Comments

Inserting Nulls Into Sql Table

4 Jun

+ 1

Countless times I have needed this information, and countless times I can't remember it and have to go searching the net for it.

So here it is so Google can search it easily.

It is a keyboard hot key to enter a null value in a Sql Enterprise Manager field.

Tab to the column and hit -0 (zero). Tab away and a null will show.

Very handy.

  Leave Comment, 1 Comment

Using NOLOCK with SELECT statements

11 May

+ 2

Holy cow! Where have I been living?

You can use NOLOCK with SELECT statements to tell SQL to not lock the row/table/page/database.

  Leave Comment, 2 Comments