How to give acces to Reports via SQL - sql

I have a large number of reports where access is controlled on Report level and not folder level.
While one method is to use Server Groups, then this is not the way the setup currently is. Although that would be a good way to manage it.
Using
select C.UserName, D.RoleName, D.Description, E.Path, E.Name
from dbo.PolicyUserRole A
inner join dbo.Policies B on A.PolicyID = B.PolicyID
inner join dbo.Users C on A.UserID = C.UserID
inner join dbo.Roles D on A.RoleID = D.RoleID
inner join dbo.Catalog E on A.PolicyID = E.PolicyID
Where C.UserName = 'XXX'
order by C.UserName
I can get all the reports a user has access to.
So I thought I could generate an SQL statement that can grant access to a user to the same reports as another user has access too, or just a given report.
But I can't seem to wrap my head around how many Places I have to insert data to make this happen.
Does anyone have a solution to this, or a starting point.
Without wanting to destroy something, I thought I could use SQL to grant access to specific reports.
Edit:
It seems I am not the only one wanting to do this:
http://social.msdn.microsoft.com/Forums/en-US/273420db-a506-47bb-9c13-f360e44996bd/bulk-add-users
However it seams the tables are not officially documented, further there are problems concerning the structure and inhertitence of rights, which leaves the RS.exe the only method short of using the webinterface to give users or add user rights to a specific report or folder.

Related

How to get table's metadata from linked server (SQL Server)

How to get tables metadata from linked server/another database? The query below works ideally when running on the main server, but returns null if calls remote server or even another database.
select object_name(major_id)
from server.bd1.sys.extended_properties
Probably, it is caused that the specific path for metadata functions (like object_name()) is not defined. Hence, functions take data not from remote server/DB, but server and DB they are launched.
If it is true, would you prompt the specific path (DB, schema) of metadata functions that I could define path explicitly.
Thank you for help.
Solved by using direct query to the tables with metadata except using builtin functions
select
c.name schma
,a.name tble
,b.name desc_categ
,b.value desc_name
from server.db.sys.objects a
join server.db.sys.extended_properties b on a.parent_object_id = b.major_id
join server.db.sys.schemas c on a.schema_id = c.schema_id

SQL error Statement, Missing Operator

Is there anything wrong with this statement?
SELECT *
FROM Movies INNER JOIN
Sessions
ON Movies.MovieID=Sessions.MovieID INNER JOIN
Tickets
ON Sessions.SessionID=Tickets.SessionID;
When ever I run it on Access I get a Syntax error 'Missing Operator'.
Also are there any alternatives to Access that I can import data from an excel spread sheet?
In general, no. In MS Access, yes. It likes extra parentheses, probably because the database developers don't believe in readability:
SELECT *
FROM (Movies INNER JOIN
Sessions
ON Movies.MovieID = Sessions.MovieID
) INNER JOIN
Tickets
ON Sessions.SessionID = Tickets.SessionID;
You could enable OPENROWSET if you have a local instance of SQL, and install MDACs (I would install both x86 and x64 if you have a 64 bit pc). Below is a link to an article that will help you get setup. Also, be sure to run the management studio with elevated privileges.
How to enable Ad Hoc Distributed Queries
Below is how the query would look. In my example I use Excel 8.0 instead of 12 because the column names are addressable in my select statements for 8.
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=C:\Temp\MyExcelDoc.xlsx;',
'SELECT * FROM [Sheet1$]')
You can export data from an excel spreadsheet into a number of formats. I find two of the best to be;
Comma Separated Values (CSV)
XML
In many cases, you can deal with the data directly from the Excell spreadsheet.
It really depends on what you want to do.
Your SQL query looks fine - but with Access you need to do the extra joins in brackets;
SELECT * FROM ((Movies
INNER JOIN Sessions ON Movies.MovieID=Sessions.MovieID)
INNER JOIN Tickets ON Sessions.SessionID=Tickets.SessionID)
;

Ignore denied columns in "select * from"

I need to restrict an (support) user from viewing columns in a table (other users should have full access to this table).
So I granted access to only the columns I specified via "GRANT SELECT ON dbo.TestTable (FirstCol, SecondCol, ThirdCol) TO HR_Intern;"
But when I am running a "SELECT * FROM dbo.TestTable;" i got an Access Denied Error for every other column in the table.
The user is doing customer support using the MSSQL Management Studio directly on the database and the errors won't allow the user to edit the data.
Is it possible to just display the columns the user have access to and ignoring every denied column?
Thanks for your help :)
Better to create a VIEW and provide the users access to it. In the VIEW only those columns these users can see should be part of SELECT statement.
As pointed out by others, you need to replace * by an explicit select list.
In case you are worried about having to specify things twice, here is a query to retrieve the list of permitted columns from metadata.
If you like, you can use its result set to generate (part of) the select list for the query on TestTable.
SELECT c.name
FROM sys.columns c
INNER JOIN sys.database_permissions p
ON p.class = 1
AND p.major_id = c.object_id
AND p.minor_id = c.column_id
AND p.state = 'G'
AND p.grantee_principal_id = DATABASE_PRINCIPAL_ID('HR_Intern')
WHERE c.object_id = OBJECT_ID('dbo.TestTable')
Replace DATABASE_PRINCIPAL_ID('HR_Intern') by DATABASE_PRINCIPAL_ID() to get metadata for the currently active user.
The query is still pretty crude; it disregards table-wide grants, and all denies. You may want to experiment with that a bit.
No. That is how security works in SQL. Basically "SELECT *" is not good form, one is supposed to provide a field list.
If the result set would magically change based on the user logged in that would result in a lot of crappy bug reports because applications would suddenly not work. You asked for all fields, that can not be sent, hence an error report.
One workaround is to have a view with a limited number of fields and direct this user to use the views. Obviously that costs time and attention during development.

Need to query SSRS database for report access

I would like to know if there is a way to return in a SQL (2005) query (for all reports):
Report ID, Report Name, Report Path, User and Group Security, Datasources.
I know that I can go to my report server page and go to Properties > Security, but we have over 120 reports and I want to see if there is an easier way to get the information I need.
Thanks in advance!
A. Never ever query RS Database directly until you know what are you doing. The queries could acquires lock which could affect overall performance of RS.
B. To know about report execution stats, you can use ExecutionLog view in RS 2008 onwards or following query with NOLOCK Hint.
Select CAST(C.Name AS VARCHAR(20)) [Name],
E.ReportID,
E.InstanceName,
E.UserName,
E.RequestType,
E.Format,
E.Parameters,
E.TimeStart,
E.TimeEnd,
E.TimeDataRetrieval,
E.TimeProcessing,
E.TimeRendering,
E.Source,
E.Status,
E.ByteCount,
E.[RowCount]
from executionlog E WITH (NOLOCK) inner join catalog C WITH (NOLOCK)
on E.ReportID = C.ItemID
C. RS exposes almost all the functionality via SOAP APIs. For example this sample published my MS shows how to get security information on Report Items http://msftrsprodsamples.codeplex.com/wikipage?title=SS2008R2%21RSPermissions%20Sample%20Application&referringTitle=Home
To know more about RS SOAP APIs please see http://msdn.microsoft.com/en-us/library/ms154052.aspx
You can query the ReportServer database directly using something like SSMS. Most of what you're looking for can be found directly in the dbo.Catalog table:
SELECT *
FROM dbo.Catalog
WHERE Type = 2; -- 2 = Reports, 5 = Data Sources

Stored procedure dependencies in SQL Server Management Studio

I don't know much about the MS world, but now it happens to be that I have to use SQL Server Management Studio 2008.
My problem: I have a column in a table, and I need to see all the stored procedures that may be acting on it.
I tried right-clicking and going 'View Dependencies' but that doesn't seem to be returning everything that it should be.
Questions like this one: SQL Server Dependencies have answers that offer 3 types of solutions
Paid third party tools.
Writing your own scripts.
Exporting everything into text files and grepping them.
WTF? Am I missing something obvious? Is that actually how things work? I would imagine that this is a very common use case: you want to alter table and you want to make sure you won't break anything. Or if say you're looking at a new project with a DB for the first time and you want to see how certain columns get populated with stored procedures. Is there actually no quick and easy built-in workflow to do this?
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
Use this query:
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%YOUR COLUMN %'
AND ROUTINE_TYPE='PROCEDURE'
I have spent a good amount of time trying to find a way to identify column level dependencies in a quick way without having to search text or use third party applications. The other challenge is finding dependencies across multiple databases where table names may repeat, which will cause false positives when searching SP text.
As of SQL 2008, there is a function that returns dependencies across databases on a field level.
The code below works with a few exceptions:
It will fail if there are stored procedures with invalid references on tables/fields that have been deleted (Incidently I found this to be useful to find SPs that had been accidentally broken by table modifications).
It doesn't find all dependencies in cases where the SP is using temp tables in unusual ways.
In some cases I found that it was returning false positives for complex stored procedures.
MSDN Documentation
This code should be run from within the database where the SP is in order to be able to cross to other database dependencies.
SELECT
--SP, View, or Function
ReferencingName = o.name,
ReferencingType = o.type_desc,
--Referenced Field
ref.referenced_database_name, --will be null if the DB is not explicitly called out
ref.referenced_schema_name, --will be null or blank if the DB is not explicitly called out
ref.referenced_entity_name,
ref.referenced_minor_name
FROM sys.objects AS o
cross apply sys.dm_sql_referenced_entities('dbo.' + o.name, 'Object') ref
where o.type in ('FN','IF','V','P','TF')
I wonder why you cannot see the dependencies via the 'View Dependencies' dialog because it works perfectly fine for me. Nevertheless you can query the 'sys.sql_expression_dependencies' system view and obtain the dependency information that you want.
Example
SELECT OBJECT_NAME(referencing_id),OBJECT_NAME(referenced_id)
FROM sys.sql_expression_dependencies
WHERE referenced_id = OBJECT_ID('XXX')
You can of course project other information that you might need.
List of All Dependent Objects in single query.
select distinct A.name from sys.procedures A inner join sys.sql_dependencies B
on A.object_id = B.object_id;
OR
select distinct A.name from sys.objects A inner join sys.sql_dependencies B
on A.object_id = B.object_id where A.type_desc = 'mentioned your Object Type';