禁用脚本中无效的对象名称检查



在发布我们的dacpac项目之前,我们从CI运行一个包含脚本的文件夹。当我们部署到空数据库进行测试

时,这不起作用。即使我检查表是否存在,它也会中断,因为sql会预先检查查询

IF OBJECT_ID(N'dbo.MyTable', N'U') IS NOT NULL AND
NOT EXISTS (SELECT 1 FROM MyTable where Something = 'foo')

我可以在我的脚本中禁用该检查吗?

我认为你可以使用sp_executesql;

BEGIN
IF OBJECT_ID(N'dbo.MyTable', N'U') IS NOT NULL 
BEGIN
DECLARE @ret int;
DECLARE @s nvarchar(max) = N'SELECT @ret = (SELECT 1 FROM MyTable where Something = ''foo'')';  
EXEC sp_executesql @s, N'@ret int OUTPUT', @ret OUTPUT;
IF @ret = 1 
BEGIN
-- do something if the table exists and the select returns a record
select getdate();  
END
END
END

最新更新