Thursday, 22 March 2012

Don’t use the KEYWORD in COLUMNS name

 

The SQL server allows to use the Keywords in Table columns name by using this [ …].

But I think it is not a good idea to use the key words especially if you want migrate the table from another RDBMS. And is not a good practice to use the key words in table columns name.

Suppose you have a database with 1000 of table objects. Now your boss told you to alter the columns name that has the name used by Keywords. Is it possible manually or you just resigned the job.

Here in my article, I am trying to make a function that search the table name and related columns name that have the Keywords used.

So don't think about resignation or others thing – just joking.

The function is ready to use you can add more key words to enrich the functionality of the function.

 

CREATE  FUNCTION dbo.fn_ChkKeyWord

            (

                  @param_sWord  VARCHAR(255)

            )     RETURNS BIT

AS

BEGIN

      DECLARE @isWorking VARCHAR(255)

      DECLARE @bitRETRUN BIT

     

      SET @isWorking = UPPER(LTRIM(RTRIM(@param_sWord)))

      -- You can Add more Key word to Enrich the Functionality

      SET @bitRETRUN = CASE WHEN @isWorking

      IN('ADD', 'ALL', 'ALTER', 'AND', 'ANY', 'AS', 'ASC', 'AUTHORIZATION'

      , 'BACKUP', 'BEGIN', 'BETWEEN', 'BREAK', 'BROWSE', 'BULK', 'BY'

      , 'CASCADE', 'CASE', 'CHECK', 'CHECKPOINT', 'CLOSE', 'CLUSTERED'

      , 'COALESCE', 'COLLATE', 'COLUMN', 'COMMIT', 'COMPUTE', 'CONSTRAINT'

      , 'CONTAINS', 'CONTAINSTABLE', 'CONTINUE', 'CONVERT', 'CREATE'

      , 'CROSS', 'CURRENT', 'CURRENT_DATE', 'CURRENT_TIME'

      , 'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR', 'DATABASE', 'DBCC'

      , 'DEALLOCATE', 'DECLARE', 'DEFAULT', 'DELETE', 'DENY', 'DESC'

      , 'DISK', 'DISTINCT', 'DISTRIBUTED', 'DOUBLE', 'DROP', 'DUMMY'

      , 'DUMP', 'ELSE', 'END', 'ERRLVL', 'ESCAPE', 'EXCEPT', 'EXEC'

      , 'EXECUTE', 'EXISTS', 'EXIT', 'FETCH', 'FILE', 'FILLFACTOR', 'FOR'

      , 'FOREIGN', 'FREETEXT', 'FREETEXTTABLE', 'FROM', 'FULL', 'FUNCTION'

      , 'GOTO', 'GRANT', 'GROUP', 'HAVING', 'HOLDLOCK', 'IDENTITY'

      , 'IDENTITY_INSERT', 'IDENTITYCOL', 'IF', 'IN', 'INDEX', 'INNER'

      , 'INSERT', 'INTERSECT', 'INTO', 'IS', 'JOIN', 'KEY', 'KILL', 'LEFT'

      , 'LIKE', 'LINENO', 'LOAD', 'NATIONAL', 'NOCHECK', 'NONCLUSTERED'

      , 'NOT', 'NULL', 'NULLIF', 'OF', 'OFF', 'OFFSETS', 'ON', 'OPEN'

      , 'OPENDATASOURCE', 'OPENQUERY', 'OPENROWSET', 'OPENXML', 'OPTION'

      , 'OR', 'ORDER', 'OUTER', 'OVER', 'PERCENT', 'PLAN', 'PRECISION'

      , 'PRIMARY', 'PRINT', 'PROC', 'PROCEDURE', 'PUBLIC', 'RAISERROR'

      , 'READ', 'READTEXT', 'RECONFIGURE', 'REFERENCES', 'REPLICATION'

      , 'RESTORE', 'RESTRICT', 'RETURN', 'REVOKE', 'RIGHT', 'ROLLBACK'

      , 'ROWCOUNT', 'ROWGUIDCOL', 'RULE', 'SAVE', 'SCHEMA', 'SELECT'

      , 'SESSION_USER', 'SET', 'SETUSER', 'SHUTDOWN', 'SOME', 'STATISTICS'

      , 'SYSTEM_USER', 'TABLE', 'TEXTSIZE', 'THEN', 'TO', 'TOP',        

      ,'TRANSACTION', 'TRIGGER', 'TRUNCATE', 'TSEQUAL', 'UNION', 'UNIQUE',    

      ,'UPDATE', 'UPDATETEXT', 'USE', 'USER', 'VALUES', 'VARYING', 'VIEW'

      , 'WAITFOR', 'WHEN', 'WHERE', 'WHILE', 'WITH', 'WRITETEXT')

THEN 1 ELSE 0 END

      RETURN @bitRETRUN

END

 

GO

 

 

 

-- To Check

 

CREATE TABLE My_KeyWordTable

   (

      [END]    char(10) NOT NULL ,

      [VALUES] char(10)  NULL ,

      [CROSS]  char(10)   NULL

   )

 

 

SELECT TABLE_NAME, COLUMN_NAME

FROM   INFORMATION_SCHEMA.COLUMNS

WHERE  1=dbo.fn_ChkKeyWord(COLUMN_NAME)

 

 

Hope you like this.

 

Posted by: MR. JOYDEEP DAS

 

4 comments:

  1. You can get the list of keywords (and other reserved words) from the master database

    select distinct name
    from master.dbo.spt_values
    where name is not null and name <> ''
    order by name

    ReplyDelete