你可以试试我在 UDF 中使用的这个技巧。
我在删除它们并重新添加它们时遇到了问题(我大部分时间都使用存储过程)。所以我想出了下面的幂等解决方案。
Use [$(DBName)]
GO
:Error $(ErrorOutputFileFromCommandLine)
/*
Create a stubbed version if it does not exist... if it exists, this could shouldn't run, thus not violating permissions
*/
if NOT exists (select * from sysobjects
where id = object_id('dbo.udfMySuperCoolUDF') and sysstat & 0xf = 0)
BEGIN
print 'Creating the stubbed version of dbo.udfMySuperCoolUDF'
EXEC ( 'CREATE FUNCTION dbo.udfMySuperCoolUDF (@i as smallint , @j as smallint ) RETURNS bit AS BEGIN RETURN 0 END')
END
GO
ALTER FUNCTION dbo.udfMySuperCoolUDF (@ValidationLookupCategoryKey smallint , @ValidationLookupKey smallint )
RETURNS bit AS
BEGIN
declare @exists int
select @exists = 0
/* Logic Here */
return @exists
END
第二。
研究“sqlcmd”模式。保持 $(MyVariable) “原样”,并了解如何在 sqlcmd 模式(在 SSMS 内部)和部署(sqlcmd.exe)中操作它们。