sql server - Compilation when Column doesn't exist -


i've stored procedure looks this:

create procedure test begin if  exists(                select 1 sys.columns                  name      = n'column2'                   , object_id = object_id(table2')               )  select column2 table2 end 

i want run procedure on db column2 doesn't exist. don't want take existence check out of sp. error :

msg 207, level 16, state 1, procedure test, line 39 [batch start line 0] invalid column name 'column2'.

is there way so? why yes , why not?

and why instance if check existence table , select non-existent table works?

use dynamic sql:

create procedure test begin     if exists (select 1                sys.columns                 name = n'column2' , object_id = object_id('table2')               )     begin         exec sp_executesql n'select column2 table2';     end; end; 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -