博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用sql compare生成的sql语句
阅读量:5998 次
发布时间:2019-06-20

本文共 6418 字,大约阅读时间需要 21 分钟。

创建表以及主键

判断表是否存在

OBJECT_ID

判断主键是否存在

SELECT 1 FROM sys.indexes WHERE name = N'PK_LISA_NoUseWebpartReplacement' AND object_id = OBJECT_ID(N'[dbo].[LISA_NoUseWebpartReplacement]')

 

/*Run this script on a database with the schema represented by:        WASYGSHA01-1020\SQL2014.Test    -  This database will be modified. The scripts folder will not be modified.to synchronize it with a database with the schema represented by:        WASYGSHA01-1020\SQL2014.TestYou are recommended to back up your database before running this scriptScript created by SQL Compare version 13.4.5.6953 from Red Gate Software Ltd at 11/13/2018 5:18:41 PM*/SET NUMERIC_ROUNDABORT OFFGOSET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ONGOSET XACT_ABORT ONGOSET TRANSACTION ISOLATION LEVEL SerializableGOBEGIN TRANSACTIONGOIF @@ERROR <> 0 SET NOEXEC ONGOPRINT N'Creating [dbo].[LISA_NoUseWebpartReplacement]'GOIF OBJECT_ID(N'[dbo].[LISA_NoUseWebpartReplacement]', 'U') IS NULLCREATE TABLE [dbo].[LISA_NoUseWebpartReplacement]([Id] [int] NOT NULL,[Category] [nvarchar] (255) NULL,[WebpartCode] [nvarchar] (255) NOT NULL,[WebpartName] [nvarchar] (255) NULL,[WebpartFile] [nvarchar] (255) NULL,[Replacement] [nvarchar] (255) NULL)GOIF @@ERROR <> 0 SET NOEXEC ONGOPRINT N'Creating primary key [PK_LISA_NoUseWebpartReplacement] on [dbo].[LISA_NoUseWebpartReplacement]'GOIF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'PK_LISA_NoUseWebpartReplacement' AND object_id = OBJECT_ID(N'[dbo].[LISA_NoUseWebpartReplacement]'))ALTER TABLE [dbo].[LISA_NoUseWebpartReplacement] ADD CONSTRAINT [PK_LISA_NoUseWebpartReplacement] PRIMARY KEY CLUSTERED  ([WebpartCode])GOIF @@ERROR <> 0 SET NOEXEC ONGOCOMMIT TRANSACTIONGOIF @@ERROR <> 0 SET NOEXEC ONGO-- This statement writes to the SQL Server Log so SQL Monitor can show this deployment.IF HAS_PERMS_BY_NAME(N'sys.xp_logevent', N'OBJECT', N'EXECUTE') = 1BEGIN    DECLARE @databaseName AS nvarchar(2048), @eventMessage AS nvarchar(2048)    SET @databaseName = REPLACE(REPLACE(DB_NAME(), N'\', N'\\'), N'"', N'\"')    SET @eventMessage = N'Redgate SQL Compare: { "deployment": { "description": "Redgate SQL Compare deployed to ' + @databaseName + N'", "database": "' + @databaseName + N'" }}'    EXECUTE sys.xp_logevent 55000, @eventMessageENDGODECLARE @Success AS BITSET @Success = 1SET NOEXEC OFFIF (@Success = 1) PRINT 'The database update succeeded'ELSE BEGIN    IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION    PRINT 'The database update failed'ENDGO

 

创建存储过程

先判断存储过程是否存在,如果存在就先drop,然后创建。如果不存在就直接创建

/*Run this script on a database with the schema represented by:        WASYGSHA01-1020\SQL2014.Test    -  This database will be modified. The scripts folder will not be modified.to synchronize it with a database with the schema represented by:        WASYGSHA01-1020\SQL2014.TestYou are recommended to back up your database before running this scriptScript created by SQL Compare version 13.6.3.8160 from Red Gate Software Ltd at 11/14/2018 11:11:52 AM*/SET NUMERIC_ROUNDABORT OFFGOSET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ONGOSET XACT_ABORT ONGOSET TRANSACTION ISOLATION LEVEL SerializableGOBEGIN TRANSACTIONGOIF @@ERROR <> 0 SET NOEXEC ONGOPRINT N'Dropping [dbo].[pi_NoUseWebpartReplacement]'GOIF OBJECT_ID(N'[dbo].[pi_NoUseWebpartReplacement]', 'P') IS NOT NULLDROP PROCEDURE [dbo].[pi_NoUseWebpartReplacement]GOIF @@ERROR <> 0 SET NOEXEC ONGOPRINT N'Creating [dbo].[pi_NoUseWebpartReplacement]'GOIF OBJECT_ID(N'[dbo].[pi_NoUseWebpartReplacement]', 'P') IS NULLEXEC sp_executesql N'-- =============================================-- Author:        Chuck Lu-- Create date: 2018-11-14-- Description:    -- =============================================CREATE PROCEDURE [dbo].[pi_NoUseWebpartReplacement]    -- Add the parameters for the stored procedure here    @Id INT ,    @Category NVARCHAR(255) ,    @WebpartCode NVARCHAR(255) ,    @WebpartName NVARCHAR(255) ,    @WebpartFile NVARCHAR(255) ,    @Replacement NVARCHAR(255)AS    BEGIN        -- SET NOCOUNT ON added to prevent extra result sets from        -- interfering with SELECT statements.        SET NOCOUNT ON;        IF EXISTS (   SELECT 1                      FROM   dbo.LISA_NoUseWebpartReplacement                      WHERE  WebpartCode = @WebpartCode )            BEGIN                UPDATE dbo.LISA_NoUseWebpartReplacement                SET    Category = @Category ,                       WebpartName = @WebpartName ,                       WebpartFile = @WebpartFile ,                       Replacement = @Replacement                WHERE  WebpartCode = @WebpartCode;            END;        ELSE            BEGIN                INSERT INTO dbo.LISA_NoUseWebpartReplacement ( Id ,                                                               Category ,                                                               WebpartCode ,                                                               WebpartName ,                                                               WebpartFile ,                                                               Replacement )                VALUES ( @Id ,          -- Id - int                         @Category ,    -- Category - nvarchar(255)                         @WebpartCode , -- WebpartCode - nvarchar(255)                         @WebpartName , -- WebpartName - nvarchar(255)                         @WebpartFile , -- WebpartFile - nvarchar(255)                         @Replacement   -- Replacement - nvarchar(255)                    );            END;    END;'GOIF @@ERROR <> 0 SET NOEXEC ONGOCOMMIT TRANSACTIONGOIF @@ERROR <> 0 SET NOEXEC ONGO-- This statement writes to the SQL Server Log so SQL Monitor can show this deployment.IF HAS_PERMS_BY_NAME(N'sys.xp_logevent', N'OBJECT', N'EXECUTE') = 1BEGIN    DECLARE @databaseName AS nvarchar(2048), @eventMessage AS nvarchar(2048)    SET @databaseName = REPLACE(REPLACE(DB_NAME(), N'\', N'\\'), N'"', N'\"')    SET @eventMessage = N'Redgate SQL Compare: { "deployment": { "description": "Redgate SQL Compare deployed to ' + @databaseName + N'", "database": "' + @databaseName + N'" }}'    EXECUTE sys.xp_logevent 55000, @eventMessageENDGODECLARE @Success AS BITSET @Success = 1SET NOEXEC OFFIF (@Success = 1) PRINT 'The database update succeeded'ELSE BEGIN    IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION    PRINT 'The database update failed'ENDGO

 

转载地址:http://hyzmx.baihongyu.com/

你可能感兴趣的文章
Python 初体验(三)
查看>>
android开发(22)使用正则表达式 。从一个字符串中找出数字,多次匹配。
查看>>
AJAX
查看>>
2015 多校联赛 ——HDU5334(构造)
查看>>
例10-9 uva1636简单概率问题
查看>>
几个ES6新特性
查看>>
mysql字符集
查看>>
DP_1d1d诗人小G
查看>>
非、半、结构化数据学习【转载】
查看>>
SpringMVC之单/多文件上传
查看>>
cache-fusion笔记
查看>>
改变div属性的一个实例
查看>>
avalon加载一闪而过现象
查看>>
线段树模板【数据结构 - 线段树】
查看>>
Castle IOC概念理解
查看>>
如何配置Log4Net使用Oracle数据库记录日志
查看>>
一道在知乎很火的 Java 题——如何输出 ab【转】
查看>>
Python学习第二天-编写购物车
查看>>
AD域组策略-只显示指定的控制面板选项配置方法
查看>>
BigTable——针对结构型数据的一种分布式存储系统
查看>>