Alter table schema based on another table - sql

In sql if i had 2 databases where each had similar columns but not exactly the same schema , is there a built in way to alter the schema of a table in database2 make it similar to the schema in of a table database1?
considering that the column type and column name and default value are the factors to decide if 2 columns match.

No, there is no built in functionality for doing this, you will either have to do it manually or use a comparison tool such as RedGate SQL Compare or Visual Studio.

Related

SQL how to compare two tables from different servers?

I have a table job_order with column order_no in my store_db. Is there a way or query that I can compare if data from the order_no column matched with the same column but on a different database server?
Note that server1 and server2 has the same database. I would appreciate the help. Thank you!
Different approaches can be used to achieve the table comparison:
Create a Linked Server connection, then create a Stored Procedure that performs a comparison on the Target Server.
A wide option exists to achieve an effective table data comparison.
References:
Table Comparison

How can I get the modified table name(any table in db) and it's modified value without using triggers in SQL Server?

I am creating database snapshot for reporting(creating a common demoralized table from normalized tables), for which I need to enter normalized tables modified values at the same time when the table modify. I want a common solution for this. Same is achievable using triggers but I am looking for some other generic solution which will work with any table change. Is there any other way in SQL server by which I can achieve the same?

Adding transformations in Sybase replication server

We have primary source database as Oracle 11gR2 and target as SAP HANA. We are trying to test SAP - Sybase Replication server for replication from Primary ORACLE to Target HANA.
We need to add extra columns such as RECORD_DATE and LAST_MODIFIED_DATE to HANA tables. Is it possible to add Transformations or extra columns to target tables which are not present in Primary Database.
Best Regards
are you thinking of adding these fields during replication.
or want to merge them after replication. If after replication you want to merge them simply just go to Hana Studio and make an Information view to get the the merged or simply joined data from different tables.
and if that table is not present in the the source System then instead of replication make a Excel flat file and import it into Hana using the Import option on the RHS of hana studio.
and The only way to Alter a table definition in Hana is by Using the Alter Table SQL statement no other Shortcuts. Or just import and make a join.
I'm assuming you want to capture auditing data for records inserted/updated by the Repserver maintenance user (in the HANA database).
While the column default (for inserts; as discussed with Shivam) will work, for updates you've got a few options:
an update trigger on the HANA table [I don't work with HANA so I don't know if this is doable]
defining the update column as a (materialized) computed column, with the associated function being responsible for obtaining the current date/time when other columns in the table are modified [while this is doable in Sybase ASE, I don't know if this is doable in HANA]
(in repserver) create a custom function string for the rs_update function on this table which emulates a standard rs_update function string with the addition of an update of LAST_MODIFIED_DATE = getdate() (replace getdate() with HANA's equivalent of the current date/time) [there are a couple different ways to do this depending on SRS version, what's doable with HANA-specific function strings, and personal preference - a bit much to go into at this point if a custom function string is going to be out of the question or you've already got an acceptable solution]

generate create table from existing table

I have a few large tables in a oracle DB (lots of columns and data types) that I need to move to another oracle database (say from my DEV region to UAT region). Is there anyway I can get sql developer or sql plus to output a create table statement that is exactly the structure of the existing table?
thanks
If you want to get it through SQL statement then you can try the below query
SELECT dbms_metadata.get_ddl('TABLE', 'Your_Table_Name') FROM dual;
See DBMS_METADATA for more information.
You can generate the script using Toad software as well (In case you have Toad installed)
If you are making use of the SQL Developer you can right click the table that you want to generate a script for.
From there select Quick DDL and then click on Save To File. This will then generate the create table script to an external sql file. You can do this for multiple tables as well by selecting more than one table at a time.
Hope this helps!!
Another option is to create a database link between the two schemas and then do a
create table table_name AS SELECT * from table_name#original_server

Generate Scripts of Certain Columns from a table in SQL Server 2008 management studio

I have a SQL Server 2008 database. This database has a table with 6 columns in it. I want to generate the insert scripts associated with the data only. I know how to do this part with SQL Server management studio via the "Advanced" dialog in the "Generate Scripts..." wizard. My challenge is, I only want to script 5 of the 6 columns. How do I do this?
Check out the FREE SSMS Tools Pack v2.0 - it has among a lot of other things a feature to generate INSERT statements for data in your tables, and yes - you can pick for which columns that INSERT statement should be:
SELECT Column1,Column2,Column3,Column4,Column5
INTO new table
FROM your Existing table;
This will create a new table with the columns that you specified, then the data will be inserted in the new created table. Then script the new table that have only the five columns that you want.
I don't think there's a way to do this. After you generate the full table script, you can do one of a few things.
If you want to preserve the actual field (keep it there), then just do something like:
update yourTable
set yourColumnThatYouDidntWantDataFor = null
Or, if you didn't want that field to be in the table at all, you can just drop it like so:
alter table yourTable
drop column yourColumnThatYouNoLongerWant