Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to compare/transfer table schema of all tables of one database(lets say Database2) to another(lets say Database1) and i want this to happen through a stored procedure, could you please help me in it?

For eg. if database 1 has a table employee in which empname column is varchar(3) and database 2 has a table employee in which empname column is varchar(5), then after running the script the empname column of database1 table employee should also become varchar(5). I have written the below stored procedure for the same but dont know whether this will work or not

What I have tried:

SQL
CREATE PROCEDURE [dbo].[CompareSchemaScript] ( @ix_sys_error int OUTPUT ) AS BEGIN TRY DECLARE @LogError INT, @PrimaryKey INT,@ErrorMessage nvarchar(2000) EXEC log_ins @LogError, @PrimaryKey,5,1,NULL,1,'Script Started' ALTER SCHEMA Database1.tables TRANSFER Database2.tables;
EXEC log_ins @LogError, @PrimaryKey,7,1,NULL,1,'Script Completed Successfully' END TRY

BEGIN CATCH
SELECT @ix_sys_error = ERROR_NUMBER()
SELECT @ErrorMessage = ERROR_MESSAGE()
EXEC ix_sys_event_log_ins @LogError, @PrimaryKey,6,1,NULL,1,@ErrorMessage,NULL
END CATCH

IF (@ix_sys_error !=0)
RETURN 1
ELSE
RETURN 0
END
Posted
Updated 22-Dec-16 10:55am
v2
Comments
[no name] 22-Dec-16 13:37pm    
"but dont know whether this will work or not", well try it and find out.
PIEBALDconsult 22-Dec-16 14:42pm    
Nope. Any reason not to use SSMS or SSIS?
If the two databases are in the same instance, SELECT INTO might work if your needs are simple.
Just this week I started using SMO; there are a few Articles here on CP.

1 solution

Will this help?

https://www.codeproject.com/tips/664327/copy-table-schema-and-data-from-one-database-to-an
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900