Solved

sql  insert tables using loops

Posted on 2013-01-02
7
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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