T-SQL: UDF Scalar Function to quote the DB Object Name

While working on ETL, I build a list of tables and their corresponding columns that need to be created on the destination server. In this case the square brackets around SchemaName and TableName are mandatory. I started adding the square brackets with the built-in function QUOTENAME(), but soon I got a “dirty” data and created a user-defined scalar function that adds square brackets around SchemaName and TableName.

Create database

Create function

The highlighted line (row 55) shows another approach that bulk replace the square brackets and adds QUOTENAME()

To test the function, we need a table with sample data

The data in #DBObjects

Let’s check QUOTENAME()

Test the function

Only the outer square brackets are manipulated

Keep it simple :-)

Leave a comment

Your email address will not be published. Required fields are marked *