Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Inserting Multiple Records Using While Loop Expand / Collapse
Author
Message
Posted Wednesday, January 16, 2013 6:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:42 PM
Points: 3, Visits: 5
I'm trying to insert multiple records in while loop but I keep getting cannot insert duplicate into primary key error. I'm trying to insert same information into temp table except for linecd should be 4 or 5. Issue seems to be with while loop.

create table #TempSalesGLLink(
AHInsCostAcct varchar(10)
, AHInsCostDept varchar(10)
, AHInsRevenueAcct varchar(10)
, AHInsRevenueDept varchar(10)
, CashDownAcct varchar(10)
, CashDownDept varchar(3)
, CLInsCostAcct varchar(10)
, CLInsCostDept varchar(10)
, CLInsRevenueAcct varchar(10)
, CLInsRevenueDept varchar(10)
, CostGoodsAcct varchar(10)
, CostGoodsDept varchar(3)
, CostofSalesSubAcct varchar(10)
, CostofSalesSubDept varchar(10)
, DueFromFinAcct varchar(10)
, DueFromFinDept varchar(3)
, DueToCustAcct varchar(10)
, DueToCustDept varchar(3)
, InvSoldAcct varchar(10)
, InvSoldDept varchar(3)
, LineCd char(2)
, LoanDiscountAcct varchar(10)
, LoanDiscountDept varchar(3)
, LocId varchar(10)
, NSFFeeAcct varchar(10)
, NSFFeeDept varchar(3)
, NSFRecAcct varchar(10)
, NSFRecDept varchar(10)
, OtherIncomeAcct varchar(10)
, OtherIncomeDept varchar(3)
, OverAllowAcct varchar(10)
, OverAllowDept varchar(3)
, SalesGLLinkUniqueNum int
, SalesTypeCd varchar(5)
, StoreId varchar(10)
, TradeACVAcct varchar(10)
, TradeACVDept varchar(3)
, VehSaleAcct varchar(5)
, VehSaleDept varchar(3),
CONSTRAINT [PK_SalesGLLink] PRIMARY KEY
(
[StoreId] ASC,
[LocId] ASC,
[SalesGLLinkUniqueNum] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]




SET NOCOUNT ON


--Insert Into #TempSalesGLLink(AHInsCostAcct, AHInsCostDept, AHInsRevenueAcct, AHInsRevenueDept, CashDownAcct, CashDownDept, CLInsCostAcct, CLInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CostGoodsAcct,
--CostGoodsDept, CostofSalesSubAcct, CostofSalesSubDept, DueFromFinAcct, DueFromFinDept, DueToCustAcct, DueToCustDept, InvSoldAcct, InvSoldDept, LineCd, LoanDiscountAcct, LoanDiscountDept, LocId
--, NSFFeeAcct, NSFFeeDept, NSFRecAcct, NSFRecDept, OtherIncomeAcct, OtherIncomeDept, OverAllowAcct, OverAllowDept, SalesGLLinkUniqueNum , SalesTypeCd, StoreId
--, TradeACVAcct, TradeACVDept, VehSaleAcct, VehSaleDept
--)


--/* Gets the Product Description, MembershipNo, DateAdded, LastName and FirstName, and BranchName for New Members */
-- select AHInsCostAcct, AHInsCostDept, AHInsRevenueAcct, AHInsRevenueDept, CashDownAcct, CashDownDept, CLInsCostAcct, CLInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CostGoodsAcct,
-- CostGoodsDept, CostofSalesSubAcct, CostofSalesSubDept, DueFromFinAcct, DueFromFinDept, DueToCustAcct, DueToCustDept, InvSoldAcct, InvSoldDept, LineCd, LoanDiscountAcct, LoanDiscountDept, LocId,
-- NSFFeeAcct, NSFFeeDept, NSFRecAcct, NSFRecDept, OtherIncomeAcct, OtherIncomeDept, OverAllowAcct, OverAllowDept, SalesGLLinkUniqueNum , SalesTypeCd, StoreId,
-- TradeACVAcct, TradeACVDept, VehSaleAcct, VehSaleDept from SalesGLLink where salestypecd = 'r' and linecd = '3'


declare @rowCount int
select @rowCount = count(*) from SalesGLLink where salestypecd = 'r' and linecd = '3'
Print @rowCount
declare @counter int
select @counter = 0


[color=#FF4040]while @counter < @rowCount[/color]
begin


Insert Into #TempSalesGLLink(StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept)


select StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, '4' as LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept from SalesGLLink where salestypecd = 'r' and linecd = '3'


Insert Into #TempSalesGLLink(StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept
)


select StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, '5' as LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept from SalesGLLink where salestypecd = 'r' and linecd = '3'

select @counter = @counter + 1
Print @Counter
end


select StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept from #TempSalesGLLink where LineCd = '4' or LineCd = '5'
--drop table #TempSalesGLLink
Post #1408114
Posted Wednesday, January 16, 2013 10:34 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, October 8, 2013 7:13 PM
Points: 1, Visits: 44
Just like the error said...you have a primary key on the table and primary keys don't accept duplicates
Post #1408147
Posted Thursday, January 17, 2013 4:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:42 PM
Points: 3, Visits: 5
Thanks for the reply. I know what the error means. I'm trying to figure out why when performing the loop it keeps putting in the same records multiple times. That's why I added the primary key to the temp table.
Post #1408310
Posted Thursday, January 17, 2013 4:07 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 9:35 AM
Points: 2,114, Visits: 3,720
On that PK violation - As far as I can tell, each loop just inserts the same data, so you will get the PK violation on the second iteration?
Post #1408314
Posted Thursday, January 17, 2013 4:08 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 9:35 AM
Points: 2,114, Visits: 3,720
cdub11532 (1/17/2013)
Thanks for the reply. I know what the error means. I'm trying to figure out why when performing the loop it keeps putting in the same records multiple times. That's why I added the primary key to the temp table.


Because your select statement is the same in each loop
Post #1408315
Posted Thursday, January 17, 2013 5:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 17, 2013 2:42 PM
Points: 3, Visits: 5
Thank you Gazareth. I looked again and realized I didn't even need the loop.
All I need is and really I don't even need the temp table just need to do the insert on the permanent table. I think my brain was on vacation yesterday.
create table #TempSalesGLLink(
AHInsCostAcct varchar(10)
, AHInsCostDept varchar(10)
, AHInsRevenueAcct varchar(10)
, AHInsRevenueDept varchar(10)
, CashDownAcct varchar(10)
, CashDownDept varchar(3)
, CLInsCostAcct varchar(10)
, CLInsCostDept varchar(10)
, CLInsRevenueAcct varchar(10)
, CLInsRevenueDept varchar(10)
, CostGoodsAcct varchar(10)
, CostGoodsDept varchar(3)
, CostofSalesSubAcct varchar(10)
, CostofSalesSubDept varchar(10)
, DueFromFinAcct varchar(10)
, DueFromFinDept varchar(3)
, DueToCustAcct varchar(10)
, DueToCustDept varchar(3)
, InvSoldAcct varchar(10)
, InvSoldDept varchar(3)
, LineCd char(2)
, LoanDiscountAcct varchar(10)
, LoanDiscountDept varchar(3)
, LocId varchar(10)
, NSFFeeAcct varchar(10)
, NSFFeeDept varchar(3)
, NSFRecAcct varchar(10)
, NSFRecDept varchar(10)
, OtherIncomeAcct varchar(10)
, OtherIncomeDept varchar(3)
, OverAllowAcct varchar(10)
, OverAllowDept varchar(3)
, SalesGLLinkUniqueNum int
, SalesTypeCd varchar(5)
, StoreId varchar(10)
, TradeACVAcct varchar(10)
, TradeACVDept varchar(3)
, VehSaleAcct varchar(5)
, VehSaleDept varchar(3)
)
--CONSTRAINT [PK_SalesGLLink] PRIMARY KEY
--(
-- [StoreId] ASC,
-- [LocId] ASC,
-- [SalesGLLinkUniqueNum] ASC
--)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
--) ON [PRIMARY]


SET NOCOUNT ON

Insert Into #TempSalesGLLink(StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept)

select StoreId, LocId, 6, SalesTypeCd, '4' as LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept from SalesGLLink where salestypecd = 'r' and linecd = '3'

Insert Into #TempSalesGLLink(StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept)

select StoreId, LocId, 7, SalesTypeCd, '5' as LineCd, VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept from SalesGLLink where salestypecd = 'r' and linecd = '3'
Post #1408367
Posted Thursday, January 17, 2013 8:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 9:35 AM
Points: 2,114, Visits: 3,720
No problem!
Post #1408450
Posted Thursday, January 17, 2013 8:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
-- you only need to read the SalesGLLink table once:
INSERT INTO #TempSalesGLLink(
StoreId, LocId, SalesGLLinkUniqueNum, SalesTypeCd, LineCd,
VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept)

SELECT
StoreId, LocId, cj.SalesGLLinkUniqueNum, SalesTypeCd, cj.LineCd,
VehSaleAcct, VehSaleDept, TradeACVAcct, TradeACVDept, OverAllowAcct, OverAllowDept, LoanDiscountAcct, LoanDiscountDept,
DueFromFinAcct, DueFromFinDept, CashDownAcct, CashDownDept, CostGoodsAcct, CostGoodsDept, InvSoldDept, OtherIncomeAcct, OtherIncomeDept, DueToCustAcct, DueToCustDept, NSFFeeAcct, NSFFeeDept, NSFRecAcct,
NSFRecDept, CostofSalesSubAcct, CostofSalesSubDept, AHInsRevenueAcct, AHInsRevenueDept, AHInsCostAcct, AHInsCostDept, CLInsRevenueAcct, CLInsRevenueDept, CLInsCostAcct, CLInsCostDept
FROM SalesGLLink s
CROSS JOIN (SELECT SalesGLLinkUniqueNum = 6, LineCd = '4' UNION ALL SELECT 7, '5') cj
WHERE s.salestypecd = 'r' AND s.linecd = '3'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1408460
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse