SQL view from another Database - sql

I want to create a view in a Database residing in SQL Server Express 2008.
The view will be take its data from table residing on another server SQL Server 2008 on another machine in the same LAN.
My Question is if a must create a Linked Server for this goal and if it is possible to create a linked Server on SQL Express.
Are there any solutions without using a linked Server?

Yes and Yes and No.
See my answer here:
Login failed for user sql
To access table T_Users on database catalog Cor_Basic on server CORDB2005 with user ApertureWebServicesDE (on CORDB2005) and password MY_TOP_SECRET_PASSWORD you would use:
EXEC master.dbo.sp_addlinkedserver
#server = N'RemoteDB'
,#srvproduct = 'OLE DB Provider for SQL'
,#provider = N'SQLNCLI'
,#datasrc = 'CORDB2005'
,#catalog = 'COR_Basic'
GO
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'RemoteDB'
,#useself = false
--,#locallogin = 'LocalIntegrationUser'
,#locallogin = NULL
,#rmtuser = N'ApertureWebServicesDE'
,#rmtpassword = N'MY_TOP_SECRET_PASSWORD'
GO
SELECT *
FROM RemoteDB.COR_Basic.dbo.T_Users

Related

Create Linked Server to SQL Azure Database not working

I have follwed the below step from the question that was answered in relating to the linked server for Azure DB
I need to add a linked server to a MS Azure SQL Server
-- Make a link to the cloud
EXEC sp_addlinkedserver
#server='[servername].database.windows.net', -- specify the name of the linked server
#srvproduct=N'Azure SQL Db',
#provider=N'SQLNCLI',
#datasrc='yourservername', -- add here your server name
#catalog='FCS';
GO
--Set up login mapping
EXEC sp_addlinkedsrvlogin
#rmtsrvname = '[servername].database.windows.net',
#useself = 'FALSE',
#locallogin=NULL,
#rmtuser = 'username',
#rmtpassword = 'password'
GO
This does create a linked server in my envirnoment, however it doesn't connect to the catalog that I have specified (FCS). It connects to a default for some reason. Is there something i am doing wrong
Here's a simple example explaining how to connect to a Azure SQL Database using distributed queries:
-- Configure the linked server
-- Add one Azure SQL DB as Linked Server
EXEC sp_addlinkedserver
#server='myLinkedServer', -- here you can specify the name of the linked server
#srvproduct='',
#provider='sqlncli', -- using SQL Server Native Client
#datasrc='myServer.database.windows.net', -- add here your server name
#location='',
#provstr='',
#catalog='myDatabase' -- add here your database name as initial catalog (you cannot connect to the master database)
-- Add credentials and options to this linked server
EXEC sp_addlinkedsrvlogin
#rmtsrvname = 'myLinkedServer',
#useself = 'false',
#rmtuser = 'myLogin', -- add here your login on Azure DB
#rmtpassword = 'myPassword' -- add here your password on Azure DB
EXEC sp_serveroption 'myLinkedServer', 'rpc out', true;
-- Now you can use the linked server to execute 4-part queries
-- You can create a new table in the Azure DB
EXEC ('CREATE TABLE t1tutut2(col1 int not null CONSTRAINT PK_col1 PRIMARY KEY CLUSTERED (col1) )') at myLinkedServer
-- Insert data from your local SQL Server
EXEC ('INSERT INTO t1tutut2 VALUES(1),(2),(3)') at myLinkedServer
-- Query the data using 4-part names
SELECT * FROM myLinkedServer.myDatabase.dbo.myTable
Please follow the official example and test again.
Reference: Add a Azure SQL Database as a Linked Server For Use With Distributed Queries on Cloud and On-Premises Databases
Hope this helps.
You are using the #server as you should use the #datasrc. The #server is just the name of your linked server on your SQL Server (on-premise) instance, while the #datasrc should be the name of your Azure SQL Database logical server.
Please read this article for a complete example of how to create a linked server.

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

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

I need to add a linked server to a MS Azure SQL Server

I have tried and tried, and can not get linked. I can connect to the server using SSMS, but can not link to it from a local server. Here is my script (replacing things in brackets with pertainent information):
EXEC master.dbo.sp_addlinkedserver
#server = N'[servername].database.windows.net',
#srvproduct = N'Any',
#provider = N'MSDASQL',
#datasrc = N'Azure_ODBC1'
GO
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'[servername]',
#useself = N'False',
#locallogin = NULL,
#rmtuser = N'[username]',
#rmtpassword = '[password]'
GO
As specified in ckarst second link, there is a solution that works. I am posting it here to save you the trouble to search for it. As suggested by JuanPableJofre in this page Azure feedback :
Using SQL 2014, I was able to do a distributed query between a local SQL server and a SQL Azure.
First, I created a Linked-Server:
Linked Server (name): LinkedServerName
Provider: Microsoft OLE DB Provider for SQL Server
Product name: (blank)
Data Source: azure-db.database.windows.net
Provider string: (blank)
Location: (blank)
Catalog: db-name
In security options: (*)
Be made using this security context
Remote login: azure-user-name
With password: yourPassword
In SSMS entered the following test query:
use [Local_DB]
go
Select *
from [LinkedServerName].[RemoteDB].[dbo].[Remote_Table]
It worked beautifully !!
To summarize, the linked server is created on your local database. The catalog (database name) is important as Azure might not let you specify it in a query (ie: use azureDBName will not work on Azure), so the database name has to be in the catalog.
Unfortunately, Linked Server is not supported by SQL Azure DB.
https://msdn.microsoft.com/en-us/library/azure/ee336281.aspx
However, as you can see from the forum link below, Microsoft is aware of the scenario and the customer feedback has been heard. http://feedback.azure.com/forums/217321-sql-database/suggestions/402636-cross-database-reference
While Azure DB doesn't support defining Linked Servers, you can use the recently previewed Elastic Query feature to define an "External Data Source" that is just another Azure DB database, and define "External Tables" that are references to tables in that external database. Then you can query these as though they are local objects. This is very similar to the Linked Server concept and is described in detail here.
-- Make a link to the cloud
EXEC sp_addlinkedserver
#server='[servername].database.windows.net', -- specify the name of the linked server
#srvproduct=N'Azure SQL Db',
#provider=N'SQLNCLI',
#datasrc='yourservername', -- add here your server name
#catalog='FCS';
GO
--Set up login mapping
EXEC sp_addlinkedsrvlogin
#rmtsrvname = '[servername].database.windows.net',
#useself = 'FALSE',
#locallogin=NULL,
#rmtuser = 'username',
#rmtpassword = 'password'
GO
This does create a linked server in my envirnoment, however it doesn't connect to the catalog that I have specified (FCS). It connects to a default for some reason. Is there something i am doing wrong

How to create new SQL Server 2012 login without SQL Server Management Studio?

I have creating my system using Entity Framework code first for it create database automatic if it is not exist, but first, I need a user for access database
But I know only create user of SQL Server with SQL Server Management Studio, but I'm in a situation, where computer have SQL Server 2012 installed, but have not SQL Server Management Studio
What can I do for create a new database user without SQL Server Management Studio? Or have any solution with Entity Framework like it create database automatic?
To create a login from windows domain account using SQL Server Windows Authentication
CREATE LOGIN [Domain\Windows_UserName] FROM WINDOWS;
GO
Then you have to Create A user with that Login
CREATE USER [Domain\Windows_UserName] FOR LOGIN [Domain\Windows_UserName];
GO
For Sql Server Authentication
CREATE LOGIN Login_Name WITH PASSWORD = 'ksblaksvfdh&';
GO
You can use SQL Server SMO to programatically create the database Users and add it the 1st time in your EF Code First DatabaseInitializer. Use with caution though ;)
Add References to the following.
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SqlEnum
Then create an SQL login and also add the user to your target database
var serverName = "."; // Your SQL Server Instance name
var databaseName = "Test"; // Your database name
var loginName = "testuserY"; // Your login name (should not exist - or you should add code to check if the login exists)
Server svr = new Server(serverName);
var db = svr.Databases[databaseName];
if (db != null)
{
// You probably want to create a login and add as a user to your database
Login login = new Login(svr, loginName);
login.DefaultDatabase = "master"; // Logins typically have master as default database
login.LoginType = LoginType.SqlLogin;
login.Create("foobar", LoginCreateOptions.None); // Enter a suitable password
login.Enable();
User user = new User(db, loginName);
user.UserType = UserType.SqlLogin;
user.Login = login.Name;
user.Create();
// add a role
user.AddToRole("db_owner");
}
Source of Source Code: https://stackoverflow.com/a/7899436/325521

Error using distributed transaction in SQL Server 2008 R2

I am using SQL Server 2008 R2.
I am getting the following error when I try to execute a SP from java side. The same SP is running well when I use query browser to run.
I have tried using hot fix also but it is not working.
ERROR :: The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "server name" was unable to begin a distributed transaction.
when executed in transaction
Inside SQL Server Management Studio, expand Server Objects, then Linked Server, then right click on the linked server in question and choose 'Properties.' Select the 'Server Options' page, and make sure 'Enable Promotion of Distributed Transactions' is set to 'False'
Or you can do it with T-SQL:
USE master;
EXEC sp_serveroption '<<your linked server name>>', 'remote proc transaction promotion', 'false';