After Insert Trigger never fires using Import Wizard - sql-server

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.

Related

How to overcome truncation error?

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.

Does Data Import Replace Existing Data in MySQL Workbench?

I'm working with a database in a development environment in MySQL Workbench. I have everything ready to go and need to move it to a prod database. I've exported it to a sql file but I'm unsure if I'm approaching the import the correct way.
If I use the "Data Import/Restore" feature, select my SQL file, and import, will it replace the existing data in the database (what I want to happen) or will it add new records to each table for the new data?
The schema is the same in each database. I just need to replace the old data in the prod database with the new data from dev.
Thanks for your help
That depends on how your export-file looks. Just open it in a text editor and read over the statements in your export-file.
By default it should contain statements like:
CREATE TABLE IF NOT EXISTS `customer` (
`CUSTOMER_ID` int(11) NOT NULL,
`CUSTOMER_NM` varchar(100) DEFAULT ''
) EN
and right after it the data of this table:
INSERT INTO `customer` (`CUSTOMER_ID`, `CUSTOMER_NM`) VALUES
(0, 'Dummy Customer');
(1, 'Dummy Two');
Since your tables already exist in your PROD-Environment it will not delete, create or replace them (Note the CREATE TABLE IF NOT EXISTS-Statement). The INSERT-Stamement will be executed (there is no condition which says it shouldn't).
So after importing your file you will have your previous PROD-Data in your database + the imported DEV-Data your your DEV-Environment.
On the other hand it could contain a statement like:
DROP TABLE IF EXISTS `customer`
And right after it the CREATE-Statement followed by some INSERT-Statements. In this case your whole PROD-Database will be replaced by the DEV-Database as you want it to.
You can use MySQL specific replace statement to achieve this goal. check out this link

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.

How can i import indexes into ms SQL Server?

I created a new databases , and added tables using the import data wizard. But the wizard din't create the indexed and constraint's on the table. How can I Import indexes?
If your source is also SQL Server, you should be able to run Tasks -> Generate Scripts and select "Script Indexes" in the list of options for the old database and execute the script on the new database with maybe a change in database name.
Just manually add indexes to your table
Here is an example from MSDN:
This example creates an index on the au_id column of the authors table.
SET NOCOUNT OFF
USE pubs
IF EXISTS (SELECT name FROM sysindexes
WHERE name = 'au_id_ind')
DROP INDEX authors.au_id_ind
GO
USE pubs
CREATE INDEX au_id_ind
ON authors (au_id)
GO
The other way you can do this is open the table in design mode management studio, highlight the field you want to index and look at the options at the top. One of them is for indexes and you can simply manually add it and give it a name right in management studio.

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