Validate object name

  • Do you guys know what sp or function validates the name of the objects so that brackets are used to refer to it... like in EM if you name a column "case" it will be referred has [case].

    I often found myself writing scripts that output columns from our database (that have way too many columns with "abnormal names" like ""n° bon de travail"") and the scripts fail when I don't use [] for every columns which makes the code much less readable).

    I tried running a trace will making a columns but nothing came out of it. I also found functions on the net that would return are alpha numeric characters but I'd preffer not to alter the names at all besides brackets.

  • You could try using quotename in your queries.

    Check BOL for usage.

  • I believe (and I may be wrong...) that the square brackets are required for any name that matches a reserved word (i.e. "user") or has special characters, including blanks (i.e. "First Name" -> [First Name]). I don't think the brackets are required for a database or column name that meets the standards. So to refer to database "dbo.Employee Data" you'd need

    dbo.[Employee Data]

    to refer to the "cn" column in that database:

    dbo.[Employee Data].cn

    to refer to the "User" column in that database:

    dbo.[Employee Data].[User]

    This is all discussed in BOL under naming conventions.

     

  • Thanx for all your replies but I think I wasn't too clear in my question.  I'm already well aware of the naming conventions and that this DB is breaking about every rule in almost every Object name.  What I need is a function like this :

    Create procedure dbo.ValidateObjName @sObjName as varchar(255)

    as

    if @sObjName is valid

         select @sObjName

    else

         select '[' + sObjName + ']'

    GO

     

    All I want is something that will return the name in brackets when the Object name breaks any of the conventions' rules.  And My question is this : is there any available SP in the master database or anywhere else that can do this work for me?

     

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply