“If not exists” fails on SQL CE

前端 未结 3 589
刺人心
刺人心 2020-12-03 21:47

I\'ve got an unexpected problem. I\'m making a script to update the schema on a SQL CE database. This won\'t run:

if not exists
(
    Select column_name from         


        
相关标签:
3条回答
  • 2020-12-03 21:49

    I confess to not being familiar with SQL Server CE's special limitations, however, if it supports Dynamic SQL, then this should work:

    Declare @sql as nvarchar(max);
    Set @sql = '';
    
    Select @sql = 'Alter table Inventory_Master_File
      add TempTestField nvarchar(10) null   '
    from information_schema.columns
    where column_name = 'TempTestField' 
        and table_name = 'Inventory_Master_File' 
    
    EXEC(@sql);
    
    0 讨论(0)
  • 2020-12-03 22:03

    It appears that SQL CE does not support any procedural extensions at all; only DDL and DML like from the 1970s.

    You will either have to put procedural logic in your application (C# or whatever) or go to an embeddable database with procedural extensions, like MySQL.

    0 讨论(0)
  • 2020-12-03 22:16

    I'm not used to CE at all. But in the case I wouldn't have any procedural extensions I simply would fire the ALTER TABLE statement. If the field already exists, you will get an exception. As you have to try ... catch it in either way you simply have to handle this exception separately in your catch phrase ...

    just my two cents.

    0 讨论(0)
提交回复
热议问题