February 2009 - Tip of the Week

  • How to allow multiple NULL values in a UNIQUE column

    SQL Server can guarantee unique values in two ways, Primary keys and a unique indexes/constraints. The primary difference between the two is that fields bound to a unique index or constraint can be NULL while a primary key field can not. For the purpose of the unique indexes and constraints, NULL values are treated equal. That means that you cannot have two NULL values. Fortunately, there is a workaround for that will allow you to store multiple NULL values while ensuring that non-NULL values are...
  • Viewing SQL Server Jobs and History

    In SQL Server 2000, it was cumbersome to allow those who were not members of the sysadmin group the ability to view SQL Agent Jobs and job history through Enterprise Manager. SQL Server 2005 has made this a lot easier through the use of specialized database roles in the msdb database for viewing such information. In the scripts below, I get around these SQL 2000 restrictions with a couple of custom objects that allow you to view SQL Agent Job status and history. First, I need to create a function...
  • Server Role Members

    Below is a quick and easy way to query SQL Server 2005 system views to determine which users belong to which server roles on the instance. SELECT rolename = rolep.name, membername = memp.name FROM sys.server_role_members rm JOIN sys.server_principals rolep ON rm.role_principal_id = rolep.principal_id JOIN sys.server_principals memp ON rm.member_principal_id = memp.principal_id To determine which users are members of your database level roles on your instance, see this article .
  • Database Role Members

    Below is a quick and easy way to query SQL Server 2005 system views to determine which users belong to which database roles on your SQL Server databases. SELECT rolename = rolep.name, membername = memp.name FROM sys.database_role_members rm JOIN sys.database_principals rolep ON rm.role_principal_id = rolep.principal_id JOIN sys.database_principals memp ON rm.member_principal_id = memp.principal_id To determine which users are members of your server level roles on your instance, see this article ...
  • Using Table Aliases

    Table aliases are an often overlooked, underused, and confusing facet of database management systems. While table alaises may be confusing at first, they are wonderful for making queries easier to read, and easier to write. A table alias is nothing more than assigning a pseudo name to a table name in a SQL statement. For example, the following SELECT statement DOES NOT use a table alias because it is not helpful to do so. Table aliases are most helpful when you're joining two or more tables and...
  • Deleting Duplicate Values in SQL Server 2005

    SQL Server 2005 provides a very easy way to remove duplicate values through the use of a Common Table Expression (CTE). The script below creates a temp table named #PendingAccounts. Notice that I will check in the tempdb to see if the table already exists. If it does, I will drop it. IF OBJECT_ID('tempdb..#PendingAccounts') IS NOT NULL DROP TABLE #PendingAccounts GO CREATE TABLE #PendingAccounts ( AccountID INT NOT NULL, PendingDate SMALLDATETIME, PendingAmount MONEY ) GO Below I am adding...
  • Management Studio Quick Keys

    SQL Server Management Studio provides some neat shortcut features that makes it very quick easy to run certain stored procedures if you're in a hurry. Using these shortcut keys, you can assign administrative procedures to certain keyboard combinations to allow for quick execution without having to type the procedure name. First, from the Tools menu, select Options. Once on the Options screen, select the Keyboard option in the Environment menu. On they Keyboard menu, you can assign certain stored...
  • How to Automate SQL Server Express Backups

    SQL Server Express is great for small business with small data requirements. It is essentially a scaled down version of SQL Server that is free. However, because it is throttled down there are some features that are missing...one such feature is the SQL Server Agent. This service is responsible for automating SQL Server tasks, such as automatically backing up your databases. While there are plenty of tools out there that you can use to automate the backup of your SQL Server database....why pay for...
  • IDENTITY Columns

    SQL Server Identity Columns SQL Server IDENTITY columns are numeric columns which are set to auto increment when new values are inserted into the table. These columns are great for uniquely identifying records in a table, especially when you do not have any natural key to define a primary key constraint on the table. Like a primary key, there can only be one IDENTITY property defined on a column in a table. An IDENTITY column has a seed value (where the values begin) and the numeric increment value...
    Filed under: ,
  • SELECT...INTO

    The SQL Server SELECT statement includes an INTO clause which is used to create a table object based on the resultset of the query. The table created by the INTO clause can be a session-level temporary table, a global temporary table, or a user defined table. The fields in the newly created table will be listed in the order in which they appear in the SELECT list, and will be of the same data type as the base table from the query or as defined by the SELECT statement. In the following statement,...
    Published 2/23/2009 2:00 PM by admin
Copyright SQL Server Nation 2010
Powered by Community Server (Non-Commercial Edition), by Telligent Systems