2012-05-22 2 views
0

일부 데이터를 삽입하려고합니다. 트랜잭션을 사용하여 테이블에 삽입되지 않습니다. 대신 catch 블록으로 이동합니다. 다음은 SQL 저장 프로 시저는 다음과 같습니다 자신을 돕기 위해 할트랜잭션을 사용하는 테이블에 데이터가 삽입되지 않습니다.

/****** Object: StoredProcedure [dbo].[rml_re_GetAndStoreConsumerSubscriptionData_NEWUPS] Script Date: 05/21/2012 12:39:59 ******/ 
SET ANSI_NULLS ON 
GO 

SET QUOTED_IDENTIFIER ON 
GO 

--EXEC rml_re_GetAndStoreConsumerSubscriptionData_NEWUPS 
ALTER PROCEDURE [dbo].[rml_re_GetAndStoreConsumerSubscriptionData_NEWUPS] 

AS 
BEGIN 
DECLARE @currenttime datetime 
SET @currenttime= getdate() 
INSERT INTO rml_re_consumerdatasync_log VALUES('rml_re_GetAndStoreConsumerSubscriptionData',@currenttime,null,'started',@currenttime,'',getdate(),null); 
    BEGIN TRY 
    BEGIN TRANSACTION PULLCONSUMERDATA 
     DECLARE @dbServerName VARCHAR(50),@databaseName VARCHAR(50),@dbUserName VARCHAR(50),@query VARCHAR(MAX)  
     SELECT @dbServerName=dbservername,@databaseName=dbname,@dbUserName=dbusername FROM rml_re_applications 
     WHERE id=(SELECT configvalue FROM rml_re_configuration WHERE configname='CRMApplicationID') 

     -- Removes all old data before insertion. 
     ALTER TABLE [dbo].[rml_re_consumermarkets] DROP CONSTRAINT [FK_rml_re_consumermarkets_rml_re_consumercrops] 
     ALTER TABLE [dbo].[rml_re_consumermarkets] DROP CONSTRAINT [FK_rml_re_ConsumerSubscriptionMarket_rml_re_ConsumerSubscriptionData] 
     ALTER TABLE [dbo].[rml_re_consumercrops] DROP CONSTRAINT [FK_rml_re_ConsumerSubscriptionCrops_rml_re_ConsumerSubscriptionData] 

     TRUNCATE TABLE rml_re_consumermarkets 
     TRUNCATE TABLE rml_re_consumercrops 
     TRUNCATE TABLE rml_re_ConsumerSubscriptionData 

    ALTER TABLE [dbo].[rml_re_consumercrops] WITH NOCHECK ADD CONSTRAINT [FK_rml_re_ConsumerSubscriptionCrops_rml_re_ConsumerSubscriptionData] FOREIGN KEY([subscriptionid]) 
    REFERENCES [dbo].[rml_re_ConsumerSubscriptionData] ([id]) 
    ALTER TABLE [dbo].[rml_re_consumermarkets] WITH NOCHECK ADD CONSTRAINT [FK_rml_re_ConsumerSubscriptionMarket_rml_re_ConsumerSubscriptionData] FOREIGN KEY([subscriptionid]) 
    REFERENCES [dbo].[rml_re_ConsumerSubscriptionData] ([id]) 
    ALTER TABLE [dbo].[rml_re_consumermarkets] WITH CHECK ADD CONSTRAINT [FK_rml_re_consumermarkets_rml_re_consumercrops] FOREIGN KEY([cropid]) 
    REFERENCES [dbo].[rml_re_consumercrops] ([cropid]) 


     SET @query=''; 
     SET @query= 'INSERT INTO rml_re_ConsumerSubscriptionData(id,msisdn,consumername, 
        languagetype,statecode,talukcode,districtcode,regioncode,status, 
        subscriptionstartdate,subscriptionenddate,DistributorCode, 
        package,soiltype,comp_category,AdditionalInfo) 
          SELECT id,msisdn, 
          case when Len(consumername) <=150 then consumername else null end as consumername, 
          case when Len(languagetype) <=10 then languagetype else null end as languagetype ,' 
     SET @[email protected]+' case when Len(statecode) <=5 then statecode else null end as statecode , 
          case when Len(talukcode) <=10 then talukcode else null end as talukcode , 
          case when Len(districtcode) <=10 then districtcode else null end as districtcode, 
          case when Len(regioncode) <=10 then regioncode else null end as regioncode , 
          case when Len(status) <=50 then status else null end as status , 
          subscriptionstartdate ,' 
     SET @[email protected]+' subscriptionenddate, 
          case when Len(DistributorCode) <=100 then DistributorCode else null end as DistributorCode , 
          case when Len(package) <=50 then package else null end AS package , 
          case when Len(soiltype) <=50 then soiltype else null end as soiltype,' 
     SET @[email protected]+' case when Len(comp_category) <=50 then comp_category else null end as comp_category , 
          case when Len(AdditionalInfo) <=100 then AdditionalInfo else null end as AdditionalInfo ' 
     SET @[email protected]+' FROM ' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New ' 
     SET @[email protected]+' WHERE crop1code is not null AND crop2code is not null ' 



     EXEC (@query) 
     SET @query=''; 
     -- INSERT FOR rml_re_consumercrops 
     --INSER FOR PRIMARY CROP 
     SET @query='INSERT INTO rml_re_consumercrops(subscriptionid,commoditycode,sowingdate,sowingweek)' 
     SET @[email protected]+'SELECT id,case when Len(crop1code) <=15 then crop1code else null end as crop1code ,sowingdate1,DATEDIFF (WEEK,sowingdate1,getdate()) ' 
     SET @[email protected]+' FROM ' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New ' 
     SET @[email protected]+' WHERE crop1code is not null AND crop2code is not null' 


     EXEC (@query) 
     SET @query='' 
     --INSER FOR SECONDARY CROP 
     SET @query='INSERT INTO rml_re_consumercrops(subscriptionid,commoditycode,sowingdate,sowingweek)' 
     SET @[email protected]+'SELECT id,case when Len(crop2code) <=15 then crop2code else null end as crop2code ,sowingdate2,DATEDIFF (WEEK,sowingdate2,getdate()) ' 
     SET @[email protected]+'FROM ' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New ' 
     SET @[email protected]+' WHERE crop1code is not null AND crop2code is not null' 

     EXEC (@query) 
     SET @query='' 

     --INSERT FOR rml_re_consumermarkets 
     --INSER FOR PRIMARY CROP 
     SET @query='INSERT INTO rml_re_consumermarkets(cropid,subscriptionid,marketcode,ric)' 
     SET @[email protected]+'SELECT CS.cropid,E.id,case when Len(M.item) <=15 then M.item else null end AS marketcode ,case when Len(R.item) <=15 then R.item else null end AS ric ' 
     SET @[email protected]+'FROM rml_re_consumercrops CS,' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New E ' 
     SET @[email protected]+' OUTER APPLY fnSplit(E.Ric1,'+''','''+') R,' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New F ' 
     SET @[email protected]+' OUTER APPLY fnSplit(F.MandiCode1,'+''','''+') M ' 
     SET @[email protected]+' WHERE F.ID = E.ID 
       AND R.ID = M.ID 
       AND CS.commoditycode = E.crop1code 
       AND CS.subscriptionid=E.id 
       AND CS.subscriptionid=F.id ' 
     SET @[email protected]+' AND E.crop1code is not null AND E.crop2code is not null' 
     SET @[email protected]+' AND F.crop1code is not null AND F.crop2code is not null' 

     EXEC (@query) 
     SET @query='' 
     --INSER FOR SECONDARY CROP 
     SET @query='INSERT INTO rml_re_consumermarkets(cropid,subscriptionid,marketcode,ric) ' 
     SET @[email protected]+'SELECT CS.cropid,E.id,case when Len(M.item) <=15 then M.item else null end AS marketcode,case when Len(R.item) <=15 then R.item else null end AS ric ' 
     SET @[email protected]+'FROM rml_re_consumercrops CS,' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New E ' 
     SET @[email protected]+' OUTER APPLY fnSplit(E.Ric2,'+''','''+') R,' 
     SET @[email protected]+' ['[email protected]+'].'[email protected]+'.'[email protected]+'.rml_re_getConsumerSubscriptionData_New F ' 
     SET @[email protected]+' OUTER APPLY fnSplit(F.MandiCode2,'+''','''+') M ' 
     SET @[email protected]+' WHERE F.ID = E.ID 
       AND R.ID = M.ID 
       AND CS.commoditycode = E.crop2code 
       AND CS.subscriptionid=E.id 
       AND CS.subscriptionid=F.id ' 
     SET @[email protected]+' AND E.crop1code is not null AND E.crop2code is not null' 
     SET @[email protected]+' AND F.crop1code is not null AND F.crop2code is not null' 

    EXEC (@query)  
    COMMIT TRANSACTION PULLCONSUMERDATA 
    UPDATE rml_re_consumerdatasync_log SET status='completed',endtime=getdate() WHERE [email protected] 
    END TRY 
    BEGIN CATCH 
    -- ROLLBACK TRANSACTION PULLCONSUMERDATA 

    UPDATE rml_re_consumerdatasync_log SET status='failure',error_info=ERROR_MESSAGE() WHERE [email protected] 
    END CATCH 

END 

--exec rml_re_GetAndStoreConsumerSubscriptionData 



GO 

답변

0

확인 여기에 처음있는 일도 이동합니다. proc에 값 0의 디버그 매개 변수를 추가합니다. 그런 다음, debuig 매개 변수 값을 1로 설정하면 동적 SQl을 인쇄하지 않고 코드를 추가하십시오. 동적 SQl을 사용할 때마다 표준 연습을해야합니다 proc. 그 동적 인 문장 중 하나는 정확하게 형성되지 않으므로 잘못된 것을 알아 내기 위해 생성 된 SQL을 볼 필요가 있습니다.

catch 블록에 오류를 저장하면 어떤 오류 메시지가 나타 납니까?

관련 문제