I’ve read “When you start writing code that writes code for you, you’ve moved up from a beginner to a professional.” Today I’d like to walk you through an example of where learning to write this kind of code pays off. At my we have a table that defines a hierarchy. Each level on the…
Tag: T-SQL
Get Rid of the Duplicate Records
Time after time I’ll get a table that has duplicates in it and I have to clear out all the duplicate entries while leaving a distinct set of records. There’s a couple of ways to solve the problem. Today I want to take you through my standard fix. Script the source table out to a…
SQL 102 – CLUSTERED vs. NONCLUSTERED Indexes
Previously, I covered the basics of creating an index using CREATE INDEX. Today, I’m going to dig a little deeper. I’d like to teach you the difference in an CLUSTERED versus a NONCLUSTERED index. If you learn nothing else, know that a CLUSTERED INDEX physically orders the table, a NONCLUSTERED INDEX creates another “table” and…
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,…
Find Tables Containing a Certain Column
Today I had to find all the tables that contained a certain column. I was going to rename the column, so it made more sense when you see it in my tables. In order to make the change, I had to know all the tables that had the column. Then, I get to use sp_rename!…
Correlated Subqueries with Comparison Operators
If you haven’t used a sub query yet, I’m surprised. They can be incredibly useful. They can also be a crutch…but like I say time and again, each tool has it’s use. I’d like to go into a little detail on sub queries today by talking about correlated sub queries. Let’s say you wanted to…
Insert Triggers
An insert trigger is a Data Manipulation Language (DML) trigger that acts when a new record is added to the table. CREATE TRIGGER t_tableName_insert ON tableName AFTER|FOR|INSTEAD OF INSERT AS Some bit of T-SQ OK, let’s walk through this. CREATE TRIGGER begins the command. The next part t_tableName_insert is the trigger’s name. I follow a…
Return a Success/Failure to a Job based on a SQL query?
Over on Experts Exchange, someone posted a question I felt I could answer. The user posted a question on how to return a failure and not run a job. I originally took the approach of putting a query at the top of the queries run by the job that would query the table of failure…
Triggers
Triggers are one of the most misunderstood features in Microsoft SQL server. Most of that misunderstanding comes from people implementing triggers with little understanding to their impact. Even worse, they’ll implement them without testing. And the worst of all, not using them, when the functionality being requested is the very definition of what a trigger…
SQL Session – What’s new in Katmai (SQL Server 2008)
If you’re not already a member of the Charlotte SQL Server User Group, go now and sign up. This group is a great resource of knowledge and connections to the SQL Server community in Charlotte, NC. Their plan is to get together once a month and discuss a new and interesting topic on SQL Server….