Solved

sql  insert tables using loops

Posted on 2013-01-02
7
281 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

10 Experts available now in Live!

Get 1:1 Help Now