Here's a tidied up and working version of your script
#$dbh->do('DROP TABLE test1'); # uncomment after first run my $sql = 'CREATE TABLE test1 ( type char(5), students int, class int, teachers int, hostel char(10) )'; $dbh->do($sql); my $sqli = 'INSERT INTO test1 VALUES (?,?,?,?,?)'; my $sth = $dbh->prepare($sqli); # 1 records my @fld=(); # read data foreach (<DATA>) { chomp $_; my ($event,$data)=split(/:/,$_); if ($event =~ /\(B\)/){ insert_record() if ($fld[0]); $fld[0] = "BOYS"; } if( $event =~ /\(G\)/){ insert_record() if ($fld[0]); $fld[0] = "GIRLS"; } if ($event =~ /^No.of stud/i){ $fld[1] = $data; } if($event =~ /^No.of class/i){ $fld[2] = $data; } if($event =~ /^No.of teach/i){ $fld[3] = $data; } if($event =~ /^Hostel facility/i){ $fld[4] = $data; } } # don't forget last record insert_record() if ($fld[0]); # insert one records sub insert_record { print "Inserting @fld\n"; $sth->execute(@fld[0..4]); @fld=(); } __DATA__ +++++++++++School(B):Students++++++++++ No.of students:120 No.of classes:2 No.of teachers:5 Hostel facility:available ++++++++++School(G):Students+++++++++++ No.of students:300 No.of classes:3 No.of teachers:10
poj

In reply to Re: Data insert into SQL Server? by Anonymous Monk
in thread Data insert into SQL Server? by sowraaj

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.