Solved

sql  insert tables using loops

Posted on 2013-01-02
7
279 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in query 3 52
AWE-based memory on 32-bit servers 1 23
TSQL previous 5 23
SQL log file keeps growing despite getting successful log backups 4 25
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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

16 Experts available now in Live!

Get 1:1 Help Now