VBA empty string for insert into SQL Server - sql-server

Here is the situation:
There's a SQL Server database with a Microsoft Access front end. There is a table in the SQL Server database with a column called PackID which is defined as VARCHAR(6) NOT NULL (it has to be NOT NULL because it's part of a composite primary key for this table). Empty strings are legitimate in the PackID column. In fact, they occur quite often. When the user enters data in the UI for this table, and tabs through the PackID field, an error message appears: "Cannot insert the value NULL into column 'PackID'; column does not allow nulls. INSERT fails."
Things that have been tried that do not work:
adding a default constraint on this column in the SQL Server database with a value of '' - apparently the default is only used if the column is omitted from the INSERT statement
setting the Default Value of the PackID field in Access to "" - it behaves as though "" is a NULL
calling the following VBA code when user moves off row in UI (Lost Focus event)
If IsNull(PackID.Value) Then
PackID.Value = ""
End If
Does anyone know how to force an empty string in Access so it's interpreted as an empty string for SQL Server and not a NULL?

Setting the Default Value in the text box Properties for PackID in Access to this
=" "
worked. The space between the double quotes is very important. Leaving the space out causes the insert to fail. In SQL Server LEN(PackID) returns 0. For instance:
SELECT LEN(''), LEN(' ');
both return 0. It appears as though SQL Server treats both of these as empty strings.

An alternative solution to this problem (no Default Value in text box Properties for PackID).
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(PackID.Value) Or PackID.Value = "" Then
PackID.Value = " "
End If
End Sub
This still uses the concept of passing SQL Server " " (read this as quote space quote) as the field's value. On the UI side, this is an alternative to setting a default value for the field.

Related

MS Access + SQL Server front end concatenation problem (#NAME)

When I link an SQL Server table to MS Access, I have an issue with concatenation of the SQL Server table/view columns.
I'm used to be doing this in Access: =[FirstName] & " " & [LastName]
However, when I do this with data linked from SQL Server, I get #NAME in the text field every time.
I tried to create a local dummy table in Access and the concatenation works without issues.
I also tried to type the concatenation in SQL Server format: =[FirstName] + ' ' + [LastName], without success.
My current workaround is to create all concatenations directly in the SQL View definitions, but this is quite limiting and I would like to be able to do concatenations directly in Access.
Is it possible?
This looks like a new bug in Access
I linked to a SQL table and added 2 text boxes to a form one an integer and one text
on the integer one =[field] & "b" works on the text field it fails.
even just =[textfieldname] throws an error
A workaround i found is to add firstname and lastname text boxes and rename them txtfirstname and txtlastname then add a third text box and give the datasource property = [txtfirstname] & " " & [txtlastname]
irritating as this used to work

Selecting from a column with Ampersand(&)

The data type of the column is nvarchar. I can insert to the column properly using parameters. I see the ampersand character in the column when I view the table in sql server management studio.
My problem is when i'm selecting from the column in my application the ampersand character disappears.
Once I select the column I assign the value to a label like, label1.Text = reader("column")
The column in my table has a value of "Foo&Bar" but in my application it just shows as "FooBar".
Looks like the ampersand was turning the next letter to a shortcut.
I just did a replace on the ampersand to 2 ampersands so it escapes.
label1.Text = reader("column").ToString().Replace("&", "&&")
It is a property of the Label called UseMnemonic, set it to False to show the text & properly

Cannot insert default value into SQL database from Win Forms project

I have a MS SQL database.
On one of my fields, LotNumber I set the default value to be [dbo].CalculateJulianDate which is a scalar-valued function that takes the current date and creates a Julian date formatted lot number.
When I insert values into my table using SSMS, the value is populated correctly replacing the empty value with the LotNumber.
I am trying to create a visual basic winforms front end for the database in visual studio 2010. I set my datasource, dragged a details view onto a form and I can see the data, make edits to the data, and delete data. However, when I try to insert new data it fills the LotNumber field with NULL.
I do not understand why MS SQL doesn't use the default value rather than the NULL value when data is entered from within my winForms application. I was under the impression that the point of setting up a default value in the SQL schema was to replace NULL values with the default value.
The code for the insert is auto-generated and I can't seem to find it in my application. The only code visible is:
Private Sub Dt_InventoryBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles Dt_InventoryBindingNavigatorSaveItem.Click
Me.Validate()
Me.Dt_InventoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CSRPRDDataSet)
End Sub
Does anyone know why SQL Server doesn't insert the default value like it's supposed to? How do I get the default value to populate from within my application?
The default value is used in SQL Server when you don't set that field value at all. If you set it to NULL then it stays set to NULL. If you remove that field from your INSERT statement then the default value will be used.
To make a custom INSERT query on the TableAdapter, right-click on your Table Adapter, choose Add Query and the Query Wizard will appear. Select "Use SQL Statements" and select "INSERT" for the query type.

SQL Server Nullable Column

I have a SQL Server table with a few columns of type varchar which are nullable. When an aspx page posts data if the textbox is empty, the database table column is updated to an empty string.
To maintain the null value rather han have it replaced by an empty string, I can either have the logic to change the empty string to System.DBnull in the middle tier c# code or do so in the stored procedure.
Are there other better ways to handle this situation?
you can use a trigger or do it in the proc, you can use the NULLIF function
Example
DECLARE #d VARCHAR(20)
SELECT #d = ''
SELECT NULLIF(#d,'')
I would probably put it near the code that makes a decision based on the distinction between an empty string and a null string. If there is no difference between their meaning or the resulting behavior of the application, I would suggest making the field non-nullable and sticking with an empty string.

Oracle considers empty strings to be NULL while SQL Server does not - how is this best handled?

I have to write a component that re-creates SQL Server tables (structure and data) in an Oracle database. This component also has to take new data entered into the Oracle database and copy it back into SQL Server.
Translating the data types from SQL Server to Oracle is not a problem. However, a critical difference between Oracle and SQL Server is causing a major headache. SQL Server considers a blank string ("") to be different from a NULL value, so a char column can be defined as NOT NULL and yet still include blank strings in the data.
Oracle considers a blank string to be the same as a NULL value, so if a char column is defined as NOT NULL, you cannot insert a blank string. This is causing my component to break whenever a NOT NULL char column contains a blank string in the original SQL Server data.
So far my solution has been to not use NOT NULL in any of my mirror Oracle table definitions, but I need a more robust solution. This has to be a code solution, so the answer can't be "use so-and-so's SQL2Oracle product".
How would you solve this problem?
Edit: here is the only solution I've come up with so far, and it may help to illustrate the problem. Because Oracle doesn't allow "" in a NOT NULL column, my component could intercept any such value coming from SQL Server and replace it with "#" (just for example).
When I add a new record to my Oracle table, my code has to write "#" if I really want to insert a "", and when my code copies the new row back to SQL Server, it has to intercept the "#" and instead write "".
I'm hoping there's a more elegant way.
Edit 2: Is it possible that there's a simpler solution, like some setting in Oracle that gets it to treat blank strings the same as all the other major database? And would this setting also be available in Oracle Lite?
I don't see an easy solution for this.
Maybe you can store your values as one or more blanks -> ' ', which aren't NULLS in Oracle, or keep track of this special case through extra fields/tables, and an adapter layer.
My typical solution would be to add a constraint in SQL Server forcing all string values in the affected columns to have a length greater than 0:
CREATE TABLE Example (StringColumn VARCHAR(10) NOT NULL)
ALTER TABLE Example
ADD CONSTRAINT CK_Example_StringColumn CHECK (LEN(StringColumn) > 0)
However, as you have stated, you have no control over the SQL Database. As such you really have four choices (as I see it):
Treat empty string values as invalid, skip those records, alert an operator and log the records in some manner that makes it easy to manually correct / re-enter.
Convert empty string values to spaces.
Convert empty string values to a code (i.e. "LEGACY" or "EMPTY").
Rollback transfers that encounter empty string values in these columns, then put pressure on the SQL Server database owner to correct their data.
Number four would be my preference, but isn't always possible. The action you take will really depend on what the oracle users need. Ultimately, if nothing can be done about the SQL database, I would explain the issue to the oracle business system owners, explain the options and consequences and make them make the decision :)
NOTE: I believe in this case SQL Server actually exhibits the "correct" behaviour.
Do you have to permit empty strings in the SQL Server system? If you can add a constraint to the SQL Server system that disallows empty strings, that is probably the easiest solution.
Its nasty and could have unexpected side effects.. but you could just insert "chr(0)" rather than ''.
drop table x
drop table x succeeded.
create table x ( id number, my_varchar varchar2(10))
create table succeeded.
insert into x values (1, chr(0))
1 rows inserted
insert into x values (2, null)
1 rows inserted
select id,length(my_varchar) from x
ID LENGTH(MY_VARCHAR)
---------------------- ----------------------
1 1
2
2 rows selected
select * from x where my_varchar is not null
ID MY_VARCHAR
---------------------- ----------
1
NOT NULL is a database constraint used to stop putting invalid data into your database. This is not serving any purpose in your Oracle database and so I would not have it.
I think you should just continue to allow NULLS in any Oracle column that mirrors a SqlServer column that is known to contain empty strings.
If there is a logical difference in the SqlServer database between NULL and empty string, then you would need something extra to model this difference in Oracle.
I'd go with an additional column on the oracle side. Have your column allow nulls and have a second column that identifies whether the SQL-Server side should get a null-value or empty-string for the row.
For those that think a Null and an empty string should be considered the same. A null has a different meaning from an empty string. It captures the difference between 'undefined' and 'known to be blank'. As an example a record may have been automatically created, but never validated by user input, and thus receive a 'null' in the expectation that when a user validates it, it will be set to be empty. Practically we may not want to trigger logic on a null but may want to on an empty string. This is analogous to the case for a 3 state checkbox of Yes/No/Undefined.
Both SQL and Oracle have not got it entirely correct. A blank should not satisfy a 'not null' constraint, and there is a need for an empty string to be treated differently than a null is treated.
If you are migrating data you might have to substitute a space for an empty string. Not very elegant, but workable. This is a nasty "feature" of Oracle.
I've written an explanation on how Oracle handles null values on my blog a while ago. Check it here: http://www.psinke.nl/blog/hello-world/ and let me know if you have any more questions.
If you have data from a source with empty values and you must convert to an Oracle database where columns are NOT NULL, there are 2 things you can do:
remove the not null constraint from the Oracle column
Check for each individual column if it's acceptable to place a ' ' or 0 or dummy date in the column in order to be able to save your data.
Well, main point I'd consider is absence of tasks when some field can be null, the same field can be empty string and business logic requires to distinguish these values. So I'd make this logic:
check MSSQL if column has NOT NULL constraint
check MSSQL if column has CHECK(column <> '') or similar constraint
If both are true, make Oracle column NOT NULL. If any one is true, make Oracle column NULL. If none is true, raise INVALID DESIGN exception (or maybe ignore it, if it's acceptable by this application).
When sending data from MSSQL to Oracle, just do nothing special, all data would be transferred right. When retrieving data to MSSQL, any not-null data should be sent as is. For null strings you should decide whether it should be inserted as null or as empty string. To do this you should check table design again (or remember previous result) and see if it has NOT NULL constraint. If has - use empty string, if has not - use NULL. Simple and clever.
Sometimes, if you work with unknown and unpredictable application, you cannot check for existence of {not empty string} constraint because of various forms of it. If so, you can either use simplified logic (make Oracle columns always nullable) or check whether you can insert empty string into MSSQL table without error.
Although, for the most part, I agree with most of the other responses (not going to get into an argument about any I disagree with - not the place for that :) )
I do notice that OP mentioned the following:
"Oracle considers a blank string to be the same as a NULL value, so if a char column is defined as NOT NULL, you cannot insert a blank string."
Specifically calling out CHAR, and not VARCHAR2.
Hence, talking about an "empty string" of length 0 (ie '' ) is moot.
If he's declared the CHAR as, for example, CHAR(5), then just add a space to the empty string coming in, Oracle's going to pad it anyway. You'll end up with a 5 space string.
Now, if OP meant VARCHAR2, well yeah, that's a whole other beast, and yeah, the difference between empty string and NULL becomes relevant.
SQL> drop table junk;
Table dropped.
SQL>
SQL> create table junk ( c1 char(5) not null );
Table created.
SQL>
SQL> insert into junk values ( 'hi' );
1 row created.
SQL>
SQL> insert into junk values ( ' ' );
1 row created.
SQL>
SQL> insert into junk values ( '' );
insert into junk values ( '' )
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("GREGS"."JUNK"."C1")
SQL>
SQL> insert into junk values ( rpad('', 5, ' ') );
insert into junk values ( rpad('', 5, ' ') )
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("GREGS"."JUNK"."C1")
SQL>
SQL> declare
2 lv_in varchar2(5) := '';
3 begin
4 insert into junk values ( rpad(lv_in||' ', 5) );
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>

Resources