×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to avoid SQLEXEC() function being posting NULL value into sql server table

How to avoid SQLEXEC() function being posting NULL value into sql server table

How to avoid SQLEXEC() function being posting NULL value into sql server table

(OP)
Hi,

my sql table has 10 columns the first five being updated when with following function

SQLEXEC(lnHandle,"INSERT INTO table (column1, column2,..., column5) VALUES (?val1, ?val2...?val5)")

when FoxPro execute the above function the rest of the columns (column6 to column10) getting NULL automatically. is there any way I can avoid the NULL is being posted.

Regard MSIDDEEK

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

If you have control of the database design on the SQL database, you could set a non NULL default for each column.

Otherwise you could code a non NULL value into the INSERT INTO for each column

Regards

Griff
Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

If the remote database is VFP based, you could make the column NOT NULL when you define it as well.

Regards

Griff
Keep Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

I second Griff, specifically you have to define default values in the MSSQL table. If there are no defaults defined and a field is nullable null is taken as value, unlike VFP tables, where a missing default value is filled with a data type spcific empty value and you explicitly need to set default to NULL.

But actually, MSSQL is ANSI compliant with that behavior and VFP is not.

But - again but - VFP does not fill in NULLs, just try this without any Foxpro involvement in MS SQL Management studio:

CODE

INSERT INTO table (column1, column2,..., column5) VALUES (1, 2, ..., 5) 

You'll see the other columns become NULL, also if nothing is specified for them.

So it's not VFP forcing in NULLs, it's the columns nullability that does that.

Same goes for MySQL, this is the standard of SQL. If you want some column to have a default value, you have to set that in the table definition. If you set a column not nullable and specify no default value it becomes even worse and INSERT statements without specifying values for the columns fail, because still NULL is tried to be inserted even though the SQL engine knows these columns can't be null and could use empty strings for many character data types and 0 for numeric data types, FALSE for bit fields and so on, but then it stops to have a logical value for date or datetimes, there is no empty date/datetime in any database besides VFP.

And the rest of the datasbase world here has the normal and logical concept about missing values, that is the meaning of NULL.

In essence, your two options are 1. to define default values in the table definition or 2. to specify them specifically in the insert statement. Whether to disallow null in the columns is a separate decision, as it doesn't only depend on whether a record only optionally has a value in those columns at the initial "birth" state of the life cycle of such a record, but also whether that columns can become null later,w whether that attribute can be purged without deleting the whole record.

So, you can have any combination of nullable and non-nullable fields with or without a default value. A bit off topic, as your columns obviously are nullable: Non-nullable fields without default values obviously are forcing clients to specify all values and are the least comfortable, so you're a bit luckier. But it's also a valid concept, it makes all such fields mandatory and if they are it would be an error to allow nulls and if there is no sensible default it also would be an error to define one just to prevent errors. If you have legitimate demands you have legitimate demands of clients to specify all columns values.

The combinations you can do are not designed to guarantee error-free table usage.

Ok, enough food for thought, I think. But if you're accusing VFP to force in NULLs you're barking up the wrong tree, you can turn on SQL PRofile and see for yourself what statements arrive. It's SQL Server who fills in the NULLs for unmentioned columnns when they have no default.

A simple examplem fully MSSQL contained without any Foxpro influence:

CODE

Declare @testtable as table (id int identity(1,1), somecolumn char(10) not null, othercolumn char(10) null)

Insert into @testtable (somecolumn) values ('Hello') -- works and inserts 'Hello' into somecolumn and nothing into othercolumn, which is NULL, therefore.

Select * from  @testtable

Insert into @testtable (othercolumn) values ('World') -- errors as soomecolumn isn't nullable (not null) and so some value has to be specified

Select * from  @testtable -- shows the table therefore is unchanged and only has the first record 

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

(OP)
Thanks Olaf, Griff
For your valuable comments and advice. Olaf I got your point it's SQL who is the culprit.
Is there any way when I recall the SQL records (ie SELECT * FROM table) these NULL words can be eliminated, so the result cursor will not have these NULL words on it?.

Thanks MSIDDEEK

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

(OP)
because the rest of the five columns are filled by another user.

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

Well, you can either select COALESCE(column6,'') to change the nulls into empty strings or you get the NULLS as they are and SET NULLDISPLAY TO ''.

Also, see the discussion or rather the lengthy monologue I just had recently in thread184-1794142: JOIN without .NULL..

Which makes me wonder why such duplicated questions always occur so often, but that's just a side note, ignore it.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

(OP)
Thank you Olaf for your valuable thoughts, I did SET NULLDISPLAY TO '', it is working fine my only concern is will it have an impact on numeric or date field?? I mean will it change a numeric field into char field, I checked on the resulting cursor but it has not changed the numeric field or date fields they are as it is.

I appreciate your dedication on this forum.

Regards MSiddeek

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

NULL is the non-existing value of any type, but the from the inverted perspective that means there are typed null values. NULL itself is not any type, but a NULL in an int field is an int NULL. If you query COALESCE(column6,'') you turn it into char, even if column6 would be other types. For staying with the type there is ISNULL(), if you read the help topic on COALESCE this also is pointed out as a difference.

Anyway doing all these selects I get 0 as result value:

CODE

select ISNULL(CAST(NULL AS integer),0)

select ISNULL(CAST(NULL AS integer),'')

select COALESCE(CAST(NULL AS integer),'') 

So, in the end you, better stay with your simpler typesafe SELECT *, the VFP type still will be the sql server data type, just set to NULL as "value" or non-value.

If you then set the values in the VFP cursor you need an updatable cursor to write those values back to MSSQL, so from that perspective you're not having anything from staying with the same type unless you're going for updatable SPT cursors, which is where I'd suggest using cursor adapters with cursor schema clearly defining the result cursor data type structure.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: How to avoid SQLEXEC() function being posting NULL value into sql server table

(OP)
Thanks Olaf,

I will follow as you advice, once again thank you very much for your feed back.

Regards MSiddeek

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close