zoukankan      html  css  js  c++  java
  • SQL Server 项目中 SQL 脚本更新方式

    常用的如下:Oracle 项目中 SQL 脚本更新方式

    --如果不存在这条数据,就添加
    IF NOT EXISTS (SELECT 1 FROM dbo.Settings WHERE PropertyName = 'DBVersion')
        INSERT INTO dbo.Settings(ID, Code, Value)  VALUES(NEWID(),'DBVersion','2018')
    GO
    
    --可以通过版本号控制
    IF EXISTS (SELECT 1 FROM dbo.Settings WHERE PropertyName='DBVersion' AND CONVERT(INT, CONVERT(VARCHAR(10), PropertyValue)) <= 2019)
    BEGIN
        UPDATE TABLE1 SET Field1 = 'abc' WHERE field2 = 0 
        UPDATE TABLE2 SET FIELD2 = '1900-01-01 00:00:00' WHERE [Status] = 1
        UPDATE TABLE3 SET FIELD3 = '1900-01-01 00:00:00' WHERE [Status] = 1
        
        UPDATE dbo.Settings SET PropertyValue = '2020' WHERE PropertyName='DBVersion'
    END
    GO  
    
    --如果表里不存在字段,就添加
    IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TABLE4' AND COLUMN_NAME = 'COLUMN2' AND TABLE_SCHEMA = 'dbo')
    BEGIN
        ALTER TABLE TABLE4 Add  COLUMN2 nvarchar(31)
    END 
    GO 
    
    --存储过程先删除,再创建
    IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'P' AND name = 'PRO_TEST')
    DROP PROCEDURE PRO_TEST
    GO
    CREATE PROCEDURE [dbo].[PRO_TEST]
        @StartDate    DATETIME = NULL,
        @EndDate    DATETIME = NULL,
        @Num    INT = NULL
    AS
    SELECT * FROM LogTable
    WHERE Num = ISNULL(@Num, Num)
        AND CreatedDate >= ISNULL(@StartDate, CreatedDate)
        AND CreatedDate <= ISNULL(@EndDate, CreatedDate) + '23:59:59'
    ORDER BY CreatedDate DESC
    GO
  • 相关阅读:
    分布式集群环境下运行Wordcount程序
    VM搭建hadoop分布式集群
    安装运行Hadoop
    网络问题
    Golang依赖工具
    会话进程组终端 · 守护进程
    Golang笔记
    [转]GDB
    [转]用户态与内核态
    【转】linux环境内存分配原理 malloc info
  • 原文地址:https://www.cnblogs.com/vipsoft/p/12633912.html
Copyright © 2011-2022 走看看