I have a SQL Server instance running on my machine.
It has 2 databases:
SF_PROD
SF_INIT.
SF_PROD and SF_INIT have a common table USER_MASTER with the same structure.
My requirement is that whenever SF_PROD.USER_MASTER gets updated, the same operation should be applied to SF_INIT.USER_MASTER.
Is there any way to accomplish this task?
If both databases are running on the same SQL Server Instance, then you can just write Trigger on SF_PROD.USER_MASTER which inserts the data into SF_INIT.USER_MASTER table.
CREATE TRIGGER SyncUserMasterTrigger ON SF_PROD.USER_MASTER
FOR INSERT
AS
INSERT INTO SF_INIT.USER_MASTER (col1, col2 , col3)
SELECT col1 , col2 , col3
FROM inserted
Related
I have a table where I would like to select certain columns and then create transformed columns based on that selection. Due to security reasons, I'm not able to create a new table and thought there may be a way to SELECT and ALTER in the same statement.
My statement below runs, but the column is not produced. Am I doing something wrong/is this approach not possible? Is there a better approach?
SELECT * col1,col2,col3 FROM db
AS db2
ALTER TABLE db2 ADD col4 AS (transformation) PERSISTED
Guidance and recommendation is appreciated.
You likely need just perform a query or, if you want query data often, then view in database.
In SQL server and most likely in other SQL variants you can create view with next statement (db would be table name, db2 view name):
create view db2 as
select col1, col2, col3, (transformation) as col4
from db
Usually you need to grant some permissions to it, unless you are sole user of it.
Creating a view is one-time task, afterwards you can query data as follows:
select col1, col2, col3, col4
from db2
where ...
I have two databases on different servers, which have tables called dbo.A. The data is both is largely the same, but I want to make sure both tables have the same data. I've been using SQL Server June 2016 to export data from one table to the other, but the error I get is:
Violation of PRIMARY KEY constraint ''. Cannot insert duplicate key in object A
The duplicate key value is 'Some text here'
I know I can delete the table and reinsert the rows, but that's cumbersome and pretty bad practice. What would be the best way for me to update the data in the second database?
Add the server as your linked server and use the following statement.
To add rows in TableA from say Serve B's Table A.
INSERT INTO dbo.A (Col1 , Col2 , Col3 , ....)
SELECT Col1 , Col2 , Col3 , ....
FROM [LinkedServerB].[DBName].[dbo].[A] A
WHERE NOT EXISTS ( SELECT 1 FROM dbo.A
WHERE A.PK_Column = PK_Column)
And then use the same query on Server B to add the rows from Server A
To add rows in TableA from say Serve B's Table A.
INSERT INTO dbo.A (Col1 , Col2 , Col3 , ....)
SELECT Col1 , Col2 , Col3 , ....
FROM [LinkedServerA].[DBName].[dbo].[A] A
WHERE NOT EXISTS ( SELECT 1 FROM dbo.A
WHERE A.PK_Column = PK_Column)
Ok if you can't use a linked server can you copy the data into an empty staging table. Then run the similar insert statement but use the staging table instead of the linked table
I have a table with million records.
But, I just want to copy first 10000 rows and insert it to another table which is in different server in sql server management studio.
Server1
Db1
Table : table1
Server2
Db1
Table : table1
I want to copy 10000 rows from table1 in Db1 and insert it to table1 in Db2 from Server1 to Server2.
I know this query syntax, but i dont know how can i deal with different servers
INSERT table1 (Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM table2
WHERE ...
on your first instance, you can add a linked server to your second instance, and then reference it with 4 part naming . You can add a linked server by expanding the server objects node, and then right click 'Linked Servers'
Then your query would look like
INSERT YourlinkedServer.YourDB.YourSchema.table1 (Col1, Col2, ..., ColN)
SELECT TOP 1000 Col1, Col2, ..., ColN
FROM table2
WHERE ...
Your query could also be run by inserting the remote data into a local table
INSERT table1 (Col1, Col2, ..., ColN)
SELECT TOP 1000 Col1, Col2, ..., ColN
FROM YourlinkedServer.YourDB.YourSchema.table2
WHERE ...
linked servers https://msdn.microsoft.com/en-GB/library/ms188279.aspx
In addition to the other answers given involving linked servers, you can right click on your database in SSMS and use the Import/Export Wizard to move rows from one server to another.
In the wizard you can specify a query that will return only the top 10k rows that you want to export.
Updated the answer to include how you would do it across a linked server. The 'Insert Into' statement allows you to insert into a linked server just as you would into another table on the same server, a temp table, a table variable, etc. As long as the select contains the correct number of columns and corresponding data types of the second table the following pseudo code works:
Insert Into LinkedServer.Db2.DbSchema.Table1
Select Top 10000
Column1,
Column2,
...
From Db1.Table1
Where ...
I have 2 tables, TABLE1 with columns col1, col2, col3, col4 and col5 in a database on server1. I have TABLE2 with the same columns and almost same data as TABLE1 excluding columns col2 and col5 in a database on server2. TABLE2 has the same data in col1, col3 and col4 as TABLE1. Now I know I can just create a new table and use the import/export feature of sql-server. But this would not be feasible if the tables had tons of data and more columns.
So my question is there any way to insert data in col2 and col5 to TABLE2 through any other means? If this is possible I would like to make sure the data that is being inserted is col2 and col5 in TABLE2 matches to that of TABLE1 meaning if I have col1 with data lets say 5 and col2 has data xyz, would like this data to be inserted at this row. (FYI col1 is the primary key on both tables).
Both servers are sql server 2008r2.
Provided the two SQL servers are on the same network, make a linked server on server2.
How to create the linked server for SQL Server 2008 where we have the database from 2000 and 2005
You may need to do an IDENTITY INSERT should you wish to keep the Id columns the same.
How to turn IDENTITY_INSERT on and off using SQL Server 2008?
You would be able to write an INSERT statement along the lines of:
INSERT INTO [server2].[database1].[table1] ( col1, col3, col5 )
SELECT col1, col3, col5 FROM [server1].[database1].[table1]
If you don't want to introduce duplicate keys, use a WHERE NOT EXISTS col1 IN ( ... ).
If you don't want to introduce duplicate rows, use a EXCEPT statement.
Hope this helps you.
I need to move data from one table to another in the same database using an SSIS package, The data from source table is taken and it is computed to form two rows differently and load it into destination table.
I prefer using a stored procedure for this. Kindly help me out here..
A stored procedure to copy data from one table to another:
CREATE MyStoredProcedure AS
INSERT INTO Table1 (Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM TABLE2
Now put this in an execute SQL Task inside your SSIS:
EXEC MyStoredProcedure
Now consider whether you need the overhead of a SSIS package to do this when you can just run it in a SQL step in a SQL Agent job.