What your code requires is Multiple Active Statement support (or as MS calls it MARS) as you have a cursor open (and outstanding rows unread) whilst trying to execute another statement (the alter). You have 2 choices. 1) as runrig suggests or 2) enable multiple active statement support (assuming your sql server driver and database support it. You can do the latter by adding MARS_Connection=yes to your connect string. For this simple case I believe you are better reorganizing your code and avoiding MARS as a) there are a few gotchas with MARS support and b) your script will be compatible with more ms sql servers.

UPDATE: There is a 3rd choice. Open 2 connections to the database.


In reply to Re: DBI DBD::ODBC, Create a table, drop its constraints, then recreate it again! by mje
in thread DBI DBD::ODBC, Create a table, drop its constraints, then recreate it again! by blackadder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.