Adding All Columns In A Table To A Query [Easy]


I wanted to share this quick tip with you on how to add a list of all the columns in a table to your query window in SQL Server Management Studio. This is the easiest way I know!

  1. Expand the Object Explorer to show the table that you want to add the columns for.
        Adding All Columns 1
  2. Select the Columns folder on to your query window.
        Adding All Columns 2
  3. Drag the whole folder on to your query window and just drop it where you want it. That’s it!
    Adding All Columns 3



If you work with stored procedures or if you script objects in SQL Server, you have probably seen SET statements that turn on/off some crazy thing called ANSI_PADDING. If you’re like most people, you just ignore this because it works. I wanted to take a few minutes to explain what this ANSI_PADDING thing is.

ANSI_PADDING is an option that controls how VARCHAR and VARBINARY values are stored. If ANSI_PADDING is turned on, then SQL Server will NOT trim the trailing spaces when it inserts into a VARCHAR field. Similarly, it will NOT trim trailing nulls when it inserts into a VARBINARY field.

Setting the ANSI_PADDING only affects the spaces on inserts. It does NOT affect comparisons.

Syntax For Setting The ANSI_PADDING:

How To Create A Date Table


Many times when developing SQL Server databases you have the requirement to show all dates in a range. One way to do this is with a date table. One of the most common reasons that I use a date table is when I have a reconciliation report. In a reconciliation report I want to show all of the days in a date range, even if they don’t have data on those days. SQL Server doesn’t have a function to generate dates in a range. So to accomplish this, I create a date table using the following script.


SQL Server Synonyms – What They Are And How To Use Them


What Is A Synonym?

A synonym in SQL Server can be thought of like having another name for a table.  The table doesn’t even have to be in your own database.  The intent of having synonyms is for an easy way to reference objects in another database.  It basically gives you a short alias to call server objects that live in another database.  You can create a synonym for tables, views, stored procedures, and functions!

Example Without Synonym:

Example With Synonym:

In the example above, the synonym AnimalSounds represents FarmAnimals.dbo.SoundsOfAnimals.

NOTE: Because the synonym is basically an alias for a different server object, INSERT, UPDATE, SELECT, DELETE operations happen directly on the base table and not on the synonym.

Why Would You Use A Synonym?

There are 2 main reasons for using synonyms.  The first reason is to make the code a little easier to read.  It makes all of the remote objects look like local objects.  The second reason is that it creates a layer of abstraction.  If the base table changes, then you only need to change the synonym.  If you did not use a synonym, then you would need to go in to every stored procedure, view, function, etc… and change the reference to the new table.

Real-Life Use

So… although it is a neat concept, I really don’t use synonyms very much.  The one application where I see it very useful is when you do not own the database that you are referencing (like if you bought a 3rd party product).  Product updates could change the table/stored procedure names… if you use a synonym to reference the database tables, then it would only take 1 change on your side to get your program working again.



How To Use The OUTPUT Clause


The OUTPUT clause is an amazing part of SQL Server that many people do not know about.  As corny as it is, I still remember the day that I found it.  I had been searching for the whole day trying to figure out how to get the identity values from a large amount of data that I just inserted in to the database.  There had to be a way… right?  After searching and searching, I gave up and just accepted that it is not possible… a few days later I found exactly what I was looking for… the OUTPUT clause.

The OUTPUT clause is a part of the query that will return data from before or after the operations is completed.  Let’s say that you inserted data in to a table and you wanted the ID column values (which are auto-numbers).  The OUTPUT clause gives you this information!  It can work on INSERT, DELETE, UPDATE, and MERGE statements.  I will take you through examples of each.

To access the data that is being altered in your sql statement, you need to use special column prefixes that SQL Server makes available to you.  The two special prefixes are “inserted” and “deleted”.  During an insert statement, the inserted prefix is available for you to use.  During a delete statement, the deleted prefix is available to you.  During the update and merge statements, both the deleted and inserted prefixes are available to you.  In these cases, the deleted represents the data before it was changed and the inserted represents the data after it was changed.

One thing to note is that the data being outputted must go in to a table or table variable.

OUTPUT Clause On An INSERT Statement

OUTPUT Clause On A DELETE Statement

OUTPUT Clause On An UPDATE Statement

OUTPUT Clause On A MERGE Statement