How to insert data from prod server to dev server in SQL Server - sql

I would like to insert data from prod server to dev server for a particular table.
I am using insert into SQL query and fully qualified name. That is I am specifying server name, databsename, schema name and table name.
insert into ServerADev.[ING_DB].dbo.[Table1]
select *
from ServerAProd.[ING_DB].dbo.[Table1]
where ID = '08914'
ID is the column in Table1.
For above query I am getting an error:
Cannot find ServerAProd in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
When I EXEC sp_addlinkedserver #server='ServerAProd', I am getting:
User does not have permission to perform this action.
Do I need to make a request to DBA (database admin) to grant permission to perform this query?

You need to set up a linked server to query a foreign server.
So, either:
ServerAProd is not what you named the linked server
or
You didn't create a linked server yet. You can use the sp_addlinkedserver from the error message, or browse to "server objects" in the object explorer then right-click -> new on "Linked Servers". See the link above for more details.
For your edit... yes this requires permissions:
When using Transact-SQL statements, requires ALTER ANY LINKED SERVER permission on the server or membership in the setupadmin fixed server role. When using Management Studio requires CONTROL SERVER permission or membership in the sysadmin fixed server role.

1. create linked server for the dev server on production server,
2. use this openquery() to insert data, so that you can insert large data very quickly.
INSERT OPENQUERY (devservername, 'SELECT * FROM devservar_database..dev_server_table_name')
select * from production_table_database_name..production_table_table_name

Related

access a synonym using linked server

Using SQL server 2016
Is it possible to access a synonym from a linked server, when the actual target table of the synonym cannot be accessed by the source server?
There are two databases in one server
say on
server1
database1
database2
server2
database3
And there is a table "table1" on database1.
And a synonym for that has been created in database2
USE [database2]
GO
CREATE SYNONYM [dbo].[synonym1_for_table1] FOR [server1].[database1].[dbo].[table1]
GO
There is a linked server access for database2 from server2
When the following query is executed from the server2
SELECT *
FROM [server1].[database2].[dbo].[synonym1_for_table1]
Below error is encountered
Cannot process the object [database2].[dbo].[synonym1_for_table1]. The
OLE DB provider "SQLNCLI" for linked server "server1" indicates that
either the object has no columns or the current user does not have
permissions on that object.
In server2 under "linked servers", under the tree
I see tables and views of database2 but not synonyms.
Is there a way to see synonyms. That could be a solution to this issue.
Update:
If the synonym is made a view, the following error is observed
The server principal "xyz" is not able to access the database "database1" under the current security context.
"xyz" cannot be created/cannot be granted access to "database1".
You should create a view to read data from table present on the other server/database.
Also, create a separate user (service account or user account) that will have access only to read data from the source table and can execute the view(present on the different server).

Calling sp_help on linked server

I have a table named 'Application' on linked server (hosting server) in schema 'ID'.
I am trying to view the details of same using sp_help.
But I'm not able to do it.
The problem is:
1. sp_help is in schema dbo. (It is obvious!)
2. But my table in another schema named ID. (e.g. select * from ID.Application)
I've added linked server to my local sql server management studio.
And tried:
exec [198.198.198.198].[sms].[dbo].sp_help applicantion
exec [198.198.198.198].[sms].[dbo].sp_help id.applicantion
exec [198.198.198.198].[sms].[dbo].sp_help sms.id.applicantion
'sms' is my database on linked server.
Results: syntax error.
I tried: SQL using sp_HelpText to view a stored procedure on a linked server
But my problem is 2 different schema.
Any suggestions??
Does this work?
exec [198.198.198.198].[sms].[dbo].sp_help 'id.applicantion'

Add a group in SQL 2008 RC

I have a group called kwr-fs-dws-sqladm (Active Directory/Exchange) and there are two users in this group.
This group will go to all sql server and instances. I have access to windows server 2008 as admin.
How can I add that group in all 60 SQL Servers and 200 Instances?
i.e. kwr-fs-dws-sqladm - user as sysadmin -.
Use [Master]
CREATE LOGIN [DOMAIN\kwr-fs-fws-sqladm] FROM WINDOWS;
EXEC sp_addsrvrolemember 'DOMAIN\kwr-fs-fws-sqladm', 'sysadmin';
Execute on each server, you will want to check out the CREATE LOGIN syntax for extra options.
I've never managed that many SQL servers, i'd assume there is a way of executing on all. One way is to add each server to a server group in SSMS, then you can right click that group and create a new query which will execute on each server.

Using T-Sql, how can I insert from one table on a remote server into another table on my local server?

Given the remote server 'Production' (currently accessible via an IP) and the local database 'Development', how can I run an INSERT into 'Development' from 'Production' using T-SQL?
I'm using MS SQL 2005 and the table structures are a lot different between the two databases hence the need for me to manually write some migration scripts.
UPDATE:
T-SQL really isn't my bag. I've tried the following (not knowing what I'm doing):
EXEC sp_addlinkedserver
#server = N'20.0.0.1\SQLEXPRESS',
#srvproduct=N'SQL Server' ;
GO
EXEC sp_addlinkedsrvlogin '20.0.0.1\SQLEXPRESS', 'false',
'Domain\Administrator', 'sa', 'saPassword'
SELECT * FROM [20.0.0.1\SQLEXPRESS].[DatabaseName].[dbo].[Table]
And I get the error:
Login failed for user ''. The user is
not associated with a trusted SQL
Server connection.
create a linked server and then use 4 part notation
insert table
select <column names>
from LinkedserverName.DatabaseName.SchemaName.TableName
you can also use OPENROWSET
example
insert table
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
'SELECT GroupName, Name, DepartmentID
FROM AdventureWorks2008R2.HumanResources.Department
ORDER BY GroupName, Name') AS a;
try this to create the login
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'20.0.0.1\SQLEXPRESS',
#useself=N'False',
#locallogin=NULL,
#rmtuser=N'sa',
#rmtpassword='saPassword'
You can define the PROD Server as Linked Server to the DEV box and then access it.
However I think it would be easier to get a backup from PROD Box and Restore it to DEV or use SSIS for Schema Import.
Look into the RedGate tools, esp. SQL Data Compare. If that's not an option you should look at OPENDATASOURCE or OPENROWSET to access the remote database.
Well you can use a linked server and then use the 4 part names for objects (See BOL for how to set up a linked server)
Or you could use SSIS to set up the data migrations and connect to the remote server
Or you could use OPENROWSET
I'd probably use SSIS, but I'm already familiar with it.
Use SSMS. Right click on the target DB and select "Tasks", "Import Data". You will be able to preview the data and make conversions visually. Save the package in SSIS or run it now.

Drop User from SQL Server Database?

How can I drop user from a database without dropping it's logging?
The script should check if the user exists in database, if does then drop the user.
Is this what you are trying to do??
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'username')
DROP USER [username]
If you are using SQL Server Management Studio you can browse to the user and right-click selecting delete.
The accepted answer is working good enough. Additionally that is good to know SQL Server added IF EXIST to some DROP commands from version 2016 (13.x) including 'DROP USER' command.
IF EXISTS
Applies to: SQL Server ( SQL Server 2016 (13.x) through current version, SQL Database).
Conditionally drops the user only if it already exists.
So you could just delete user as below:
-- Syntax for SQL Server and Azure SQL Database
DROP USER IF EXISTS user_name
See the full description in this link: DROP USER (Transact-SQL)
Hope this help.