-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCREATE_IF_NOT_EXISTS.sql
75 lines (64 loc) · 2.07 KB
/
CREATE_IF_NOT_EXISTS.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
--Table
IF NOT EXISTS(SELECT 1 FROM sysobjects WHERE name = 'TABLE_TESTE' AND xtype = 'U')
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
PRINT '--------------------------------------------------'
PRINT 'ACTION: CREATE TABLE TABLE_TESTE'
PRINT '--------------------------------------------------'
CREATE TABLE dbo.TABLE_TESTE (
MAJOR NUMERIC(12,0) NOT NULL,
MINOR NUMERIC(12,0) NOT NULL,
REVISION NUMERIC(12,0) NOT NULL,
BLOCK NUMERIC(12,0) NOT NULL,
DT_UPDATE DATETIME NOT NULL,
USUARIO NVARCHAR(256) NULL,
DATA_BASE NVARCHAR(256) NOT NULL,
CONSTRAINT PK_TBFR_RELEASE PRIMARY KEY CLUSTERED
(MAJOR,MINOR,REVISION,BLOCK,DATA_BASE)
);
IF (@@ERROR<>0) AND (@@TRANCOUNT>0)
ROLLBACK TRANSACTION
ELSE BEGIN
--Do something else...
COMMIT TRANSACTION
END
END
ELSE BEGIN
--Do something if exists...
END
GO
--Primary Key
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE type = 'PK' AND object_id = OBJECT_ID ('PK_TABLE_TESTE'))
BEGIN
ALTER TABLE BLR_CALCULO_DIF_ALT ADD CONSTRAINT PK_BLR_CALCULO_DIF_ALT PRIMARY KEY CLUSTERED
(
[COD_EMPRESA_FW] ASC,
[COD_CENARIO] ASC,
[SEQ_PERIODO] ASC,
[IND_TIPO] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
END
GO
--Constraint
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE type = 'F' AND object_id = OBJECT_ID ('FK_SAMPLE_01'))
BEGIN
ALTER TABLE TABLE_NAME
ADD CONSTRAINT FK_SAMPLE_01
FOREIGN KEY(COD_EMPRESA, COD_TIPO, NUM_ORDEM)
REFERENCES BLR_ECF_OPER_EXPORTACAO (COD_EMPRESA_FW, COD_TIPO, NUM_ORDEM)
END
GO
--Drop column if exists
IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'TABLE_EXAMPLE AND COLUMN_NAME = N'COLUMN_EXAMPLE')
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
ALTER TABLE TABLE_EXAMPLE DROP COLUMN COLUMN_EXAMPLE;
IF (@@ERROR<>0) AND (@@TRANCOUNT>0)
ROLLBACK TRANSACTION
ELSE BEGIN
COMMIT TRANSACTION
END
END
GO