Dear All,

Sorry about the above title!

I have this sql statement
$DB->Sql("INSERT INTO Hosts(Host_Name,SAN,location,Country) VALUES ('$host', '$clean_data{$host}'SAN'}', '$clean_data{$host}{'location'}', '$clean_data{$host}{'country'}')");
Then I follow the above with this sql statement to obtain the primary key values of the Hosts record.
die "Sql Failed" . $DB->Error( ) . "\n" if ($DB->Sql ("SELECT Host_ID FROM Hosts WHERE Host_Name='$host'")); my $rec_indx; while ($DB->FetchRow()) { my %data = $DB->DataHash(); $rec_indx = $data{"Host_ID"}; print qq(index key is $rec_indx\n); }
And then with the following code and sql statment, I insert the primary key value into Apps record to create a relationship between the two (I have already created two blank corresponding tables in ms-access with one-to-many relationship between hosts and apps).
for my $info (@{$clean_data{$host}{'applications'}}) { unless ($seen{@{$info}}++) { $DB->Sql("INSERT INTO Apps (App_Name,Description,Host_ID) VALUES ('$$info[0]', '$$info[11]', '$rec_indx')"); } }
My question is;
1- Is there away that I could do away with the second sql statement where I can insert the primary key from Hosts record, into the Apps record without having to revisit the host record again? Similar to this suggestion (node_id=463992).
2- How can I in the first sql statement check to see it the hosts record (i.e the hosts name) doesn’t exists before inserting it? So if it exists then I will run a UPDATE statement instead of instead of creating it?

I am using Win32::ODBC and ms-access.

Thanks very much

Blackadder

In reply to SQL statment to insert if none exits and to obtain the primary key of a record with one visit! 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.