4
It was added in a table plus 7 columns, only before you need to check if this column is in the database otherwise the column creation script should not be executed.
Is there a way in a single condition to check if the columns have already been created in the bank? Follows the code:
IF NOT EXISTS (SELECT *
FROM SYSCOLUMNS C
INNER JOIN SYSOBJECTS T ON C.id = T.id
WHERE C.name = ('IdUserPreparo')
AND T.name = 'ComandaItem'
AND ('dtSolicitacao')
AND T.name = 'ComandaItem'
AND ('dtPreparo')
AND T.name = 'ComandaItem'
AND ('idUserCancel')
AND T.name = 'ComandaItem'
AND ('dtCancel')
AND T.name = 'ComandaItem'
AND ('IsCancelado')
AND T.name = 'ComandaItem'
AND ('obsCancel')
AND T.name = 'ComandaItem')
BEGIN
This is the column creation script in the database:
ALTER TABLE dbo.ComandaItem ADD
IdUserPreparo int NULL,
dtSolicitacao datetime NULL,
dtPreparo datetime NULL,
idUserCancel int NULL,
dtCancel datetime NULL,
IsCancelado bit NULL,
obsCancel varbinary(5000) NULL
GO
ALTER TABLE dbo.ComandaItem ADD CONSTRAINT
FK_ComandaItem_PessoaPreparo FOREIGN KEY
(
IdUserPreparo
) REFERENCES dbo.Pessoa
(
IDCadastro
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.ComandaItem ADD CONSTRAINT
FK_ComandaItem_PessoaCancel FOREIGN KEY
(
idUserCancel
) REFERENCES dbo.Pessoa
(
IDCadastro
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.ComandaItem SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
Why do you want to do this?
– Marconi
Why , here the company to various customer data base, and on each basis and made changes, some can have the fields and others not, so I have to check on the customer base if this field is already created, if you do not have the fields is created the script creates or does nothing.
– krispim