Script out insert script with dependency in SQL server - sql

we want Script out insert script with dependency in multiple table.
For example – TABLEA is Master table- here I want to insert 1 record from old version of table script out earlier (SSMS-TASK-Generate script), but due to dependency in another table it will not work.
Regards,
Manish

If you have the real foreign key between your tables, SMSS Generate Scripts Task takes care of the dependencies and the master table insert script will be generated before the detail tables.

Related

SCD0 implementation using staging and taget table

I'm looking for a one transaction query that would allow me to truncate a target table first and insert everything from staging table to target table. I tried using merge into, but that requires using keys and is not very generic. Any thoughts?

SQL Server overwrite unique constraint violation

I have two files which I am importing via Node JS to SQL Server. The table has unique key for equity instrument identifier (ISIN)
data1.csv and data2.csv
I first import data1.csv each row is inserted to the database. After this I import data2.csv (the values are again inserted to database) which may contain the same ISIN, but it's related values are higher priority than the first file (there are not many of these ISINs 5 out of 1000 or so).
What can I do with SQL server to overwrite the values if the unique constraint is violated? I understand that there is an option to upload data2.csv first, however there are some external constrains that do not allow me to do that.
Please tell me if additional information is required
I would recommend staging process to do this:
1. create a staging table with similar schema as your target table
2. Before loading delete all rows from staging table (you can use truncate)
3. Upload the file to the staging table
4. Load your data into final table - here you can use some logic to only insert new rows and update existing rows. Merge command will be useful in scenario like this.
Repeat steps 2 to 4 for each source table.

SSIS package check if record exist then update else insert

I am creating this SSIS import package for about 10 tables , I am still new to this so I really appreciate any help I can get.
I need to compare my Excel source to this ~10 tables to see if any record exists ,if it exists then update it or else insert it. I am struggling on how to check on various tables where they all have auto-incremented PK. If one record doesn't exist how can I insert it and make sure the other tables have the foreign keys(auto-incremented primary key of tables) updated as well.Meaning the relationship of each record that have been divided into so many tables are tact.
My plan for the package:
Excel source
Look up transformer
Data conversion transformer
derived column transformer
multicast
OLE DB destination
Please advise on how I should go about, and the order I should follow for my transformers.
hmm ok firstly get the excel source into an sql staging table first (truncate before loading) then you could consider using the sql merge statement via an execute sql task to merge the data into the end tables. This will allow you to insert if the record dosent exist. You may need to lookup the foreign keys before running the merge. Are you able to post details of the 10 tables and import csv?

How to update a table copy in another database

I have two identical databases - one for development (DEV) and one for production (PROD) (both SQL Server 2008).
I have updated the contents of a certain table in DEV and now I want to sync the corresponding table in PROD.
I have not changed the table schema, only some the data inside the table (I have both changed existing rows and added some new rows).
How can I easily transfer the changes in DEV to the corresponding table in PROD?
Note, that the values in the automatic identity column mgiht not match exactly between the two tables. However, I know that I have only made changes to rows having the same value in another column.
Martin
If you don't want to use the replication, you can Create update, Insert and delete trigger in DEV database and update PROD by trigger.
or you can create view of DEV database table on the PROD database.

Overwrite SQL table within same server

I found this link that could be the solution to my question, but I need some clarification.
I have two DBs on one server, the one DB is a backup. I have a broken table and want to replace the table values/records with the table from the backup DB. Can I use the method from the link above, or is it only if the destination table is empty that I can use the "INSERT INTO destination"?
My goal is to overwrite the table with the backup values.
Since your goal is to replace the entire table with the table from the backup database, you can TRUNCATE the target table and then reload from the backup table using INSERT...SELECT.
You will need to be mindful of foreign key constraints. TRUNCATE is not allowed if FKs reference the table so you will need to use DELETE to empty the table instead.