SSIS default value is not being set when source value in null - ssis

Problem:
OLEDB source has null value in phone.
Destination table has Phone NOT NULL DEFAULT 1234567.
OLEDB destination has Keep Nulls unchecked
According to what I read here https://msdn.microsoft.com/en-us/library/ms187887(v=sql.110).aspx
The default value for the column should be inserted if incoming value is NULL.
But it's not happening. And I don't want to do any transformation in SSIS. Can someone help?

In your Data Flow Task, in OLE_DB source, set the data access mode to 'SQL command' and write out a select statement as below
SELECT Column_A
,Column_B
,ISNULL(Phone, 1234567)
,Column_C
,Column_D etc.
FROM Source_Table

you can write CASE statement in SQL command in Data Flow Task:
select
CASE
when PhoneNumber IS NUll then '1234567' else table_name.PhoneNumber END as PhoneNumber,
from table table_name

I believe the answer is that for Keep Nulls option to work the column has to be missing from the data source.
create table #tmpdel
(
a INT NOT NULL default 0,
b INT NOT NULL default 0
)
Insert into #tmpdel(a) values(1) - WORKS
Insert into #tmpdel(a, b) values(1, null) - FAILS

Related

INSERT INTO ... SELECT if destination column has a generated column

Have some tables:
CREATE TABLE `asource` (
`id` int(10) unsigned NOT NULL DEFAULT '0'
);
CREATE TABLE `adestination` (
`id` int(10) unsigned NOT NULL DEFAULT '0',
`generated` tinyint(1) GENERATED ALWAYS AS (id = 2) STORED NOT NULL
);
I copy a row from asource to adestination:
INSERT INTO adestination
SELECT asource.*
FROM asource;
The above generates an error:
Error Code: 1136. Column count doesn't match value count at row 1
Ok, quite strange to require me to mention generated query. But ok, I add that column to the query:
INSERT INTO adestination
SELECT asource.*, NULL AS `generated`
FROM asource;
This has worked fine in 5.7.10. However, it generates an error in 5.7.11 (due to a fix:
Error Code: 3105. The value specified for generated column 'generated' in table 'adestination' is not allowed.
Ok, next try:
INSERT INTO adestination
SELECT asource.*, 1 AS `generated`
FROM asource;
But still the same error. I have tried 0, TRUE, FALSE but the error persists.
The DEFAULT value which is stated as the only allowed value (specs or docs). However, the following generates a syntax error (DEFAULT is not supported there):
INSERT INTO adestination
SELECT asource.*, DEFAULT AS `generated`
FROM asource;
So, how can I copy a row from one table to another using INSERT INTO ... SELECT if the destination table adds some columns where some of them are GENERATED?
The code calling this query is generic and has no knowledge what columns that particular tables have. It just knows which extra columns the destination table has. The source table is a live table, the destination table is a historical version of the source table. It has few columns extra like user id made the change, what type of the change it is (insert, update, delete) when etc.
Sadly this is just how MySQL works now to "conform to SQL standards".
The only value that the generated column can accept in an update, insert, etc. is DEFAULT, or the other option is to omit the column altogether.
My poor mans work around for these are to just disable the generated column while I'm working with the data (like for importing a dump) and then go back and add the generated column expression afterwards.
You must declare the columns
Insert into adestination (id, generated)
select id, 1
from asource;
It is best practice to list out the columns, and use null as field1 for the auto incremented id field.
INSERT INTO adestination
(id,
field1,
field2)
SELECT
null AS generated,
asource.field1,
asource.field2
FROM asource;

issue with NOT NULL in mysql

I am using wamp in win 7.
In my database, I have one table be_users, two fields: username and email, both of them are set NOT NULL.
But why I still can insert empty value and null value into field: email, see below image:
Actually, you are inserting text and they are not NULL. The text NULL is very different from NULL. You can never violate the rule if the field is set to NOT NULL.
Try executing this statement,
INSERT INTO tableName VALUES (null, null) -- will fail
it will surely fail because those are NULL. But this statement below will surely work because you are inserting string.
INSERT INTO tableName VALUES ('', 'null') -- will work
An empty value does not equal NULL, and it looks like you are inserting 'null' not NULL into the database, so you are ending up with a string 'null' not a NULL value.
Dont send NULL enclosed with quotes 'NULL' or "NULL" its getting treated as text value.
the issue is due to the data type used for thease two field. "text" you can change this to varchar(255) to use NULL. There are some issue with NULL and data type text.
Null is a keyword. If you will enter null directly then it will show you error.
Example-
insert into tablename(fieldname) values(null);
This above line will generate error(if you have mentioned not null).
I think you have enter something else. Please check again the table structure and enter new data.

Coexistence of two columns SQL

I am new in SQL (Yes, just 1 week) and I am learning this on my own. So, I really need help in solving this problem:
How do I set a column (in a table) to be NULL when another column is also NULL?
Example:
A person table with Name and Gender as its columns. In order to have a gender, the 'Name' column must also have an entry. If the Name is NULL, then do not allow insertion under Gender.
Oh and yes, this is related to my homework question but I have modified the example above to be different. I don't want answers (although they're welcomed, but I want to learn), I just want guidance. Thanks in advance.
SQL supports a feature called "check constraints" so that you can declare an expression that must be true or else the row is invalid and can't be inserted. I.e. CHECK Name IS NULL AND Gender IS NULL OR Name IS NOT NULL AND Gender IS NOT NULL.
But unfortunately, MySQL doesn't support check constraints. Many features of SQL are optional from the standard's perspective and not all RDBMS implementations support all features. There are even a few features of SQL that none of the brands of RDBMS support.
The only way to do this in MySQL is to use a trigger. Before insert or update, if Name is null, then force the Gender to be null as well.
If Name is not null and Gender is null, then I assume the row is invalid, so you'll have to use a signal to abort the insert or update.
A stored procedure written to insert values into table can help you with this. Please see below example code:
CREATE PROCEDURE InsertIntoTable
(
#NAME NVARCHAR(20) = NULL,
#GENDER NVARCHAR(20) = NULL
)
AS
BEGIN
DECLARE #NewGender NVARCHAR(20)
IF(#NAME IS NULL)
SET #NewGender = NULL
ELSE
SET #NewGender = #GENDER
INSERT INTO Table1 VALUES(#NAME, #NewGender)
END

Setting conditionally required fields in SQL Server 2008

I need to make conditional requirements for fields. I'm not sure how this looks in SQL. I'm using Management Studio with SQL Server 2008. Essentially I would like a field to become required only when another field has data entered into it. I researched triggers, and I think that's what I need, but I'm not sure what type (DDL, DML, ect.).
For example:
When the user enters a time they must also enter a date, but if no time is entered then no date is required. SQL would send an error and not allow the user to complete the record without entering a date while the time field is filled.
Thanks!
You can use a check constraint.
create table YourTable
(
ID int identity primary key,
DateCol date,
TimeCol time,
constraint ch_DateTime check(
DateCol is not null or
TimeCol is null
)
)
Test with this:
-- null in both columns
insert into YourTable default values
-- values in both columns
insert into YourTable(DateCol, TimeCol) values(getdate(), getdate())
-- value only in DateCol
insert into YourTable(DateCol) values(getdate())
-- value only in TimeCol failes
insert into YourTable(TimeCol) values(GetDate())

SSIS Inserts not inserting the computed columns

I am using SSIS to insert a Excel file into a Sql Server Table. I believe it uses the Bulk insert, and as a result it doesn't insert into the 'CreationDate' and the 'ModificationDate' columns (both of which are computed columns with getdate() as the default).
Is there a way to get around this problem?
Also, just to be clear - both these date columns are not a part of excel. Here is the exact scenario:
My excel has two columns - code and description. My SQL Server table has 4 columns Code, Description, CreationDate, ModificationDate.
So, when the SSIS copies the data, it copies Code and Description, but the CreationDate and ModificationDate (which are SQL Server Computed Columns) are both empty.
You should use a normal column with a default constraint if you want to log creation
A computed column defined as GETDATE() will change every time you query it.
It is also impossible for a computed column to not be populated
So, assuming you mean "normal column with default", then you need stop sending NULL from SSIS which overrides the default
This is all demonstrated here:
CREATE TABLE #foo (
bar int NOT NULL,
testCol1Null datetime NULL DEFAULT GETDATE(),
testCol1NotNull datetime NOT NULL DEFAULT GETDATE(),
testCol2 AS GETDATE()
);
INSERT #foo (bar, testCol1Null) VALUES (1, NULL);
SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;
WAITFOR DELAY '00:00:00.100';
SELECT * FROM #foo;
DROP TABLE #foo;
Assuming you are using the Bulk Insert Task in SSIS, then you need to set "Keep nulls = off/unchecked" in the options page
You should have a default constraint on the column(s) that specifies get
col1 datetime default getdate()
There should also be an option for the bulk insert KEEPNULLS which should be turned off.
From Bulk Insert on MSDN:
Specifies that empty columns should retain a null value during the bulk-import operation, instead of having any default values for the
columns inserted. For more information, see Keeping Nulls or Using
Default Values During Bulk Import.
KEEPNULLS is also documented: http://msdn.microsoft.com/en-us/library/ms187887.aspx
Put in a Derived Column in your dataflow and populate the two missing columns with the values you want.
The value on a computed column doesn't physically exists on the database, it is calculated every time SQL Server needs to access it, that's why you can't inform a value to it on a insert.
What you need is a default column, which is a column that has a default value that's inserted if you don't inform any other value.
CreationDate datetime default getdate()
ModificationDate datetime default getdate()