Update in Cursor not possible

  • Hi

    There is a first cursor inserting data in table x. After that a second cursor should update table x. But each query in the second cursor outputs '0 records affected'. If the second cursor is started manualy or in a separate procedure it works. So how to avoid this lock? I tried with transaction but with the same problem.

    Thanks for help

    Jan

  • Are you saying that you have nested cursors?  I would say that the best way to avoid this problem is to avoid using cursors.  If you post your code, you may get more help.  You may even get a set based solution for your problem.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • No, there is no nesting.

    The code is very long but looks like:

    cursor

    insert in table x

    cursor end

    cursor

    update in table x

    cursor end

    It looks like the first cursor would not give the table free and all the updates failed.

  • Ok, here is the code, some lines I took out (...) only settings.

    So the cursor pdfdata can't update the data after the cursor descr has finished. If I start the second cursor manualy it works.

    I am wondering how you would loop thru query data without using of a cursor.

    Code----------------------------------

    AS

    BEGIN

    --Declaration an init

    ...

    -- selektieren center_id und user (int99xy)

    DECLARE region CURSOR FOR

    SELECT center_id, db_user

    FROM center a, gruppen_center b

    WHERE a.region = b.region

    AND (a.centerinlist = 1

    OR a.center_id = 'LU')

    ORDER BY center_id

    -- selektieren aller importieren Datensätze

    DECLARE descr CURSOR FOR

    SELECT beschr_id, datum, sprache_id, subtyp, format, text

    FROM imp_beschr

    ORDER BY beschr_id, datum, sprache_id, subtyp, zeilenr, format, text

    OPEN descr

    FETCH NEXT FROM descr INTO @beschr_id, @datum, @sprache_id, @subtyp, @format, @text

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    -- pruefen ob Key und somit die Beschreibung wechselt

    IF (@act_beschr_id != @beschr_id OR @act_datum != @datum OR @act_sprache_id != @sprache_id)

    BEGIN

    -- vor erstem Datensatz kein Insert

    IF (@act_beschr_id != '0')

    BEGIN

    -- insert der Beschreibung (gleiches Query am Ende für letzte Beschreibung)

    INSERT INTO beschr(

    beschr_id, sprache_id, datum, sektor_id, fach_id, titel, beschreibung, voraussetzung, inhalt, zielgruppe, lernziele, abschluss, regional, keywords, type, redaktion, erfuser, erfdat

    )VALUES(

    @act_beschr_id, @act_sprache_id, Cast(@act_datum AS smalldatetime), @sektor_id, @fach_id, Left(@titel,100), Left(@beschreibung,2500), Left(@voraussetzung,2500), Left(@inhalt,2500), Left(@zielgruppe,2000), Left(@lernziele,2000), Left(@abschluss,2000), Left(@regional,2000), Left(@keywords,500), 'sap', 'import', @erfuser, Convert(varchar(16), getdate(), 20)

    )

    END

    -- zuweisen Key (Beschreibungs ID, Datum, Sprache ID) der nächsten Beschreibung

    SET @act_beschr_id = @beschr_id

    SET @act_datum = @datum

    SET @act_sprache_id = @sprache_id

    -- Init Vars

    SET @beschreibung = ''

    SET @lernziele = ''

    SET @inhalt = ''

    SET @voraussetzung = ''

    SET @zielgruppe = ''

    SET @regional = ''

    SET @keywords = ''

    SET @abschluss = ''

    SET @titel = ''

    SET @erfuser = ''

    print('Key ') print(@act_beschr_id) print(@act_sprache_id) print(@act_datum)

    END

    -- Formatierung

    ...

    -- Text konkatinieren gemäss Subtyp

    IF (@subtyp = 1) -- Steuerzeile 0001

    BEGIN

    -- Parameter von Steuerzeile zuweisen

    SET @paramlist = RTrim(@text)

    WHILE(@count < 4)

    BEGIN

    SET @pos = CHARINDEX(';', @paramlist, @startpos)

    --IF (@pos != 0) -- Komma gefunden

    SET @len = @pos - @startpos

    --ELSE -- letzter Wert

    --SET @len = Len(@paramlist) - @startpos +1

    SET @param = SUBSTRING(@paramlist, @startpos, @len)

    SET @startpos = @startpos + @len +1

    -- Params zuteilen

    IF (@count = 0)

    SET @sektor_id = @param

    ELSE IF (@count = 1)

    SET @fach_id = @param

    ELSE IF (@count = 2)

    SET @area = @param

    ELSE IF (@count = 3)

    SET @center_id = @param

    SET @count = @count +1

    END

    -- Reset Vars

    SET @count = 0

    SET @startpos = 1

    -- Erfasser ermitteln

    IF (@area = 'n')

    SET @erfuser = 'int99national'

    ELSE

    BEGIN

    OPEN region

    FETCH NEXT FROM region INTO @ctr_id, @db_user

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    IF (@ctr_id = @center_id)

    BEGIN

    SET @erfuser = @db_user

    BREAK

    END

    FETCH NEXT FROM region INTO @ctr_id, @db_user

    END

    CLOSE region

    END

    -- print(@sektor_id + ' ' + @fach_id + ' ' + @area + ' ' + @center_id + ' ' + @erfuser)

    END

    ELSE IF (@subtyp = 9001) -- Beschreibung kurz

    ...

    FETCH NEXT FROM descr INTO @beschr_id, @datum, @sprache_id, @subtyp, @format, @text

    END

    -- letzte Beschreibung einfügen

    INSERT INTO beschr(

    beschr_id, sprache_id, datum, sektor_id, fach_id, titel, beschreibung, voraussetzung, inhalt, zielgruppe, lernziele, abschluss, regional, keywords, type, redaktion, erfuser, erfdat

    )VALUES(

    @act_beschr_id, @act_sprache_id, Cast(@act_datum AS smalldatetime), @sektor_id, @fach_id, Left(@titel,100), Left(@beschreibung,2500), Left(@voraussetzung,2500), Left(@inhalt,2500), Left(@zielgruppe,2000), Left(@lernziele,2000), Left(@abschluss,2000), Left(@regional,2000), Left(@keywords,500), 'sap', 'import', @erfuser, Convert(varchar(16), getdate(), 20)

    )

    -- Ressourcen

    CLOSE descr

    DEALLOCATE descr

    DEALLOCATE region

    -- gesicherte PDF Daten zurückspeichern

    DECLARE pdfdata CURSOR FOR

    SELECT bep_beschr_id, bep_beschr_sprache_id, bep_beschr_datum, bep_pdf1_text, bep_pdf1, bep_pdf2_text, bep_pdf2

    FROM beschr_pdf

    OPEN pdfdata

    FETCH NEXT FROM pdfdata INTO @beschr_id, @sprache_id, @date, @pdf1_text, @pdf1, @pdf2_text, @pdf2

    WHILE (@@FETCH_STATUS = 0)

    BEGIN

    IF @pdf1 IS NULL

    SET @pdf1 = ''

    IF @pdf1_text IS NULL

    SET @pdf1_text = ''

    IF @pdf2 IS NULL

    SET @pdf2 = ''

    IF @pdf2_text IS NULL

    SET @pdf2_text = ''

    SET @query = '

    UPDATE beschr

    SET pdf1 = ' + CHAR(39) + @pdf1 + CHAR(39) + ',

    pdf1_text = ' + CHAR(39) + @pdf1_text + CHAR(39) + ',

    pdf2 = ' + CHAR(39) + @pdf2 + CHAR(39) + ',

    pdf2_text = ' + CHAR(39) + @pdf2_text + CHAR(39) + '

    WHERE beschr_id = ' + CHAR(39) + @beschr_id + CHAR(39) + '

    AND sprache_id = ' + CHAR(39) + @sprache_id + CHAR(39) + '

    AND datum = ' + CHAR(39) + Convert(varchar, @date, 120) + CHAR(39)

    --print(@query)

    exec(@query)

    FETCH NEXT FROM pdfdata INTO @beschr_id, @sprache_id, @date, @pdf1_text, @pdf1, @pdf2_text, @pdf2

    END

    -- Ressourcen

    CLOSE pdfdata

    DEALLOCATE pdfdata

    END

  • How about your table DDL?  Can you post some sample data and what your data should look like after a sucessful run of your procedure?

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Great analogy Joe.  That's an easy way to get set based thinking accross to people.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Heh.  Maybe that's why I like being a DB Developer.  My very first "professional" computer job was updating / upgrading a Lotus 123 spreadsheet (back in the DOS days...) that my college used to calculate financial aid packages.

  • @pam Abdulla

    Let me explain why with DOS technic and without flow control it may not work.

    @john-2 Rowan

    Basicly its about collecting data to habe a course description finaly. The description will have fields like description, targetgroup, certificate and so on. Because the origin DB holds the data line by line while using for single source publishing. So the first loop is over the import table:

    CREATE TABLE [imp_beschr] (

    [beschr_id] [varchar] (20) NULL ,

    [datum] [varchar] (8) NULL ,

    [sprache_id] [varchar] (2) NULL ,

    [subtyp] [smallint] NULL ,

    [zeilenr] [smallint] NULL ,

    [format] [smallint] NULL ,

    [text] [varchar] (100) NULL

    ) ON [PRIMARY]

    These Data from the field text will be concatenated according the format code to html and finaly inserted in the table:

    CREATE TABLE [Beschr] (

    [Beschr_ID] [varchar] (20) NOT NULL ,

    [Sprache_ID] [varchar] (2) NOT NULL ,

    [datum] [smalldatetime] NOT NULL ,

    [Sektor_ID] [varchar] (2) NULL ,

    [Fach_ID] [varchar] (5) NULL ,

    [Titel] [varchar] (100) NULL ,

    [Beschreibung] [varchar] (2500) NULL ,

    [Voraussetzung] [varchar] (2500) NULL ,

    [Bemerkung] [varchar] (500) NULL ,

    [inhalt] [varchar] (2500) NULL ,

    [zielgruppe] [varchar] (2000) NULL ,

    [lernziele] [varchar] (2000) NULL ,

    [abschluss] [varchar] (2000) NULL ,

    [regional] [varchar] (2000) NULL ,

    [PDF1_Text] [varchar] (40) NULL ,

    [PDF1] [varchar] (255) NULL ,

    [PDF2_Text] [varchar] (40) NULL ,

    [PDF2] [varchar] (255) NULL ,

    [Keywords] [varchar] (500) NULL ,

    [type] [varchar] (20) NULL ,

    [Redaktion] [varchar] (40) NULL ,

    [ErfUser] [varchar] (20) NULL ,

    [ErfDat] [datetime] NULL ,

    [MutRedaktion] [varchar] (20) NULL ,

    [MutDat] [datetime] NULL ,

    CONSTRAINT [PK_Beschr] PRIMARY KEY CLUSTERED

    (

    [Beschr_ID],

    [Sprache_ID],

    [datum]

    ) ON [PRIMARY]

    ) ON [PRIMARY]

    These table holds the hole description. Because of the missing pdf data the second cursor restores the data from the table:

    CREATE TABLE [beschr_pdf] (

    [bep_beschr_id] [varchar] (20) NOT NULL ,

    [bep_beschr_sprache_id] [varchar] (2) NOT NULL ,

    [bep_beschr_datum] [smalldatetime] NOT NULL ,

    [bep_pdf1_text] [varchar] (40) NULL ,

    [bep_pdf1] [varchar] (255) NULL ,

    [bep_pdf2_text] [varchar] (40) NULL ,

    [bep_pdf2] [varchar] (255) NULL ,

    CONSTRAINT [PK_beschr_pdf] PRIMARY KEY CLUSTERED

    (

    [bep_beschr_id],

    [bep_beschr_sprache_id],

    [bep_beschr_datum]

    ) ON [PRIMARY]

    ) ON [PRIMARY]

    These data will update the Beschr table and failed after the first cursor. If I run it again, only the second cursor it updates the table.

    @joe Celco

    So I'm not sure about to find an easy solution with just joining some tables together. And also a technical other way should not lead us away from the problem that after closing a cursor the next one fails because of a db state undefined. The suggestion not to use cursors is like after having problems with a car engine to say use the train. My question is why and these question will help us(without Pam ) not to fall back to DOS history.

    Good night

    Jan

  • OK Jan.  The table DDL will help, but can you post a few lines of sample data from each table along with how you want the data to appear in Beschr?  Sorry to keep asking for more info, but the data is an important piece of the puzzle here (especially due to the language barrier with your field/table names). 

    Thanks.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Hope you can read the data in this form:

    From table imp_beschr (import description):

    beschr_id datum sprache_id subtyp zeilenr format text

    -------------------- -------- ---------- ------ ------- ------ ----------------------------------------------------------------------------------------------------

    CG_00010635 20050801 D 1 1 0 23;870;n;;

    CG_00010635 20050801 D 9001 1 0 197 Konfigurationsmanagementsystem konzipieren und implementieren

    CG_00010635 20050801 D 9003 1 0 Die Ausbildung ist in 11 Module aufgegliedert. Die Module dauern

    CG_00010635 20050801 D 9003 2 0 zwischen 3 - 5 Tage. Ergänzender und vertiefender Lehrstoff ist durch

    CG_00010635 20050801 D 9003 3 0 die Teilnehmerinnen und Teilnehmer im Selbststudium zu erarbeiten.

    CG_00010635 20050801 D 9003 4 0 Jedes Modul ist in sich abgeschlossen und kann auch einzeln besucht

    CG_00010635 20050801 D 9003 5 0 werden.

    CG_00010635 20050801 D 9003 7 3 Modul 191: IT Teilprojekte abwickeln

    CG_00010635 20050801 D 9003 8 4 Modul 249: IT Teilprojekte planen und initialisieren

    CG_00010635 20050801 D 9003 9 4 Modul 170: Managementinformationen beschaffen und aufbereiten

    CG_00010635 20050801 D 9003 10 4 Modul 167: Anforderungen ableiten und Evaluation durchführen

    CG_00010635 20050801 D 9003 11 4 Modul 166: IT Grundschutz modellieren

    CG_00010635 20050801 D 9003 12 4 Modul 176: Datenschutz, Daten- und Verarbeitungssicherheit gewährleisten

    CG_00010635 20050801 D 9003 13 4 Modul 197: Konfigurationsmanagementsystem konzipieren und implementieren

    CG_00010635 20050801 D 9003 14 4 Modul 168: Geschäftsprozesse modellieren

    CG_00010635 20050801 D 9003 15 4 Modul 227: Testen

    CG_00010635 20050801 D 9003 16 4 Modul 228: Kundenzufriedenheit sicherstellen

    CG_00010635 20050801 D 9003 17 4 Modul 207: IT Dienstleistungen budgetieren

    CG_00010635 20050801 D 9003 17 5

    CG_00010635 20050801 D 9004 1 3 Fähigkeitsausweis einer dreijährigen Berufslehre oder

    CG_00010635 20050801 D 9004 2 4 Fachausweis einer Berufsprüfung oder

    CG_00010635 20050801 D 9004 3 4 Maturitätszeugnis oder

    CG_00010635 20050801 D 9004 4 4 ein vom Bund anerkanntes Handelsdiplom.

    CG_00010635 20050801 D 9004 4 5

    CG_00010635 20050801 D 9004 6 0 Zusätzlich, nach abgeschlossener Grundausbildung, mindestens 3 Jahre

    CG_00010635 20050801 D 9004 7 0 praktische Berufstätigkeit in einer der Ausbildung entsprechenden

    CG_00010635 20050801 D 9004 8 0 Informatikfachrichtung. Massgebend dafür ist der Zeitpunkt der eidg.

    CG_00010635 20050801 D 9004 9 0 Fachausweisprüfung.

    All the lines of column text will be concatenated depending on the subtyp and zeilenr(row nr). The format will controll the html tags to be added.

    The next data will be the result of this first cursor inserted in beschr (description):

    Beschr_ID Sprache_ID datum Sektor_ID Fach_ID Titel Beschreibung Voraussetzung Bemerkung inhalt zielgruppe lernziele abschluss regional PDF1_Text PDF1 PDF2_Text PDF2 Keywords type Redaktion ErfUser ErfDat MutRedaktion MutDat

    -------------------- ---------- ------------------------------------------------------ --------- ------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- CG_00010635 de 2005-08-01 00:00:00 23 870 197 Konfigurationsmanagementsystem 197 Konfigurationsmanagementsystem konzipieren und implementieren

    • Fähigkeitsausweis einer dreijährigen Berufslehre oder
    • Fachausweis einer Berufsprüfung oder
    • Maturitätszeugnis oder
    • ein vom Bund anerkanntes Handelsdiplom.

    Zusätzlich, nach abgeschlossener Grundausbildung, mindest NULL Die Ausbildung ist in 11 Module aufgegliedert. Die Module dauern zwischen 3 - 5 Tage. Ergänzender und vertiefender Lehrstoff ist durch die Teilnehmerinnen und Teilnehmer im Selbststudium zu erarbeiten. Jedes Modul ist in sich abgeschlossen und kann auch ei Intern Jedes Modul wird mit einer ISS-Lernzielkontrolle abgeschlossen. Erfolgreiche TeilnehmerInnen, erhalten ein ISS-Zertifikat. Am Schluss der Ausbildung steht die interne Abschlussprüfung. Bei erfolgreichem Bestehen (Besuch von mind. 80% des Lehrganges) NULL NULL NULL NULL sap import int99national 2006-07-28 02:00:00.000 NULL NULL

    The text is now complete and html formated.

    But the pdf data have to be restored from the next table beschr_pdf (description pdf):

    bep_beschr_id bep_beschr_sprache_id bep_beschr_datum bep_pdf1_text bep_pdf1 bep_pdf2_text bep_pdf2

    -------------------- --------------------- ------------------------------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    CG_00010635 de 2006-08-01 00:00:00 Broschüre "Eidg. Diplome/Fachausweise" 0607_Informatikerin_KEIL.pdf

    Just a loop over this table and the update of table beschr sets back the pdf data.

    Thanks for help.

    Jan

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply