How to overcome truncation error? - sql-server

I am trying to use the import and export wizard to move a small data set from a CSV file to an existing (empty) table. I did Script Table As > Create To, to get all DML for this table. I know the field type of the two fields which are causing problems is varchar(50). I'm getting this error message:
Error 0xc020902a: Data Flow Task 1: The "Source - Reconciliation_dbo_agg_boc_consolidated_csv.Outputs[Flat File Source Output].Columns["ReportScope"]" failed because truncation occurred, and the truncation row disposition on "Source - Reconciliation_dbo_agg_boc_consolidated_csv.Outputs[Flat File Source Output].Columns["ReportScope"]" specifies failure on truncation. A truncation error occurred on the specified object of the specified component.
(SQL Server Import and Export Wizard)
The max length of all characters is 49, so I'm not sure why SQL Server is complaining about truncation. Is there any way to disable this error check and just force it to work? It should work as-is! Thanks everyone.

Is there any way to disable this error check and just force it to
work? It should work as-is! Thanks everyone.
Yes. If you're using the wizard, you can view the table schema before running it, and check the option to ignore truncation.

The max length of all characters is 49, so I'm not sure why SQL Server
is complaining about truncation.
The default datatype of source column may be Text while using import wizard, so change it to varchar(50) using advanced tab of source. Check this link for more details.

For the safe side can you please check column data type in both Source and Destination. If both are not same just declare all your columns as varchar inside table with some maximum length say for example varchar(max) or varchar(500) and see what would be the result.
Change max length of Varchar column:
ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500);
Then the column will default to allowing nulls even if it was originally defined as NOT NULL. i.e. omitting the specification in an ALTER TABLE ... ALTER COLUMN is always treated as.
ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500) NULL;
check column nullable or Not nullable based on requirement just change it.
Use below steps for better understanding How to import a CSV file into a database using SQL Server Management Studio:
While bulk copy and other bulk import options are not available on the SQL servers, you can import a CSV formatted file into your database using SQL Server Management Studio.
First, create a table in your database into which you will import the CSV file. After the table is created:
Log in to your database using SQL Server Management Studio.
Right click the database and select Tasks -> Import Data...
Click the Next > button.
For Data Source, select Flat File Source. Then use the Browse button to select the CSV file. Spend some time configuring the data import before clicking the Next > button.
For Destination, select the correct database provider (e.g. for SQL Server 2012, you can use SQL Server Native Client 11.0). Enter the Server name; check Use SQL Server Authentication, enter the User name, Password, and Database before clicking the Next > button.
In the Select Source Tables and Views window, you can Edit Mappings before clicking the Next > button.
Check Run immediately and click the Next > button.
Click the Finish button to run the package.

Related

After Insert Trigger never fires using Import Wizard

Using Import Wizard I tried importing data into tables tcc_Block and PROJECT_IDENTITY.
These 2 table structures already exist in SQL Server and they are related via the ProjectID columns. PROJECT_IDENTITY has ProjectID as PK and tcc_Block as FK key.
Any time I import tables, the ProjectID in the parent table is created and incremented, but the one in the child table is always NULL.
The trigger never fires!?
ALTER TRIGGER [dbo].[InsertTest]
ON [dbo].[tcc_Block]
AFTER INSERT
AS
BEGIN
DECLARE #proj int;
SELECT #proj = MAX(ProjectID)
FROM PROJECT_IDENTITY;
UPDATE tcc_Block
SET ProjectID = #proj
WHERE ProjectID IS NULL;
END;
GO
Bulk inserts generally do not fire triggers, unless it is explicitly set (see FIRE_TRIGGERS) option. If this package is edited in SSIS (Import Export Wizard generates SSIS packages), you can select the Fire triggers option on the Bulk Insert Task Editor Options page. But Import Export Wizard does not expose a way to set this option. You can save the package to file system and edit it in Visual Studio to enable this option, or you can export the data as flat files and import them with BULK INSERT command, specifying FIRE_TRIGGERS option.
I had a similar issue with a SQL 2008 Export that I saved as a dtsx package in File System. So, I opened the package in my SSMS 2016 and it opens as an xml file. I found the "FastLoadOptions" property: property id="72" name="FastLoadOptions".
Then scrolled to the end of the line where the options are entered and added "FIRE_TRIGGERS":
TABLOCK,CHECK_CONSTRAINTS,FIRE_TRIGGERS /property>.
Saved it and reloaded into my job step (don't forget to check the "Use 32 Bit runtime" on the Execution option tab). And it works great.

How to Insert new records using SSIS? Can I use Exec SQL task or OLE DB Source SQL Command text?

I have created a table called DimInternationalFunction.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DimInternationalFunction]') AND type in (N'U'))
DROP TABLE [DimInternationalFunction]
Go
Create Table DimInternationalFunction
(IntFunctionKey int NOT NULL identity primary key,
SubSubFunctionString char(10),
FunctionCode char(3),
SubFunctionCode char(6),
SubSubFunctionCode char(10),
SubSubFunctionName nvarchar(60),
SubFunctionName nvarchar(60),
FunctionName nvarchar(60))
I have initially inserted records in this table in SSMS.
After inserting the initial records manually in SSMS, now my manager wants me to insert "new records only" using SSIS.
I have tried using this in SSMS and it worked. Either it gives me 0 records inserted or sometimes it gives me 5 records inserted as a result. My manager wants me to do this in SSIS.
I tried using this script inside the OLE DB Source under Data Access Mode: SQL Command and SQL Command text:
insert into DWResourceTask.dbo.DimInternationalFunction
select f.SubSubFunctionString,
f.FunctionCode,
f.SubFunctionCode,
f.SubSubFunctionCode,
f.SubSubFunctionName,
f.SubFunctionName,
f.FunctionName
from ODS_Function F
where FunctionViewCode = 'INT'
and not exists (select * from DWResourceTask.dbo.DimInternationalFunction I
where (f.SubSubFunctionString=i.SubSubFunctionString
and f.FunctionCode=i.FunctionCode
and f.SubFunctionCode=i.SubFunctionCode
and f.SubSubFunctionCode=i.SubSubFunctionCode
and f.SubSubFunctionName=i.SubSubFunctionName
and f.SubFunctionName=i.SubFunctionName
and f.FunctionName=i.FunctionName)
)
The error message that I got after clicking preview is
The component reported the following warnings:
Error at Int Function [International Function Table [33]]: No column information was returned by the SQL command.
Choose OK if you want to continue with the operation.
Choose Cancel if you want to stop the operation.
Is there another component in SSIS that can do this? or can I just use either exec sql task component or ole db source?
I am thinking of using exec sql task connected to a data flow task, inside the data flow task I will put ole db source containing a staging table and do a delete on that or is there any other way to do it. Please help. Thanks in advance.
You could do it with an Execute SQL task.
If you want to do it "the pure SSIS way", you could use a lookup component. Set the "rows with no matching" handler to "Redirect to no match output", and configure the target table as connection. Then use the "No Match Output" only, ignoring the "Match Output". And send the records from the "No Match Output" to the target.
In spite of its name, the "Lookup" component can be used to filter data in many cases.
But I would assume the Execute SQL task would be more efficient for large data sets, keeping all data within the database engine.

ssis dynamically truncate table failed

I want to have a ssis package that would check the folder (and all of its subfolders) and import only files that match the predefined criteria. Depends on what the file name is, it will import into a certain database (all reside in the same sql server).
The process works like this:
1. check the matching files
2. for each matching file, parse the file's name to get information that determines where the file will be imported to
3. truncate the table that the file in the previous step will be loaded to
4. import the file (in Data Flow Task).
So far i got step 1 and 2 working, and the sql statement to truncate table populate as expected. I run this sql statement manually in ssms and it works.
But i can't pass step 3, got an error below:
Error: 0xC002F210 at Truncate table, Execute SQL Task: Executing the
query "set nocount on; truncate table [db1].[DBO].[TBL_B..." failed
with the following error: "Parameter name is unrecognized.". Possible
failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly. Task failed: Truncate table
Attached are the setting of the package. What am i missing?
You can try to specify parameter by giving ParameterName as "0".
And since there will be no resultset to the truncate query, specify ResultSet as None.

SQL Server Default field value

In SQL Server, I have a field which is a bit. I need to default it to 0 on insert of a new record. I thought there was a way for SQL Server to automatically create a constraint without writing code. I thought there was a way to right click on the field and go to properties. I tried but do not see how to default a field.
SQL Server Management Studio: Select the table from the tree, context menu, design table, select the field, in the properties grid at the bottom type a zero (0) in the Default row, save.

Can't import as null value SQL Server 2008 TSV file

I import data from a TSV file with SQL Server 2008.
null is replaced by 0 when I confirm a table after import with integer column.
How to import as null, please Help me!!
Using bcp, -k switch
Using BULK INSERT, use KEEPNULLS
After comment:
Using SSIS "Bulk insert" task, options page, "Keep nulls" = true
This is what the import wizard uses: but you'll have to save and edit it first because I see no option in my SSMS 2005 wizard.
This can be set in the OLE DB Destination editor....there is a 'Keep nulls' option.
Alternative for those using the Import and Export Wizard on SQL Server Express, or anyone who finds themselves too lazy to modify the SSIS package:
Using text editing software before you run the wizard, replace NULLs with a valid value that you know doesn't appear in your dataset (eg. 987654; be sure to do a search first!) and then run the Import Export Wizard normally. If your data contains every single value (maybe bits or tinyints), you'll have some data massaging ahead of you, but it's still possible by using a temporary table with datatypes that can store a greater number of values. Once it's in SQL, use commands like
UPDATE TempTable
SET Column1 = NULL
WHERE Column1 = 987654
to get those NULLs where they belong. If you've used a temporary table, use INSERT INTO or MERGE to get your data into your end table.

Resources