SQL Server Trigger on a Linked Server for reporting services - sql-server

I have and SQL server with a report table that is linked to an SQL database on a separate server. The linked server is maintained by a 3rd party vendor so I'd rather not add triggers... I was hoping to run a report based on a value being inserted on a linked table. Tried the following:
CREATE TRIGGER [dbo].[RunReport] ON [linkedServer].[database].[dbo].
[CustomerComments]
FOR INSERT
AS
exec [ReportServer].dbo.AddEvent #EventType='TimedSubscription',
#EventData='xxxxx'
I get the error "contains more than the maximum number of prefixes. The maximum is 2." Anyway to trigger my report without altering the linked database. I also created a view of the linked table that didn't work either.
Thanks,

Leaving aside the idea, you can create the trigger on linked server this way:
exec [linkedServer].[database].sys.sp_executesql N'CREATE TRIGGER [dbo].[RunReport] ON [dbo].
[CustomerComments]
FOR INSERT
AS
exec [ReportServer].dbo.AddEvent #EventType='TimedSubscription',
#EventData='xxxxx'

Related

SQL Server select from linked server throw error if user has "deny view definition"

I have users with datareader permission on my sql, the problem is that i noticed that the users are doing "fishing" query a lot (they don't know what exactly should be querying to get what thy need).
This is a production SQL and we started to have deadlock because of those users.
Trying to don't break their process i applied "deny view definition to [user]" so they cannot see the list of tables, view, stored procedure, so no more "fishing"
This was a great solution, it works perfect if you connect directly to the SQL Server, you can run queries (select) but you cannot see the list of tables.
However many users are using those account to connect to our SQL using linked server, and when the run a query using the linked server they are getting error:
SELECT TOP 100 *
FROM [SERVERSQL].[DATABASE].dbo.[TABLE]
Msg 7314, Level 16, State 1, Line 1
The OLE DB provider "SQLNCLI11" for linked server "SERVERSQL" does not contain the table ""[DATABASE]"."dbo"."[TABLE]"". The table either does not exist or the current user does not have permissions on that table.
Using SQL Server version 2014 (v12.0.2000.8) and 2016 (v13.0.5492.2).
What is the correct way to hide tables, view, stored procedures, etc but allow select and make it work on linked server?
Thanks
Linked server from one SQL Server to Another:
Linked server name is: PLWNSAVSQL02D which is SQL Server
This sql to linked server works:
select count(*) from [PLWNSAVSQL02D].[SFI_WMS].dbo.[TCOMPANYPALLETMESSAGES];

Cannot create trigger on linked server in SQL Server

I have one local database in SQL Server and one Oracle database as an linked server in SQL Server. I want to create a trigger on the linked server (Oracle database table) which will insert data into SQL Server table. This is what I have tried
CREATE TRIGGER TrgItemInsert
ON ORACLEDB..SYSTEM.ITEM
AFTER INSERT
AS
BEGIN
PRINT 'Something Happened to Table ITEM'
END
but I get the following error:
The object name 'ORACLEDB..SYSTEM.ITEM' contains more than the maximum number of prefixes. The maximum is 2.
Is this possible?
Is there any work around to achieve this task?
Connection to the linked server is working and I can query the data in the linked server using 'SELECT' statements.
Thanks in advance.

Query database running on another physical SQL Server

Historically we have a product which installed two databases on the same server. There is an custom application which assumes that both databases are on the same server.
In a new version they have split the databases onto two separate servers and obviously now the custom application is giving the error:
Database 'DB_2' does not exist. Make sure that the
name is entered correctly.
Is there anything I can do in the SQL Server setup so that the application is still able to query the DB_2 database without modifying the custom application?
The query being used is structured as follows:
Use DB_2
SELECT * FROM MyUser.MyTable
You can create a linked Server, then Create a Database DB_2 add a Synonym for different objects. something like below.
use master
GO;
EXEC master.dbo.sp_addlinkedserver #server = N'RemoteServer', #srvproduct=N'SQL Server'
GO
CREATE DATABASE [DB_2];
GO
USE [DB_2]
GO
CREATE SYNONYM [MyUser].[MyTable] FOR [RemoteServer].[db].[MyUser].[MyTable]
GO
You can use Linked Servers feature. In SSMS go to Server Object/Linked Servers folder in Object Explorer. And link second server. So you can query another DB using this SELECT * FROM [Linked_Server_Name].[Database_Name].[Schema_Name].[Table_Name]

Querying a view that retrieves data from a linked server

I have an emergency for which i must give a reply in short time.
I wrote an application that allows the user to query a single sql server db (MyDB from now on).
I assumed that to query other dbs (Oracles, csv, ...) the trick was:
1) creating a linked server on the same server wher the MyDB is
2) on MyDb creating a view that somehow selects data from a table on the linked server
Now I am not sure that (2) is possible. i am trying to create a linked server with Excel but with failure, this is why I asked also this.
So my question is:
is it possible to run
select * from VIEW_WITH_DATA_FROM_LINKED_SERVER
if the connection is made to MyDB and VIEW_WITH_DATA_FROM_LINKED_SERVER is a view belonging to MyDb?
Thanks!
You can create a view using linked server by below query.
USE [SNHU_Reporting]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [dbo].[VIEW_WITH_DATA_FROM_LINKED_SERVER] AS
SELECT * FROM [Servername].db_name.dbo.tablename --dbo is the schema name.
GO

SQL Server cross database alias

I'm trying to understand how I can use an alias to reference another database in the same instance, without having to use a hardcoded name.
The scenario is as below:
I have a data db with stores data, an audit db which keeps all changes made. for various reason, i want to keep the audit data in a separate database, not least because it can get quite large and for reporting purposes.
In the data db, I don't want to reference this by a hardcoded name but an alias so that in different environments, I don't have to change the name and various sp's to reference the new name.
for example:
mydevdata
mydevaudit
If a sp exists in mydevdata such as which calls the mydevaudit, I don't want to change the sp when I go to test where the db's may be called mytestdata and mytestaudit. Again, for various reasons, the database names can change, more to do with spaces an instances etc.
So if I had procedure in mydevdata:
proc A
begin
insert into mydevaudit.table.abc(somecol)
select 1
end
when I go to test, I don't want to be change the procedure to reference another name, (assume for sake of argument that happened)
Instead I am looking to do something like:
proc A
begin
insert into AUDITEBALIAS.table.abc(somecol)
select 1
end
I am interested in finding out how I could do something like that, and the pro's and cons.
Also, dymnamic SQL is not an option.
thanks in advance for you help.
You may be able to use synonyms
CREATE SYNONYM WholeTableAliasWithDBetc FOR TheDB.dbo.TheTable
This means all object references in the local DB are local to that DB, except for synonyms that hide the other database from you.
You can also use stored procedures in the audit DB. There is a 3rd form of EXEC that is little used where you can parametrise the stored proc name
DECLARE #module_name_var varchar(100)
SET #module_name_var = 'mydevaudit.dbo.AuditProc'
-- SET #module_name_var = 'whatever.dbo.AuditProc'
EXEC #module_name_var #p1, #p2, ...
Obviously you can change module_name_var to use whatever DB you like
I've just posted this to How to create Sql Synonym or "Alias" for Database Name? which is a workaround for the same situation:
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

Resources