T-SQL INSERT INTO禁用约束检查

时间:2015-07-01 10:29:24

标签: sql-server tsql insert insert-into

我想临时(仅针对t-sql语句)禁用约束检查。 我的发言是:

insert into branchOffice(
branchOfficeTypeId, 
labirintoClientiId, 
companyId,
signboardName,
address,
addressNumber,
zipCode, 
city, 
province, 
officePhoneNumber, 
officeFaxNumber, 
officeEmail,
statusId,
officeNotes,
squareMeters,
familyHelpersCount,
employeesCount,
workingCompanyPartnerCount)

SELECT
    1, 
    [NewBiz.Labirinto].dbo.Clienti.id, 
    1,
    [NewBiz.Labirinto].dbo.clienti.Insegna,
        case 
        when PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)>0 then LEFT([NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo, PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)-1)
        when PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)=0 then [NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo
        end as indirizzo, 
        case
        when PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)>0 then right([NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo,len([NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)-PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)+1) 
        when PATINDEX('%[0-9]%',[NewBiz.Labirinto].dbo.clienti.SedeLegaleIndirizzo)=0 then '' 
        end as numero,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleCAP,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleComune,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleProvincia,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleTelefono,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleFax,
    [NewBiz.Labirinto].dbo.clienti.SedeLegaleEMail,
    [NewBiz.Labirinto].dbo.clienti.SituazioneId,
    [NewBiz.Labirinto].dbo.clienti.Note,
    [NewBiz.Labirinto].dbo.clienti.Superficie,
    [NewBiz.Labirinto].dbo.clienti.Coadiuvanti,
    [NewBiz.Labirinto].dbo.clienti.Dipendenti,
    [NewBiz.Labirinto].dbo.clienti.SociLavoratori
    from [NewBiz.Labirinto].dbo.Clienti
    where [NewBiz.Labirinto].dbo.Clienti.AziendaId=1

1 个答案:

答案 0 :(得分:1)

约束在桌面上而不是单一的声明
有点难看但是 把它放在一个交易中并拿一个制表符

begin transaction 
  ALTER TABLE branchOffice NOCHECK CONSTRAINT ALL
  insert into branchOffice with (tablock) 
  -- Re-enable the constraints on a table
  ALTER TABLE branchOffice WITH CHECK CHECK CONSTRAINT ALL
commit transation;