Solved

sql  insert tables using loops

Posted on 2013-01-02
7
276 Views
Last Modified: 2013-01-02
How do Insert into multiple tables using loop?

Alter PROCEDURE sp_MoveData_StateTransaction
@id INT = NULL OUT
AS
BEGIN
INSERT INTO [dbo].[State_Transactions]
           ([title_app_id]
            ,[operator_id]
            , location_id
            , record_type_code
            , state_trans_type_code
             ,[transaction_datetime]
             ,[effective_date])
     SELECT [title_app_id]
                  ,[dealer_id]
                  ,'TEC'
                  , '10'
                  , 'T3'
                   ,Getdate()
                   ,Getdate()
       FROM [t21_maryland_dev].[dbo].[title_app] ta
  where ta.[deal_status_id] = 3
  and not exists (select 1 from [dbo].[State_Transactions] st where st.[title_app_id] = ta.[title_app_id])
  SET  @id = @@identity
  INSERT INTO [dbo].[State_Transactions_Liens](trans_id) values (@id)  
END
0
Comment
Question by:Meinhoonaa
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:drmweaver
ID: 38738111
In 2008 the Do While Looping has been updated to use the merge commands. Review this and you should be all set
http://technet.microsoft.com/en-us/library/bb510625.aspx
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38738125
What do you want here. Can you explain more?
In case you want to insert several times using the same stored procecedure, suppose 10 times, you can run the stored procedure in a loop.

declare @a int = 0;
while (@a < 10)
begin
      exec sp_MoveData_StateTransaction
end

you can modify it as per your requirement. Or you can simply modify the stored procedure if the inserting value is always same and if you are just doing it to get the identity value. In that case, output clause with inserted table comes handy.
0
 

Author Comment

by:Meinhoonaa
ID: 38738196
I think I dont need a loop if I can overcome the following error.
Msg 3902, Level 16, State 1, Procedure sp_MoveData_StateTransaction, Line 29
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

Code below

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Author,,Sam Fecto>
-- Create date: <01/02/2013,,>
-- Description:      <Transfer data from Title App tables to State Transaction tables>
-- =============================================
--exec sp_MoveData_StateTransaction
Alter PROCEDURE sp_MoveData_StateTransaction
@id INT = NULL OUT
AS
BEGIN
INSERT INTO [dbo].[State_Transactions]
           ([title_app_id]
            ,[operator_id]
            , location_id
            , record_type_code
            , state_trans_type_code
             ,[transaction_datetime]
             ,[effective_date])
     SELECT [title_app_id]
                  ,[dealer_id]
                  ,'TEC'
                  , '10'
                  , 'T3'
                   ,Getdate()
                   ,Getdate()
       FROM [t21_maryland_dev].[dbo].[title_app] ta
  where ta.[deal_status_id] = 3
  and not exists (select 1 from [dbo].[State_Transactions] st where st.[title_app_id] = ta.[title_app_id])
  commit
  --SET  @id = @@identity
  --INSERT INTO [dbo].[State_Transactions_Liens](trans_id) values (@id)
BEGIN
  INSERT INTO [dbo].[State_Transactions_Liens](trans_id)
  SELECT trans_id
       FROM [dbo].[State_Transactions] ta
  where 1=1
  and not exists (select 1 from [dbo].[State_Transactions_Liens] st where st.trans_id = ta.trans_id)  
END
END
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 25

Accepted Solution

by:
TempDBA earned 500 total points
ID: 38738211
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Author,,Sam Fecto>
-- Create date: <01/02/2013,,>
-- Description:      <Transfer data from Title App tables to State Transaction tables>
-- =============================================
--exec sp_MoveData_StateTransaction
Alter PROCEDURE sp_MoveData_StateTransaction
@id INT = NULL OUT
AS
BEGIN
BEGIN tran
INSERT INTO [dbo].[State_Transactions]
           ([title_app_id]
            ,[operator_id]
            , location_id
            , record_type_code
            , state_trans_type_code
             ,[transaction_datetime]
             ,[effective_date])
     SELECT [title_app_id]
                  ,[dealer_id]
                  ,'TEC'
                  , '10'
                  , 'T3'
                   ,Getdate()
                   ,Getdate()
       FROM [t21_maryland_dev].[dbo].[title_app] ta
  where ta.[deal_status_id] = 3
  and not exists (select 1 from [dbo].[State_Transactions] st where st.[title_app_id] = ta.[title_app_id])
 commit tran
  --SET  @id = @@identity
  --INSERT INTO [dbo].[State_Transactions_Liens](trans_id) values (@id)
BEGIN
  INSERT INTO [dbo].[State_Transactions_Liens](trans_id)
  SELECT trans_id
       FROM [dbo].[State_Transactions] ta
  where 1=1
  and not exists (select 1 from [dbo].[State_Transactions_Liens] st where st.trans_id = ta.trans_id)  
END
END
0
 
LVL 25

Expert Comment

by:TempDBA
ID: 38738218
But putting a transaction here doesn't seems logical. You might need to insert at both the tables else not right. So, move the second code  within the transaction block, remove begin end for them.
0
 

Author Comment

by:Meinhoonaa
ID: 38738244
How do I move the second code  within the transaction block, remove begin end for them?

Can you help?
0
 

Author Comment

by:Meinhoonaa
ID: 38738287
You mean like this

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:            <Author,,Sam Fecto>
-- Create date: <01/02/2013,,>
-- Description:      <Transfer data from Title App tables to State Transaction tables>
-- =============================================
--exec sp_MoveData_StateTransaction_rev
Alter PROCEDURE sp_MoveData_StateTransaction_rev
@id INT = NULL OUT
AS
BEGIN
 BEGIN tran
INSERT INTO [dbo].[State_Transactions]
           ([title_app_id]
            ,[operator_id]
            , location_id
            , record_type_code
            , state_trans_type_code
             ,[transaction_datetime]
             ,[effective_date])
     SELECT [title_app_id]
                  ,[dealer_id]
                  ,'TEC'
                  , '10'
                  , 'T3'
                   ,Getdate()
                   ,Getdate()
       FROM [t21_maryland_dev].[dbo].[title_app] ta
  where ta.[deal_status_id] = 3
  and not exists (select 1 from [dbo].[State_Transactions] st where st.[title_app_id] = ta.[title_app_id])
  commit tran
BEGIN tran
  INSERT INTO [dbo].[State_Transactions_Liens](trans_id)
  SELECT trans_id
       FROM [dbo].[State_Transactions] ta
  where 1=1
  and not exists (select 1 from [dbo].[State_Transactions_Liens] st where st.trans_id = ta.trans_id)  
END
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now