Many developers don't realize how handy it can be to set a column to itself in a WHERE clause in SQL.
Content
SQL Category
Export your Data In Sql Server Using A Stored Procedure
Scott in: Programming, SQL
This is seriously one of the coolest stored procedures there is.
Find All Children of Parent in Stored Procedure
Scott in: Programming, SQL
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.
Sql Server Templates
Scott in: Programming, SQL
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.
Shrink Sql Server Database and Transaction Log
Scott in: Programming, SQL
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” :
Sql Server Reindex Tables
Scott in: Programming, SQL
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);
Inserting Nulls Into Sql Table
Scott in: Programming, SQL
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
Very handy.
Using NOLOCK with SELECT statements
Scott in: Programming, SQL
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.












