Hi Guys,
I am trying to break a table that contains records accross in 40 columns and create multiple records out of the 40 columns record.
I have 2 Tables, TableA the one that has 40 Columns per Enrollment(Matricula) and the other TableB the one that only has 5 Columns which will store the multiple records broken down from the TableA
TableA = tblPayments
TableB = Payment_Details
Some how the records do not fall in the appropriate columns and this is the part where I am frustrated
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
uspCalculateTimeSheet
Syntax: spBreakDownPayments
*/
CREATE PROC [dbo].[spBreakDownPayments]
AS
DECLARE @Matricula varchar(50)
DECLARE @LineCnt int
DECLARE @sqlMethod NVARCHAR(4000)
DECLARE @OPMethod NVARCHAR(50)
DECLARE @sqlDate NVARCHAR(4000)
DECLARE @OPDate NVARCHAR(50)
DECLARE @sqlAmt NVARCHAR(4000)
DECLARE @OPAmt NVARCHAR(50)
DECLARE @TheMethod varchar(50)
DECLARE @TheDate datetime
DECLARE @TheAmount money
SET @LineCnt=0
SET @TheAmount=0
DECLARE Cursor_Payments CURSOR FOR
SELECT DISTINCT [Matricula] FROM [DBF_File].[dbo].[tblPayments] WHERE [Matricula] IS NOT NULL
OPEN Cursor_Payments
FETCH NEXT FROM Cursor_Payments INTO @Matricula
WHILE @@FETCH_STATUS=0
BEGIN
SET @LineCnt = @LineCnt +1
SET @sqlMethod = 'SELECT @TheMethod=method'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [DBF_Payments] WHERE [matricula]=@Matricula'
EXEC sp_executesql @sqlMethod, N'@Matricula INT, @TheMethod NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheMethod = @TheMethod OUTPUT
---SELECT @TheMethod=ISNULL(@OPMethod,'CASH')
SET @sqlDate ='SELECT @TheDate=paiddate'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [DBF_Payments] WHERE [matricula]=@Matricula'
EXEC sp_executesql @sqlDate, N'@Matricula INT, @TheDate NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheDate = @TheDate OUTPUT
---SELECT @TheDate=TRY_CONVERT(DATETIME,@OPDate)
SET @sqlAmt = 'SELECT @TheAmount=Amount'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [DBF_Payments] WHERE [matricula]=@Matricula'
EXEC sp_executesql @sqlAmt, N'@Matricula INT, @TheAmount NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheAmount = @TheAmount OUTPUT
---SELECT @TheAmount=ISNULL(TRY_CONVERT(MONEY,@OPAmt),0)
------------Append new record
IF NOT EXISTS(select * from Payment_Details where [reference]=@Matricula and [method]=@TheMethod and MONTH([date_paid])=MONTH(@TheDate) and DAY([date_paid])=DAY(@TheDate) and YEAR([date_paid])=YEAR(@TheDate) and [amount_paid]=@TheAmount)
BEGIN
BEGIN TRANSACTION
INSERT INTO [Payment_Details]([date_paid],[method],[amount_paid],[reference])
VALUES(@TheDate,@TheMethod,@TheAmount,@Matricula)
COMMIT
END
----Reset
SET @TheMethod=''
SET @TheDate=NULL
SET @TheAmount=0.00
---- It Reaches the columns count then go to the next
IF (@LineCnt>=40)
BEGIN
SET @LineCnt = 0
END
-----Next Record
FETCH NEXT FROM Cursor_Payments INTO @Matricula
END
CLOSE Cursor_Payments
Deallocate Cursor_PaymentsDownload the data and tables script -->"github.com/netstair/SQLBreakdownData/blob/main/DBF_File_Script_With_Data.sql"
Thank you in advanced.
Jose

Naimish MakwanaRank #324 · 7.7K reputationPosted May 14, 2024, 12:21 AM EDT
From your code, it seems like you’re trying to break down the records from
tblPayments(TableA) into multiple records inPayment_Details(TableB).One thing I noticed is that you’re using
@Matricula INTin yoursp_executesqlcalls, but@Matriculais declared asvarchar(50). This could be causing some issues. You should change@Matricula INTto@Matricula varchar(50)in yoursp_executesqlcalls.Another potential issue could be the way you’re constructing your dynamic SQL queries. You’re using
RTRIM(LTRIM(STR(@LineCnt)))to convert the line count to a string. However,STRfunction can return a string with leading spaces for positive numbers, andLTRIMonly removes spaces from the left side. This could lead to unexpected spaces in your column names. You might want to useCAST(@LineCnt AS varchar(10))instead.Here’s how you can modify your
@sqlMethod,@sqlDate, and@sqlAmtassignments:SET @sqlMethod = 'SELECT @TheMethod=method' + CAST(@LineCnt AS varchar(10)) + ' FROM [DBF_Payments] WHERE [matricula]=@Matricula' SET @sqlDate = 'SELECT @TheDate=paiddate' + CAST(@LineCnt AS varchar(10)) + ' FROM [DBF_Payments] WHERE [matricula]=@Matricula' SET @sqlAmt = 'SELECT @TheAmount=Amount' + CAST(@LineCnt AS varchar(10)) + ' FROM [DBF_Payments] WHERE [matricula]=@Matricula'And here’s how you can modify your
sp_executesqlcalls:EXEC sp_executesql @sqlMethod, N'@Matricula varchar(50), @TheMethod NVARCHAR(50) OUTPUT', @Matricula = @Matricula, @TheMethod = @TheMethod OUTPUT EXEC sp_executesql @sqlDate, N'@Matricula varchar(50), @TheDate datetime OUTPUT', @Matricula = @Matricula, @TheDate = @TheDate OUTPUT EXEC sp_executesql @sqlAmt, N'@Matricula varchar(50), @TheAmount money OUTPUT', @Matricula = @Matricula, @TheAmount = @TheAmount OUTPUTTry these modifications and see if they solve your problem.
Thanks
Jose SaizRank #5,346 · 302 reputationPosted May 14, 2024, 6:43 AM EDT
Thanks Naimish for the reply, but that does not works, remember I have columns as follow
Method1,method2,method3 etc
Date1,Date2,Date3 etc
AmoutPaid1, AmountPaid2, AmountPaid3 etc.
That is why the @LineCnter to add the number to the columns, instead of coding for the 40 columns that Method, date and AmountPaid have.
This is the table I am pulling the records from, I am trying not to code for each columns instead I am using the @LineCnter to give me the column I want SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tblPayments]( [ID] [float] NULL, [name] [nvarchar](255) NULL, [Matricula] [nvarchar](255) NULL, [Paidate1] [nvarchar](255) NULL, [Method1] [nvarchar](255) NULL, [Amount1] [nvarchar](255) NULL, [Paidate2] [nvarchar](255) NULL, [Method2] [nvarchar](255) NULL, [Amount2] [nvarchar](255) NULL, [Paidate3] [nvarchar](255) NULL, [Method3] [nvarchar](255) NULL, [Amount3] [nvarchar](255) NULL, [Paidate4] [nvarchar](255) NULL, [Method4] [nvarchar](255) NULL, [Amount4] [nvarchar](255) NULL, [Paidate5] [nvarchar](255) NULL, [Method5] [nvarchar](255) NULL, [Amount5] [nvarchar](255) NULL, [Paidate6] [nvarchar](255) NULL, [Method6] [nvarchar](255) NULL, [Amount6] [nvarchar](255) NULL, [Paidate7] [nvarchar](255) NULL, [Method7] [nvarchar](255) NULL, [Amount7] [nvarchar](255) NULL, [Paidate8] [nvarchar](255) NULL, [Method8] [nvarchar](255) NULL, [Amount8] [nvarchar](255) NULL, [Paidate9] [nvarchar](255) NULL, [Method9] [nvarchar](255) NULL, [Amount9] [nvarchar](255) NULL, [Paidate10] [nvarchar](255) NULL, [Method10] [nvarchar](255) NULL, [Amount10] [nvarchar](255) NULL, [Paidate11] [nvarchar](255) NULL, [Method11] [nvarchar](255) NULL, [Amount11] [nvarchar](255) NULL, [Paidate12] [nvarchar](255) NULL, [Method12] [nvarchar](255) NULL, [Amount12] [nvarchar](255) NULL, [Paidate13] [nvarchar](255) NULL, [Method13] [nvarchar](255) NULL, [Amount13] [nvarchar](255) NULL, [Paidate14] [nvarchar](255) NULL, [Method14] [nvarchar](255) NULL, [Amount14] [nvarchar](255) NULL, [Paidate15] [nvarchar](255) NULL, [Method15] [nvarchar](255) NULL, [Amount15] [nvarchar](255) NULL, [Paidate16] [nvarchar](255) NULL, [Method16] [nvarchar](255) NULL, [Amount16] [nvarchar](255) NULL, [Paidate17] [nvarchar](255) NULL, [Method17] [nvarchar](255) NULL, [Amount17] [nvarchar](255) NULL, [Paidate18] [nvarchar](255) NULL, [Method18] [nvarchar](255) NULL, [Amount18] [nvarchar](255) NULL, [Paidate19] [nvarchar](255) NULL, [Method19] [nvarchar](255) NULL, [Amount19] [nvarchar](255) NULL, [Paidate20] [nvarchar](255) NULL, [Method20] [nvarchar](255) NULL, [Amount20] [nvarchar](255) NULL, [Paidate21] [nvarchar](255) NULL, [Method21] [nvarchar](255) NULL, [Amount21] [nvarchar](255) NULL, [Paidate22] [nvarchar](255) NULL, [Method22] [nvarchar](255) NULL, [Amount22] [nvarchar](255) NULL, [Paidate23] [nvarchar](255) NULL, [Method23] [nvarchar](255) NULL, [Amount23] [nvarchar](255) NULL, [Paidate24] [nvarchar](255) NULL, [Method24] [nvarchar](255) NULL, [Amount24] [nvarchar](255) NULL, [Paidate25] [nvarchar](255) NULL, [Method25] [nvarchar](255) NULL, [Amount25] [nvarchar](255) NULL, [Paidate26] [nvarchar](255) NULL, [Method26] [nvarchar](255) NULL, [Amount26] [nvarchar](255) NULL, [Paidate27] [nvarchar](255) NULL, [Method27] [nvarchar](255) NULL, [Amount27] [nvarchar](255) NULL, [Paidate28] [nvarchar](255) NULL, [Method28] [nvarchar](255) NULL, [Amount28] [nvarchar](255) NULL, [Paidate29] [nvarchar](255) NULL, [Method29] [nvarchar](255) NULL, [Amount29] [nvarchar](255) NULL, [Paidate30] [nvarchar](255) NULL, [Method30] [nvarchar](255) NULL, [Amount30] [nvarchar](255) NULL, [Paidate31] [nvarchar](255) NULL, [Method31] [nvarchar](255) NULL, [Amount31] [nvarchar](255) NULL, [Paidate32] [nvarchar](255) NULL, [Method32] [nvarchar](255) NULL, [Amount32] [nvarchar](255) NULL, [Paidate33] [nvarchar](255) NULL, [Method33] [nvarchar](255) NULL, [Amount33] [nvarchar](255) NULL, [Paidate34] [nvarchar](255) NULL, [Method34] [nvarchar](255) NULL, [Amount34] [nvarchar](255) NULL, [Paidate35] [nvarchar](255) NULL, [Method35] [nvarchar](255) NULL, [Amount35] [nvarchar](255) NULL, [Paidate36] [nvarchar](255) NULL, [Method36] [nvarchar](255) NULL, [Amount36] [nvarchar](255) NULL, [Paidate37] [nvarchar](255) NULL, [Method37] [nvarchar](255) NULL, [Amount37] [nvarchar](255) NULL, [Paidate38] [nvarchar](255) NULL, [Method38] [nvarchar](255) NULL, [Amount38] [nvarchar](255) NULL, [Paidate39] [nvarchar](255) NULL, [Method39] [nvarchar](255) NULL, [Amount39] [nvarchar](255) NULL, [Paidate40] [nvarchar](255) NULL, [Method40] [nvarchar](255) NULL, [Amount40] [nvarchar](255) NULL ) ON [PRIMARY] GOI think I got it, by changing my store proc to this one
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* uspCalculateTimeSheet Syntax: spBreakDownPayments */ ALTER PROC [dbo].[spBreakDownPayments] AS DECLARE @Matricula varchar(50) DECLARE @LineCnt int DECLARE @sqlMethod NVARCHAR(4000) DECLARE @OPMethod NVARCHAR(50) DECLARE @sqlDate NVARCHAR(4000) DECLARE @OPDate NVARCHAR(50) DECLARE @sqlAmt NVARCHAR(4000) DECLARE @OPAmt NVARCHAR(50) DECLARE @TheMethod varchar(50) DECLARE @TheDate NVARCHAR(50) DECLARE @TheAmount NVARCHAR(50) SET @LineCnt=0 SET @TheAmount=0 DECLARE Cursor_Payments CURSOR FOR SELECT DISTINCT [Matricula] FROM [IPADb].[dbo].[ITA_Payments] WHERE [Matricula] IS NOT NULL OPEN Cursor_Payments FETCH NEXT FROM Cursor_Payments INTO @Matricula WHILE @@FETCH_STATUS=0 BEGIN SET @LineCnt = @LineCnt +1 SET @sqlMethod = 'SELECT @TheMethod=method'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [ITA_Payments] WHERE [matricula]='+@Matricula EXEC sp_executesql @sqlMethod, N'@Matricula INT, @TheMethod NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheMethod = @TheMethod OUTPUT SET @sqlDate ='SELECT @TheDate=paidate'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [ITA_Payments] WHERE [matricula]='+@Matricula EXEC sp_executesql @sqlDate, N'@Matricula NVARCHAR(50), @TheDate NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheDate = @TheDate OUTPUT SET @sqlAmt = 'SELECT @TheAmount=Amount'+RTRIM(LTRIM(STR(@LineCnt)))+' FROM [ITA_Payments] WHERE [matricula]='+@Matricula EXEC sp_executesql @sqlAmt, N'@Matricula INT, @TheAmount NVARCHAR(50) OUTPUT',@Matricula = @Matricula, @TheAmount = @TheAmount OUTPUT IF ISDATE(@TheDate)=1 BEGIN IF ISNUMERIC(@TheAmount)=1 BEGIN ------------Append new record IF NOT EXISTS(select * from Payment_Details where [reference]=@Matricula and [method]=@TheMethod and MONTH([date_paid])=MONTH(@TheDate) and DAY([date_paid])=DAY(@TheDate) and YEAR([date_paid])=YEAR(@TheDate) and [amount_paid]=@TheAmount) BEGIN BEGIN TRANSACTION INSERT INTO [Payment_Details]([date_paid],[method],[amount_paid],[reference]) VALUES(@TheDate,ISNULL(@TheMethod,'Cash'),@TheAmount,@Matricula) COMMIT END END END ------Reset SET @TheMethod='' SET @TheDate=NULL SET @TheAmount=0.00 ---- It Reaches the count then go to the next IF (@LineCnt>=40) BEGIN SET @LineCnt = 0 /*---BREAK--*/ -----Next Record FETCH NEXT FROM Cursor_Payments INTO @Matricula END END CLOSE Cursor_Payments Deallocate Cursor_PaymentsYour comment will be very much appriciated, Please let me know if I am still doing it wrong or missing any.
Thank you guys