Wednesday, 3 August 2022

Search a column name into all the table across databases in sql server

 How to search a column name in any table (entire database) or how to check a column exists in any table or not, how to check a column belongs to which table, these are very common question we can get on different websites. Some times we know the column name but don't know the column name then we need a script to get the table name to proceed.

Suppose we have table with primary key and want to know what are the other tables where this ID is used so we can relate those tables to get the required information.

In this article, we will see different query to get the column name from entire database as well as in our specified databases

Let's say we want to check for column BlogId then

Script 1: Simplest one by using information_schema.columns

  1. SELECT [table_name]
  2.   , TABLE_SCHEMA [schema_name]
  3.   , [column_name]
  4. FROM information_schema.columns
  5. WHERE column_name like '%BlogId%'
  6. ORDER BY [table_name], [column_name]

Script 2: by using sys.tables and sys.columns

  1. SELECT t.name AS [table_name],
  2.    SCHEMA_NAME(schema_id) AS [schema_name],
  3.    c.name AS [column_name]
  4. FROM sys.tables AS t
  5.     INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
  6. WHERE c.name LIKE '%blogid%'
  7. ORDER BY  [table_name], [column_name]

Script 3: by using sys.columns and sys.objects

  1. SELECT o.name[table_name]
  2.   ,c.name [column_name]
  3. FROM sys.columns  c
  4.    INNER JOIN sys.objects  o on c.object_id=o.object_id
  5. WHERE c.name like  '%BlogId%'
  6. ORDER BY [table_name], [column_name]

If we have to search the blogId in three different databases say DB1, DB2 and DB3 then we can use TABLE_CATALOG in where clause and provide the name of all the databases where we have to search.

  1. SELECT TABLE_CATALOG [database]
  2.   , [table_name]
  3.   , TABLE_SCHEMA [schema_name]
  4.   , [column_name]
  5. FROM information_schema.columns  
  6. WHERE column_name like '%BlogId%'
  7.     AND TABLE_CATALOG IN ('DB1', 'DB2', 'DB3')
  8. ORDER BY [table_name], [column_name]

Concatenate multiple columns in SQL Server with NULL value

 When we need to concatenate two columns simply we can use + sign and that is correct, but what if any of them is null, Will it return what we want, NO, it will return null. So let's discuss how we should concatenate two or more columns without creating extra space or extra comma problem. Let's say we have a customers table with following structure and data

alt text

Now suppose we have to bind a drop down on page so we need Customer id and FirstName + LastName so we will use

SELECT [CustomerID],   [FirstName] + ' ' + [LastName] AS [CustomerName] FROM Customers 

It will work smooth because first name and last name both have some values no NULL column.

Now let's say, we have to use FirstName, MidleName and LastName as CustomerName, change our query to this one

SELECT [CustomerID],   [FirstName] + ' ' + [MidleName] + ' ' + [LastName] AS [CustomerName] FROM Customers 

It will return

  1. CustomerID  CustomerName
  2. ----------- ------------------
  3. 10001       Jamey Jo Durham
  4. 10002       NULL
  5. 10003       NULL
  6. 10004       NULL

Is there any mistake we made in our sql statement, no. It is because of the null value in Middle name, so how to fix it? We will check the column value by using ISNULL function and if it is null then we will use blank ('') [we don't want to go here in detail about the differences between blank and null because we are discussing here about concatenation of columns].

  1. SELECT [CustomerID],  
  2. IsNull([FirstName], '') + ' ' + IsNull([MidleName], '')
  3.    + ' ' + IsNull([LastName], '') AS [CustomerName]
  4. FROM Customers
  5. /* ---  Output  ---- */
  6. CustomerID  CustomerName
  7. ----------- -----------------
  8. 10001       Jamey Jo Durham
  9. 10002       John  Smith
  10. 10003       Abhay  Singh
  11. 10004       Sunil  Kumar

It looks good but there is a problem, if you will see closely record 2,3 and 4, there are double spaces between first name and last name. It is not so bad, but suppose if we need ot use , in place of space then it will not look good. check the following example.

  1. SELECT [CustomerID],  
  2. [FirstName] + ' ' + [MidleName] + ' ' + [LastName] AS [CustomerName],
  3. [Address1] + ', ' + [Address2] + ', '+ [City]
  4.   + ', '+ [State] + ', ' + [Country] + '-' + [Zip] AS [Address]
  5. FROM Customers

  6. /* -----------   See Output  ----------- */
  7. CustomerID  CustomerName     Address
  8. ----------- ---------------- ----------------------------------------------
  9. 10001       Jamey Jo Durham  127 Park Avenue, Lane 10, Jersey City, NJ, USA-10234
  10. 10002       NULL             Lane 2, Friends Colony, New Delhi, DL, India-110025
  11. 10003       NULL             NULL
  12. 10004       NULL             NULL

Now let's use ISNULL function to check the null value and use blank if value is null, so new sql query will be

  1. SELECT [CustomerID],  
  2. IsNull([FirstName], '') + ' ' + IsNull([MidleName], '') + ' '
  3.      + IsNull([LastName], '') AS [CustomerName],
  4. IsNull([Address1], '') + ', ' + IsNull([Address2], '') + ', '
  5.    + IsNull([City], '') + ', '+ IsNull([State], '') + ', '
  6.    + IsNull([Country], '') + '-' + IsNull([Zip], '') AS [Address]

  7. /* --- Output  ----*/
  8. CustomerID  CustomerName     Address
  9. ----------- ---------------- ----------------------------------------------------
  10. 10001       Jamey Jo Durham  127 Park Avenue, Lane 10, Jersey City, NJ, USA-10234
  11. 10002       John  Smith      Lane 2, Friends Colony, New Delhi, DL, India-110025
  12. 10003       Abhay  Singh     A-123, T. Appartment, , , MB, -220345
  13. 10004       Sunil  Kumar     A-92 C, , New Delhi, , -
  14. FROM Customers

In record 3 and 4 CustomerName have double space one for MiddleName and one for LastName. Similarly, Address column have extra commas, so how to fix them, do we need some logic to first check and then use space or commas. No, we will use Stuff and Coalesce to fix this problem, so change our above query to this one

  1. SELECT [CustomerID],  
  2. Stuff(
  3.      Coalesce('' + [FirstName], '')
  4.    + Coalesce(' ' + [MidleName], '')
  5.    + Coalesce(' ' +[LastName], '')
  6.    , 1, 1, '') AS [CustomerName],
  7. Stuff(  
  8.      Coalesce(', ' + [Address1], '')
  9.    + Coalesce(', ' + [Address2], '')
  10.    + Coalesce(', ' + [City], '')
  11.    + Coalesce(', ' + [State], '')
  12.    + Coalesce(', ' + [Country], '')
  13.    +Coalesce('-' + [Zip], '')
  14.    , 1, 1, '') AS [Address]
  15. FROM Customers

And here is the final output, is not it what we want

alt text