help with this if statement

  • Hi,

    i want to run this script on few sql servers that some of the servers have this column name in the master database table HOLDDATABASESIZECHECK and some don't have.

    if i run this script on the server that not have this column name in the master database table i get an error (invalid column name SHRINKLOGFILE)like he run the select statement and not go to the else,why is that?

    THX

    if exists (select name from master.sys.columns where name = 'SHRINKLOGFILE')

    begin

    insert into msys_monitor.dbo.TBL_SPECIAL_DATABASES_SIZE_CHECK (DBNAME,SIZECHECK,SHRINKLOGFILE)

    select DBNAME,SIZECHECK,SHRINKLOGFILE from master.dbo.HOLDDATABASESIZECHECK

    end

    else

    begin

    insert into msys_monitor.dbo.TBL_SPECIAL_DATABASES_SIZE_CHECK (DBNAME,SIZECHECK)

    select DBNAME,SIZECHECK from master.dbo.HOLDDATABASESIZECHECK

    end

  • To accomplish what you are attempting, you will actually need to use dynamic sql. The code as written is failing when it is being parsed, not when it is actually executing. SQL Server needs to validate all the code prior to running, and since the column is missing, it fails.

Viewing 2 posts - 1 through 1 (of 1 total)

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