473,507 Members | 4,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

insert in SQL Database

3 New Member
Hi,
I'm trying to insert NULL for a column into SQL DB through a formating a string, but not through insert ...values (?) and i don't see a NULL in the DB., the column value appears blank, but i expect a NULL.

When i explictly assign NULL to that variable, it treats the values as a string, not a NULL and isnull is not returning the ROW.


$_data will have an address of the row fetched.

Program:
Expand|Select|Wrap|Line Numbers
  1.   while ($_data = $_src_dbh->fetch_row())
  2.                 {
  3.                                 $i++;
  4.                                 #write to datafile
  5.  
  6. if (not defined $_data->{'level_3'} )
  7.            $_data->{'level_3'} = undef;
  8.  
  9. my $string = "insert into ".$_db_name."..hierarchy_buffer (client_scheme_code,
  10.                                                                  client_level_1_code,
  11.                                                                  client_level_2_code,
  12.                                                                  client_level_3_code,
  13.                                                                  client_security_id)
  14.                             values (
  15.                                   '". $_data->{'scheme'} ."',
  16.                                   '". $_data->{'level_1'} ."',
  17.                                   '". $_data->{'level_2'} ."',
  18.                                   '". $_data->{'level_3'} ."',
  19.                                   '". $_data->{'client_security_id'}."') \n";
  20.  
  21.  
  22.  
If we insert the values thru values ( ? ), then undef is working properly and has a NULL in the DB, since i'm formating the string, the undef value appears as a blank in the DB, but not NULL. So when is use isnull() it is not returning the row.

Thanks
Atif
Oct 4 '07 #1
4 2529
eWish
971 Recognized Expert Contributor
Which database are you using? Here is a sample of how you can handle the undef / null values with MySQL using the DBI.

Expand|Select|Wrap|Line Numbers
  1. my $var1 = 'Some Name';
  2. my $var2 = '';
  3.  
  4. my $check_var = $var2 eq '' ? undef : $var2;
  5.  
  6. my $sql = $dbh->prepare(qq{INSERT INTO test_table (col1, col2) VALUES(?,?)});
  7.    $sql->execute($var1, $check_var);
  8.    $sql->finish();
When you check the database it should have the 'null' in lieu of the word "undef" or the field being empty.
Oct 4 '07 #2
AtifurRahman
3 New Member
Which database are you using? Here is a sample of how you can handle the undef / null values with MySQL using the DBI.

Expand|Select|Wrap|Line Numbers
  1. my $var1 = 'Some Name';
  2. my $var2 = '';
  3.  
  4. my $check_var = $var2 eq '' ? undef : $var2;
  5.  
  6. my $sql = $dbh->prepare(qq{INSERT INTO test_table (col1, col2) VALUES(?,?)});
  7.    $sql->execute($var1, $check_var);
  8.    $sql->finish();
When you check the database it should have the 'null' in lieu of the word "undef" or the field being empty.

Hi,
I'm using MS SQL Db. My issue is i'm not using insert...values( ? ?) syntax, if I use values along with ?, then undef is working properly. But I'm formatting a string, as shown below

Expand|Select|Wrap|Line Numbers
  1. my $string = "insert into ".$_db_name."..pre_bcp_hierarchy_buffer (client_scheme_code,
  2.                                                                  client_level_1_code,
  3.                                                                  client_level_2_code,
  4.                                                                  client_level_3_code,
  5.                                                                  client_security_id)
  6.                             values (
  7.                                   '". $_data->{'scheme'} ."',
  8.                                   '". $_data->{'level_1'} ."',
  9.                                   '". $_data->{'level_2'} ."',
  10.                                   '". $_data->{'level_3'} ."',
  11.                                   '". $_data->{'client_security_id'}."') \n";
  12.  
  13. print " String Val $string \n";
  14.  
  15.                                 $allstring = $allstring.$string;
  16.                                 # Check if $i is n * 1000
  17.                                 if ( !($i % 1000 ))
  18.                                 {
  19.                                                 #write to pre_bcp_option_buffer
  20.                                                 $retval = $_trg_dbh->exec( $allstring );
  21.  
  22.  

When the count reaches 1000, i.e. when i've formatted an insert string of 1000 rows i'm inseritng at one shot, before formatting the insert string i'm checking whether i've any null values in any of the columns. if it is there then i've to assign NULL that is undef (per PERL) to that column in the insert statement/string i'm formatting, coz its a string the undef is assigning ' ' into the db, but not a [NULL]. I expect a [NULL] in the DB so that firther my query can use isnull() to check for NULL values.
Since my program is inserting the ' ' the isnull() couldn't identify the NULL rows.

Thanks
Atif
Oct 5 '07 #3
AtifurRahman
3 New Member
Hi,
I'm using MS SQL Db. My issue is i'm not using insert...values( ? ?) syntax, if I use values along with ?, then undef is working properly. But I'm formatting a string, as shown below

Expand|Select|Wrap|Line Numbers
  1. my $string = "insert into ".$_db_name."..pre_bcp_hierarchy_buffer (client_scheme_code,
  2.                                                                  client_level_1_code,
  3.                                                                  client_level_2_code,
  4.                                                                  client_level_3_code,
  5.                                                                  client_security_id)
  6.                             values (
  7.                                   '". $_data->{'scheme'} ."',
  8.                                   '". $_data->{'level_1'} ."',
  9.                                   '". $_data->{'level_2'} ."',
  10.                                   '". $_data->{'level_3'} ."',
  11.                                   '". $_data->{'client_security_id'}."') \n";
  12.  
  13. print " String Val $string \n";
  14.  
  15.                                 $allstring = $allstring.$string;
  16.                                 # Check if $i is n * 1000
  17.                                 if ( !($i % 1000 ))
  18.                                 {
  19.                                                 #write to pre_bcp_option_buffer
  20.                                                 $retval = $_trg_dbh->exec( $allstring );
  21.  
  22.  

When the count reaches 1000, i.e. when i've formatted an insert string of 1000 rows i'm inseritng at one shot, before formatting the insert string i'm checking whether i've any null values in any of the columns. if it is there then i've to assign NULL that is undef (per PERL) to that column in the insert statement/string i'm formatting, coz its a string the undef is assigning ' ' into the db, but not a [NULL]. I expect a [NULL] in the DB so that firther my query can use isnull() to check for NULL values.
Since my program is inserting the ' ' the isnull() couldn't identify the NULL rows.

Thanks
Atif

Hi,
Thanks very much for you updated code.

Here is the way i've updated my code

code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  my $a = "TEST A";
  3.     my $b = "TEST B";
  4.     my $c = "TEST C";
  5.     my $d = undef;
  6.     my $e = "TEST E";
  7.  
  8.  
  9.         my $string = sprintf("insert into %s..pre_bcp_hierarchy_buffer (client_scheme_code,
  10.                                                                     client_level_1_code,
  11.                                                                     client_level_2_code,
  12.                                                                     client_level_3_code,
  13.                                                                     client_security_id)
  14.                             values (
  15.                                   %s,
  16.                                   %s,
  17.                                   %s,
  18.                                   %s,
  19.                                   %s) \n",
  20.                                   $_db_name,
  21.                                   (defined($a) ? "'" . $a . "'" : 'null'),(defined($b) ? "'" . $b . "'" : 'null'),(defined($c) ? "'" .
  22.  $c . "'" : 'null'),(defined($d) ? "'" . $d . "'" : 'null'),(defined($e) ? "'" . $e . "'" : 'null'));
  23.  
  24. my $retval = $_trg_dbh->exec( $string );
  25.  
Thanks
ATIF
Oct 5 '07 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
Hi,
Thanks very much for you updated code.

Here is the way i've updated my code

code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  my $a = "TEST A";
  3.     my $b = "TEST B";
  4.     my $c = "TEST C";
  5.     my $d = undef;
  6.     my $e = "TEST E";
  7.  
  8.  
  9.         my $string = sprintf("insert into %s..pre_bcp_hierarchy_buffer (client_scheme_code,
  10.                                                                     client_level_1_code,
  11.                                                                     client_level_2_code,
  12.                                                                     client_level_3_code,
  13.                                                                     client_security_id)
  14.                             values (
  15.                                   %s,
  16.                                   %s,
  17.                                   %s,
  18.                                   %s,
  19.                                   %s) \n",
  20.                                   $_db_name,
  21.                                   (defined($a) ? "'" . $a . "'" : 'null'),(defined($b) ? "'" . $b . "'" : 'null'),(defined($c) ? "'" .
  22.  $c . "'" : 'null'),(defined($d) ? "'" . $d . "'" : 'null'),(defined($e) ? "'" . $e . "'" : 'null'));
  23.  
  24. my $retval = $_trg_dbh->exec( $string );
  25.  
Thanks
ATIF
Atif,

When you are posting to TSDN, please use code tags around your code. A series of "---" do not suffice to surround your code as they do not have the same effect.

In the "REPLY GUIDELINES" box to the right of the message window when replying contains the code tags that you should use. They have a beginning and end tag structure, just like html.

I have fixed all of your posts in this thread.

Regards,

Jeff
Oct 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
16975
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
8
6274
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
4
11817
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be...
7
6647
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
3
3426
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
7
20920
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
5
2511
by: Bonzol | last post by:
Hello, PHP n00b here. Using SQL just working off some examples, I have no problem selecting data, but I cant seem to be able to insert. If someone could see where im going wrong <!DOCTYPE...
14
2875
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
0
1856
by: radiodes | last post by:
I am very new to SQL/Access, and only use it very limitedly, so apologies ahead of time. I have a website that inserts data into an access database, using the code below. Basically, I've got...
4
2173
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.