Vérifier si l'élément existe déjà avant création de trigger-tables-insertion dans SQL serveur

Tags : Sql Server

Categories: Sql Server

- Trigger

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

IF EXISTS (SELECT * FROM sys.objects WHERE [type] = 'TR' AND [name] = 'MyTrigger' )

    DROP TRIGGER  MyTrigger;

GO

-- =============================================

-- Author:<Author,,Name>

-- Create date: <Create Date,,>

-- Description:<Description,,>

-- =============================================

CREATE TRIGGER [ MyTrigger ]

   ON ...

- Table

IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE name = 'MyTable')

CREATE TABLE [dbo].[ MyTable ](....

- Insertion de données

IF NOT EXISTS (SELECT id FROM [MyTable] WHERE [Name] = 'toto')

INSERT INTO [ MyTable ]

           ([Name]

           ,[Code])

     VALUES

           ('toto'

           ,'007')

- View

IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS  WHERE TABLE_NAME = 'MyView')

    DROP VIEW MyView

GO

 

CREATE VIEW MyView

AS 

     ....

GO

Aucun commentaire

Ajouter un commentaire