如果我在SQL Server CE中运行以下SQL,
alter table audit add new_key nvarchar(100) null在尝试将该列添加到同一个SQL语句中之前,是否可以对其进行更改以检查该列是否存在?我知道在SQL Server中可以很容易地做到这一点,但是SQL Server CE?
发布于 2013-10-01 07:15:17
如果您正在寻找可以使用SQL Server执行的操作:IF NOT EXISTS (...,不幸的是,这是不能与SQL Server Compact一起使用的(不支持T-SQL IF )。参见SQL Reference (SQL Server Compact)。
基于this thread,您可以这样做:
select * from Information_SCHEMA.columns
where Table_name='audit' and column_name='new_key'(即无结果=不存在)。
另请注意,SQL Server Compact完全不支持批处理查询。因此,您不能检查列是否存在,也不能根据需要在同一个SQL语句中添加该列。
或者,如果您正在寻找可与VB.NET或C#一起使用的代码片段,请查看这个SO问题:How can I determine whether a column exists in a SQL Server CE table with C#?
发布于 2013-10-01 06:45:57
最简单的方法是查询information_schema并查看列是否在其中。
IF NOT EXISTS(
SELECT * FROM information_schema.columns
WHERE Table_Schema = 'dbo'
AND Table_Name = 'audit'
AND Column_Name = 'new_key'
) BEGIN
alter table audit add new_key nvarchar(100) null
ENDhttps://stackoverflow.com/questions/19105026
复制相似问题