Wednesday, 15 June 2011

sql - Check for the existence of multiple columns -


I need to create a SQL change script which checks the existence of two columns in the table if these columns are present, The script will run the optical table to connect them. Below is an example of how we check for 1 column.

  does not exist  <(INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'table' and column = 'selection from ColumnC' *) / pre> 

Then I will call ColumnC and ColumnD This script needs to be checked.

After

You would like to run the query once for each column, unless there is no way There may be one in the table in the table, but not the other. Because if you check to be both columns at once, and only one of them exists, then you will raise the column ALTER table an exception to try to add both, after one of the columns already exists . Either be with you and once for every column or performance is important, write 3 statements ... one for both columns at once and then one for each column. The pseudocode will look something like this:

  No column alter table is present ... the other two columns if Columna is present and does not change in the ColumnB table ... ColumnB add else if ColumnB is present and Adding Columna does not change the table ... Adding Columna  

Something like this is the table that both columns ...

  are missing selection Will come back * from INFORMATION_SCHEMA.TABLES Where TABLE_TYPE = 'base table' and does not exist (select * from INFO Where is RMATION_SCHEMA.COLUMNS C.TABLE_CATALOG = T.TABLE_CATALOG and C.TABLE_SCHEMA = T.TABLE_SCHEMA and C.TABLE_NAME = T.TABLE_NAME and C.COLUMN_NAME ('Column 1', 'Column2'));  

Be sure to refer to the tables so that you do not accidentally try to change any view.


No comments:

Post a Comment