Published on

November 11, 2008

Handling Repeating Columns in SQL Server

When designing a database, it is important to follow normalization principles to ensure data integrity and efficiency. However, there are situations where tables may not be properly normalized, resulting in repeating columns. This can make it difficult to locate specific data within the table.

One common scenario is when there are multiple columns for different types of phone numbers. For example, a table may have columns for home phone, work phone, and cell phone. If you need to search for a specific phone number, it can be challenging to determine which column to check.

One approach to solve this problem is to write a query with multiple “or” statements in the WHERE clause, enumerating every column. For example:

SELECT * FROM phonebook WHERE phone1 = '234-5678' OR phone2 = '234-5678' OR workphone = '234-5678' OR cellphone = '234-5678'

This approach works well when the number of columns is small. However, it becomes cumbersome and inefficient when there are many columns to check.

Another solution is to normalize the data and remove the repeating columns. Normalization involves organizing the data into separate tables and establishing relationships between them. This eliminates the need for repeating columns and improves data integrity. However, there are cases where normalization is not possible or practical.

In such situations, a general script can be useful to search for a specific value across all columns. The script dynamically generates a SELECT statement based on the table structure and the value to search for. Here is an example of such a script:

CREATE PROCEDURE [dbo].[FindValue] 
@TableName NVARCHAR(128), 
@Value NVARCHAR(4000), 
@schema NVARCHAR(128) = 'dbo' 
AS 
DECLARE @columns TABLE (ColumnName NVARCHAR(128)) 
DECLARE @sql NVARCHAR(MAX) 

INSERT INTO @columns (ColumnName) 
SELECT Column_name 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE Table_schema = @schema 
AND Table_name = @TableName 
AND data_type IN ('char', 'nchar', 'varchar', 'nvarchar') 

IF ISNUMERIC(@value) = 1 
INSERT INTO @columns (ColumnName) 
SELECT Column_name 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE Table_schema = @schema 
AND Table_name = @TableName 
AND data_type IN ('int', 'numeric', 'bigint', 'money', 'smallint', 'smallmoney', 'tinyint', 'float', 'decimal', 'real') 

IF ISDATE(@value) = 1 
INSERT INTO @columns (ColumnName) 
SELECT Column_name 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE Table_schema = @schema 
AND Table_name = @TableName 
AND data_type IN ('datetime', 'smalldatetime') 

SELECT @sql = CASE 
WHEN @sql IS NULL THEN 'Select ''' + ColumnName + ''' as ContainingColumn, * From ' + QUOTENAME(@Schema) + '.' + QUOTENAME(@TableName) + ' where ' + ColumnName + ' like ''' + @Value + ''' ' 
WHEN @sql IS NOT NULL THEN @sql + 'UNION ALL Select ''' + ColumnName + ''' as ContainingColumn, * From ' + QUOTENAME(@Schema) + '.' + QUOTENAME(@TableName) + ' where ' + ColumnName + ' like ''' + @Value + ''' ' 
END 
FROM @columns 

EXEC (@sql)

This script dynamically generates a SELECT statement that searches for the specified value in all relevant columns of the table. It uses the INFORMATION_SCHEMA.COLUMNS view to retrieve the column names and data types. The script can handle text, numeric, and date values, and it supports wildcard characters such as ‘%’. However, it does not search text, ntext, xml, or user-defined fields.

It is important to note that while this script provides a general solution for searching across columns, it may search unnecessary columns and potentially return false positives. However, it can be a more practical approach compared to manually writing a SELECT statement with numerous “or” conditions.

The script can be modified to handle specific data types more effectively. For example, it can be enhanced to handle approximate data types like float or to perform broader matches on dates. Additionally, instead of creating a stored procedure, the script can be used as a standalone script for occasional use.

In conclusion, when dealing with repeating columns in SQL Server, the ideal approach is to avoid them in the initial database design. If repeating columns exist, the best solution is often to normalize the tables and remove the repetition. However, there are cases where normalization is not feasible or desirable. In such situations, using a script or procedure to search across columns can be a practical solution.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.