I have a Table shown below
DateRange
10/1
11/1
12/1
I am looping through this table using cursor and trying to populate a temp table
Create table #BidStatusCalculation1
(
Code nvarchar(max),
Data int
)
Declare @monthStart varchar(20)
DECLARE cur CURSOR FOR SELECT DateRange FROM @dateRange
OPEN cur
FETCH NEXT FROM cur INTO @monthStart
WHILE @@FETCH_STATUS = 0 BEGIN
Insert into #BidStatusCalculation1
select 'SL Payroll',Count(*) from DashboardData
where DataType = 'Bid'
and CONVERT(NVARCHAR, CreatedDate, 105) = convert(datetime,
(@monthStart+'/'+@RequestYear))
FETCH NEXT FROM cur INTO @monthStart
END
CLOSE cur
DEALLOCATE cur
This gives me a result like below
Code Data
SL Payroll 22
SL Payroll 33
SL Payroll 43
I am looking to modify the table So I get just one row like
Code Data1 Data2 Data3
SL Payroll 22 33 43
Can someone please tell me what a good approach might be to first Insert Data inside a table on the first loop and then update it. I can put a variable to count the loop iteration and then write separate insert and update but was wondering if there is a better way.
Thanks