Create database on remote server via Linked Server - sql

I am automating deployments of our admin tools. When a new server is added to our monitoring system I have a trigger that creates a linked server.
I am trying to get my code to create database to work via the linked server from the central server.
Any tip and tricks on accomplishing this task?

Typically, you can't execute DDL against a linked server. There is a trick though. You can call sp_executesql remotely.
You'll need to set the Linked Server's RPC Out property to True before attempting.
EXEC [yourlinkedserver].tempdb.dbo.sp_executesql N'CREATE DATABASE Test;';

Related

Can I link a server that I only have read-access to?

Trying to link a server that I only have read-access to and uses windows authentication to log me in. I'm trying to link it to my local database so I can combine commands in both databases because my web application executes stored procedures.
Is this possible?
https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addlinkedserver-transact-sql?view=sql-server-2017
Yes, you can.
From your description, you want to uses windows authentication to log in the linked server.
When a linked server is created by using sp_addlinkedserver, a default self-mapping is added for all local logins. As a result, if the windows account for the local server also exist in the remote account, you can log in the linked server via the current windows account.
Besides after adding the linked server, you can also create a mapping between a login on the local instance of SQL Server and a security account on a remote server. For detailed information, please refer to the sp_addlinkedsrvlogin (Transact-SQL).

How to connect AWS sql server by sql server express

We have created SQL server database using RDS AWS console. With following configurations:
Availability zone
ap-south-1b
VPC
vpc-07b03fb50131ed688
Subnet group
default-vpc-07b03fb50131ed688
Subnets
subnet-01ea3ba2b300b123d
subnet-019551993b22cc459
Security groups
rds-launch-wizard-1 (sg-08ceba391dda818db)
( active )
Publicly accessible
Yes
But we are not able to connect it with SQL express with local machine. Any help to update security options..
Good day,
Not sure what you mean by connect it with SQL express. My feeling is that you confuse two unrelated application: (1) SQL Server Express and (2) SQL Server Management Studio (SSMS). With that being said, it is possible that you meant to connect from one server to the other using linked server for example. I will answer both options...
Connecting from SSMS to AWS SQL Server
This is well documented in the official AWS system as you can see here. Basically you need to copy your server endpoint from your account.
If this is what you meant so you must understand that SSMS is a client application and had nothing to do with SQL Server (meaning it is not part of SQL Server but totally separate application, which can be install with or without SQL Server like any unrelated app)
Create linked server from local server to AWS SQL Server
If you do mean to connect using linked server from one server to the other, then:
Step 1: Do the same as above option and get the information to connect from SSMS. We need the Server name, user name, and password.
Step 2: Confirm that you can connect from SSMS first.
Step 3: continue to create linked server, and map your remote username to the local server. For this you can use the bellow queries.
USE [master]
GO
-- create linked server to the remote server
EXEC master.dbo.sp_addlinkedserver
#server = N'<Your Server name come here>', -- this is the same information as you use from SSMS
#srvproduct=N'SQL Server';
GO
-- check your list of servers to confirm that you see the new one
select * from sys.servers
GO
-- Now we need to map your remote login to your new linked server
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'<Your Server name come here>',
#useself = 'FALSE',
#locallogin = NULL ,
#rmtuser = N'RonenAriely', -- enter your remote user name
#rmtpassword = '<Enter Your remote Password here>'
GO
-- that's it, wee can query the remote server from the local server now
--check that your can execute remote query
SELECT name FROM [<Your Server name come here>].master.sys.databases
GO
I hope this solve your issue :-)

Create a Linked Server Using a Windows Account in SQL Server

There are multiple applications on SQL Server which I am trying to connect to from a Single SQL Server. Instead of creating multiple SQL User ID we thought of creating a single Windows Service Account and use that to connect to other SQL Servers.
The issue is I dont know if I can configure a Linked Server to take the credentials of a Windows Service account.
Points:
Access has been given to the Windows user account
SQL Servers are on Different domains
Is there a best approach to read from multiple sources within the organization other than linked servers?
Update:
Please find below code I am trying to use to create a Linked Server:
USE [master]
GO
EXEC sp_addlinkedserver
#server=N'serverinstance',
#srvproduct=N'',
#provider=N'SQLNCLI',
#datasrc=N'serverinstance.domain.local';
GO
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname = N'serverinstance'
, #useself = N'TRUE'
, #locallogin = N'Domain\NTAccount'
--, #rmtuser = N'user'
--, #rmtpassword = N'pass'
GO
The linked server still fails. I do not want to use any SQL Server User Auth.
I think you need to setup a trust between the 2 domains and make sure the firewall is letting you access the linked server.
It can be either.
https://technet.microsoft.com/en-us/library/ms188477(v=sql.105).aspx
Authentication works the same regardless of if you use Sql or NT Domain authentication.

Issue deploying SQL Server database from local machine to SQL Azure

I have a database I've created on my local machine that I can't seem to correctly deploy to SQL Azure. I'm deploying two databases, including the one that provides user membership.
Every time I attempt this, when I try to login and access to the database is attempted, I receive a The system cannot find the file specified error:
I've tried deploying the database every way I could from Microsoft SQL Management Studio - by right clicking my databases and "Deploy Database to SQL Azure", by generating drop and create data only SQL scripts and running these against an existing target database, by creating a bacpac and importing this and all of these have the same result.
I've double checked connection strings in my .NET MVC applications web.config and these are all correct.
Interestingly, if I run my website locally with connection strings that pointed to my updated Azure SQL db, everything works fine - but on my Azure Website I always get the same results.
Your issue actually looks like a connection string issue but if anyone else ends up here thanks to Google you can resolve orphaned users on Azure SQL Db using:
ALTER USER <user_name> WITH Login = <login_name>;
I am not a AZURE expert, but when moving DB's from one server to another sometimes user's get orphaned making the DB not accessible.
Can you run this command on the AZURE DB via SQL MANAGEMENT STUDIO?
exec sp_change_users_login 'Report'
It will tell you what username and UserSSID may be orphaned. If indeed there is a returned record run
EXEC sp_change_users_login 'Auto_Fix', '[[UserName_goes_here]]'

Is there a way to find what accounts can execute xp_cmdshell?

I'd like to find out what users on a SQL Server system can execute xp_cmdshell. Is there a query that will do this?
I'd like to know how to do it for SQL Server 2000 - 2008
Googling "xp_cmdshell":
When first enabled, xp_cmdshell requires CONTROL SERVER
permission to execute and the Windows process created by
xp_cmdshell has the same security context as the SQL Server
service account.
http://msdn.microsoft.com/en-us/library/ms175046.aspx