Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8013143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:33:11+00:00 2026-06-04T19:33:11+00:00

I have problems with a cursur that’s ends randomly. I want to produce a

  • 0

I have problems with a cursur that’s ends randomly. I want to produce a row for each element , so for every element there is a row for every month in 2012. If there is a row missing for a specific month, it should create that as well.

Now, it’s producing a row for the last element in every month until year 2057.

Now, I have a row for each element until in every month until year 2057 and only for one element in my table.

My table design:

create table tblDataUpdateTest
(
slno int identity(1,1),
cName varchar(50),
cRemarks varchar(50),
[Date] date,
 )

insert into tblDataUpdateTest (cName, cRemarks,[Date]) values ('name1','Text1','2012-01-01'),
('name2','Text2','2012-01-01')

My code:

declare @y as int
declare @d as int
SET @y = 2012
SET @d = 1
Declare @@counter int
Declare @@month int
set @@counter=0
Declare @@slno int
Declare @@cRemarks varchar(100)
Declare @@cName varchar(50)
Declare @@Date date

set @@month = 1
Declare tmepTbl cursor
For
Select slno,cName,cRemarks,date from tblDataUpdateTest

Open tmepTbl /* Opening the cursor */

fetch next from tmepTbl
into @@slno,@@cName,@@cRemarks,@@Date

while @@fetch_Status=-1
begin

if not exists (select cRemarks from tblDataUpdateTest where MONTH(Date) = @@month AND YEAR(Date) = 2012)
begin
insert into tblDataUpdateTest (cName, cRemarks,[Date]) values (@@cName,'s',(DateAdd(yy, 2012-1900,DateAdd(m,  @@month - 1, 01 - 1))))
end

fetch next from tmepTbl
into @@slno,@@cName,@@cRemarks,@@Date

set @@month=@@month+1
end
close tmepTbl
Deallocate tmepTbl

My table now

cName cRemarks Date
name1 Text1    2012-01-01
name2 Text2    2012-01-01

I want follwing to happen:

cName cRemarks Date
name1 Text1    2012-01-01
name1 Text1    2012-02-01
name1 Text1    2012-03-01
name2 Text2    2012-01-01
name2 Text2    2012-02-01
name2 Text2    2012-03-01

and so on.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:33:13+00:00Added an answer on June 4, 2026 at 7:33 pm

    Wouldn’t be better to use a recursive CTE, instead of a cursor?
    Here is an example:

    DECLARE @y INT
    DECLARE @m INT
    DECLARE @n INT
    SET @y = 2012
    SET @m = 1
    SET @n = 3
    
    ;WITH dates(d, m) AS(
        SELECT CONVERT(DATE, CONVERT(VARCHAR(4), @y) + '-01-01'), 1
        UNION ALL
        SELECT CONVERT(DATE, CONVERT(VARCHAR(4), @y) + '-' + CASE WHEN m + 1 < 10 THEN '0' ELSE '' END + CONVERT(VARCHAR(2), m + 1) + '-01')
            , m + 1 
        FROM dates
        WHERE m < @n
    )
    INSERT INTO tblDataUpdateTest(cName, cRemarks,[Date])
        SELECT cName, cRemarks, [date] FROM (
        SELECT cName, cRemarks, dates.d AS [date]
        FROM tblDataUpdateTest
        CROSS JOIN dates) t
        WHERE NOT(EXISTS(SELECT * FROM tblDataUpdateTest WHERE cName = t.cName AND [date] = t.[date]))
    OPTION(MAXRECURSION 11)
    
    SELECT * FROM tblDataUpdateTest ORDER BY cName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that is fed by an SQLiteDB. On each row returned,
I have problems with dealing with widows within a multicols environment, that is, I
Say that I have a system service, and I want to offer a low-level
I have the following JQuery code that is giving me some problems. The first
I have searched for problems like mine ,they are there but do not answer
I'm creating an application and I have problems with Cursor . I have an
i have problems with a really basic thing. setting properties after instantiation a UIViewController-
I have problems with creating a simple Group-Box-Control via CreateWindowEx. The font-size/-style of its
I have problems with CSS. The table is creating white border and I do
I have problems dealing with ProGuard and Android. I searched on the web for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.