Liquibase:如果函数存在,如何删除它

问题描述 投票:0回答:3

我想删除 SQL 标量函数(如果它存在于我的数据库中)。我们正在使用 SQL Server 2014。

如果我尝试以下脚本:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">  
    <changeSet context="schema" author="myself" id="1">
        <comment>Drop Function MyFunction</comment>
        <sql endDelimiter="\nGO">
IF EXISTS (SELECT 1 FROM sys.objects where name = N'MyFunction' AND Type = N'FN') DROP FUNCTION dbo.MyFunction
GO
        </sql>      
    </changeSet>    
</databaseChangeLog>

我收到以下消息:

运行 Liquibase 时出现意外错误:第 1 行第 1 列出现词法错误 105. 遇到:“\u00a0”(160),之后:“”

我检查了 Management Studio 中的 SQL 语法,它运行良好。

有什么想法吗?

sql-server liquibase
3个回答
2
投票

只需从

endDelimiter
中删除
<sql>
:

<changeSet context="schema" author="myself" id="1">
    <comment>Drop Function MyFunction</comment>
    <sql>
        IF EXISTS 
        (SELECT 1 FROM sys.objects where name = N'MyFunction' AND Type = N'FN')
        DROP FUNCTION dbo.MyFunction GO
    </sql>      
</changeSet>  

1
投票

你可以试试这个

IF EXISTS (SELECT 1 FROM sys.objects where name = 'MyFunction' AND Type = 'FN') 
begin
 DROP FUNCTION dbo.MyFunction
 end

0
投票

您可以按照以下步骤操作。

  1. 检查状况
  2. 如果预期结果为1,则执行下一个
  3. 创建您的函数。

    <changeSet context="schema" author="myself" id="1">
        <!--check the condition-->
        <preConditions onFail="MARK_RAN">
            <sqlCheck expectedResult="1">SELECT COUNT(*)
                                         FROM information_schema.routines
                                         WHERE routine_name = 'MyFunction'
                                           AND routine_type = 'FUNCTION'
                                           AND routine_schema = 'your_schema'</sqlCheck>
        </preConditions>
        <!--if expectedResult is 1, execute the next <sql>-->
        <sql>
        <![CDATA[
            DROP FUNCTION IF EXISTS your_schema.MyFunction;
            ]]>
        </sql>

        <!--creating your function-->

        <sql dbms="mysql" splitStatements="true" endDelimiter="//">
            CREATE FUNCTION MyFunction(param1 VARCHAR (36)) RETURNS INT
                DETERMINISTIC
            BEGIN
        DECLARE ....
        </sql>
    </changeSet>

© www.soinside.com 2019 - 2024. All rights reserved.