Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

sql server - T-SQL INSERT INTO disabling Constraints check

i would like to temporarly(just for the t-sql statement) disable the constraints check. My statement is:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Constraint is on the table not a single statement
Kind of ugly but
Put it in a transaction and take a tablock

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; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...