Showing posts with label select. Show all posts
Showing posts with label select. Show all posts

Wednesday, March 28, 2012

Per-record processing?

Hi,

I am looking for a way to process each record from a SELECT query. Each record has to go through a stored procedure to be validated and different actions have to be taken depending on whether or not it passes the validation.

I'm a bit new to Transact SQL. My background is in programming, so I keep wanting to visualize a loop, which doesn't exist in TSQL, from what I understand. Can anyone help me understand how something of this type would be done?

Thank you!

SQL Server is designed for SET based operation. 'Per-Record' operations are often best left to a client application.

However, in some situations, it may be necessary to (a) gather relavent records into a temporary table, (b) set a counter variable with the @.@.Rowcount from the temporary table, (c) use a WHILE @.CurrentRecord < @.RecordCount type of loop.

However, this is guarenteed to be a non-optimal use of SQL Server. Perhaps if you were to post the select query, the stored procedure, and some sample data, folks here would help you properly design a SET based operation -if possible.

|||Thank you. I think this is just what I needed to move this into a .Net app.sql

Monday, March 26, 2012

Permit user to select columns for a report: why not use Dynamic SQL?

I certainly have read lots of negative comments about using Dynamic SQL. And I have read some of the excellent web pages on the subject, such as

http://www.sommarskog.se/dynamic_sql.html

I have learned much from those discussions.

But I still find myself faced with a problem for which Dynamic SQL seems like the only reasonable solution: letting a user select the columns to include in a report, and the sort order for the output data.

In the application of concern, there are maybe seven columns that one might want to have in a report. If the user can choose any combination of these seven columns, and the order in which they are displayed (as well as the SORT order), it would be necessary to write something like 7! (7-factorial = 5040) individual reports to provide all the possibilities that included all 7 columns, plus (7 choose 6, with concern for order) (or 7*6*5*4*3*2 = 5040) ways to choose 6 of the 7 columns (with a group-by taking care of the column not chosen), etc.

(Even if we chose to ignore the order, and let the "presentation layer" of a .NET UI take care of that, there are still (2^7 -1) = 127 different ways to choose the columns for the report, knowing that at least one column must be selected.)

And when another level of detail is added to the data, we must duplicate all the existing reports so that we can have them "with and without" the new column.

This would be hundreds or thousands of reports -- just dealing with 7 columns in the selection list, and possibly the desired order for those 7 columns.

In MS Access it seems one has a "query builder" that is essentially constructing Dynamic SQL statements based on the selections the user makes in the "query builder." In Access one can use the "SQL View" to see the SQL statement written by the "query builder."

Why does this user community seem to frown on Dynamic SQL as a way to make thousands of very similar reports (same tables JOINed together on the same columns) that merely need different columns in the SELECT and GROUP BY clauses? This would essentially be duplicating some of the operation of the "query builder" in MS Access. Is that a bad design?

What am I missing?

In the type of situation you describe, Dynamic SQL is a good option. However, I would never allow the end user to be able to type in column names, or even see the SQL code. That would open you to SQL injection.

Instead, build a form to create the SQL string. Use drop down lists and check boxes for the user to select columns and sort criteria. Build the SQL string 'behind the scene'. Do not allow the user to write anything that you put in the SQL String. If you follow such precautions, you 'may' be able to prevent the use of SQL injection by the end users, but not by anyone familiar with the code. (Upwards of 75% of economic damage comes from within...) Preventing even developers (and power users using Excel, etc.) from having the opportunity to use SQL injection would require using stored procedures.

|||

The key word is "report". For purely reporting, particularly reporting on a database that is not part of your OLTP system with live users trying to get work done, you are absolutely right. Giving users ad hoc access to write reports to your live system is just giving them a license to bring down your system. So be careful and make sure to constrain them some how (query governor, qa processes, etc.) My advice is always to provide a copy of the data (either transformed into a format for easy reporting (like OLAP), or just replicated/snapshotted) that the user can write reports to their hearts delight. If the query brings down the server, no revenue is likely lost.

But you are right, the only thing you gain by using stored procedures for reports is plan saving, and who cares? Reports may be run once a day, whereas OLTP queries are often run once a second (or more in some systems). We use stored procedures for reports only when the queries are so complex they won't fit in a single query (well, when the people writing them can't fathom how to make them into a single query :)

I definitely prefer the no dynamic SQL rule for reports that go off of the OLTP system though. The more fixed the set of queries, the more reasonable the support is, and your OLTP system is where money comes in, and that is where you get paid from. Never want to dissapoint there.

|||

Arnie,

Yes, as you mentioned, the user would select columns in some sort of drop-down list. There would be no room for SQL Injection. The User Interface in .NET would also allow the user to define the column order for the output report display, as well as the sort order for that display. The details have not yet been worked out.

Louis,

The reports I am discussing ARE for the "OLTP System." The main purpose of the OLTP system is to provide reports to users (people are producing reports more than any other sort of transaction). The current report menu tree is approx. 40 selections, each with its own stored procedure (it had been 23 reports, but we just recently added a dozen more, or so; soon we must add approx. 200 more if we don't use Dynamic SQL; each returns 2 or 3 data tables; each is maybe a 500-line stored procedure). But the users are asking for a wider variety of reports (to "slice and dice" the data different ways, to look at the same data from different perspectives, and with different levels of detail), which would cause the menu tree to expand to 10,000 entries or so, each with its own stored procedure unless we make a single stored procedure that uses Dynamic SQL.

So we could either write 5,000,000 lines of code and make a separate procedure for each report, and have the User Interface decide which of the 10,000 stored procedures to use (one per report), or we could write a few Dynamic SQL stored procedures that were each maybe 1000-1500 lines, and which could produce all 10,000 reports. (I hate to think of editing 10,000 stored procedures if we decide to rename a table, or a column in a table, or any similar maintenance activity!) (And there is also the issue of how to provide a .NET menu for the user to select from 10,000 reports, as opposed to providing merely a "select columns, display order, and sort order" for a list of 7 columns.)

These reports are run only hundreds to thousands of times per day, on a busy day; maybe dozens of times per day on a slow day. Because of the volume of the data a report may take a minute or two for the stored procedure to run and for the output to be formatted. Many users can only access a small amount of the data, so their reports run in a few seconds.

The users would not be writing any reports -- they would be merely choosing the columns they wanted in their output, which thereby defines the level of aggregation of any sums, etc., of numeric quantities. The "dynamic SQL builder" would be a stored procedure called by .NET, with the column selections and sort order as part of the input parameters.

The main advantage I see for doing this in Dynamic SQL is that it means we don't have 10,000 nearly identical stored procedures, all doing essentially the same thing -- just returning different columns, with different "group by" and "order by" clauses. Instead we have only one or two Dynamic SQL stored procedures, which build the SQL strings for the 10,000 different reports, based on user selections in the User Interface.

We get paid to provide the users with the reports they need. Different situations require different reports. How can you easily let users choose the data they want in their reports, and produce those somewhat-customized reports, without using Dynamic SQL?

|||

However, if this is a web based app, there still may be the opportunity for sQL injection.

It would be much more secure to collect the tables, columns, criteria, and sorting information into parameters, or simulated 'array' strings, and then pass those into a stored procedure and create the dynamic completely in the stored procedure.

For security purposes, I am against application embedded dynamic SQL. The DBA doens't know what's out there, and it is not easy to find and repair when business requirements force changes to the data store.

|||

>>The reports I am discussing ARE for the "OLTP System." The main purpose of the OLTP system is to provide reports to users (people are producing reports more than any other sort of transaction). <<

So are these realtime reports? Like show me exactly what is going on kinds of things? And what is the problem with returning all columns and tossing out the ones that they don't need? Are you returning that many columns with that much data?

Bottom line is that I would provide a reporting database for reporting, especially if these are not real time.

If they are real time needs, and the performance of returning 10 columns is noticably worse than 5, or 2, then dynamic SQL is not a "sin" it is just something to shy away from. I build dynamic SQL stored procedures to support some of the very same sort of things too. Just beware of the issues you can run into, is really all I think any of us are saying.

|||

Arnie,

I am not the .NET programmer, but I am sure he will not be allowing the users to enter any strings -- merely make selections from drop-down lists. So I think we will be safe from SQL Injection. We haven't yet decided on any parameter passing mechanism, or even whether we would provide these reports by a selection from a 10,000 item menu, or by having the user select columns for display, GROUP BY, and sort order.

Louis,

Yes, these are real-time reports, in the sense that a user wants to make edits to the data, and then run a report and see that the edits are reflected in the report. I suppose the user could tolerate some milliseconds of delay -- especially since it takes a while to make the selections for running a report. We are not dealing with anything like a point-of-sale system wherein the up-to-the-millisecond scanning of barcodes at the cash register might be important. Edits in the data tables are a result of numeric entries from the keyboard, so rather slow in terms of computer processing speed.

As for returning all columns and tossing out the ones they don't need, that doesn't really work since the numeric quantities must be summed according to the "GROUP BY" associated with the columns requested in the report.

The main performance issues I've noticed when returning more columns is that temporary tables used in the report procedures contain enough rows to make indexing beneficial, e.g., 200,000 rows. We will implement some way of informing the user that the number of rows returned exceed the limits of an Excel spreadsheet (approx. 65,000 rows), so the user must select a smaller portion of data for the report.

I fully agree with the notion of using Dynamic SQL only when it appears as the most appropriate solution to the problem at hand.

Thanks for your input in this discussion, from both of you.

Dan

sql

Permissions when using trusted connection

HI,
I am having a little different issue. When my windows users login into sql s
erver with trustes connection, they can select the data but no update and in
sert. It said permission denied. But I have put them into DBO group or role
and have all permission on
database. What went wrong ? Please help. Many thanks.Why don't you make these users members of the db_datawriter fixed database
role? You can find more info in books online but here's an example:
EXEC sp_addrolemember 'db_datawriter', 'username'
Carlos E. Rojas
SQL Server MVP
Co-Author SQL Server 2000 programming by Example
"Eugene" <eliu@.ctisinc.com> wrote in message
news:30D10394-6085-4C6B-A268-9E3BFA75833A@.microsoft.com...
quote:

> HI,
> I am having a little different issue. When my windows users login into sql

server with trustes connection, they can select the data but no update and
insert. It said permission denied. But I have put them into DBO group or
role and have all permission on database. What went wrong ? Please help.
Many thanks.

Friday, March 23, 2012

Permissions Question

I've got a permission question for you. In SQL Server 2000, if a user
is given a read-only role (which is select to all the tables in a db)
AND another role that allows updates to those same tables, what rights
will prevail?
Toni
*** Sent via Developersdex http://www.codecomments.com ***Rights are addtive, so additional "allow" rulwes which ensure that the user
will have a greater access to the data, except the deny roles which prevent
him regardless in how many roles he is in to allow him doing something.
HTH, Jens Suessmeyer.
"Toni" wrote:

> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.codecomments.com ***
>|||Toni
Both, unless you give them DENY permission.
"Toni" <teibner@.SQLallina.com> wrote in message
news:OQNBKc5hFHA.1248@.TK2MSFTNGP12.phx.gbl...
> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.codecomments.com ***|||Thank you for the quick response. I don't have any denies so that tells
me what I need to know.
Thanks!
Toni
*** Sent via Developersdex http://www.codecomments.com ***

Permissions Question

I've got a permission question for you. In SQL Server 2000, if a user
is given a read-only role (which is select to all the tables in a db)
AND another role that allows updates to those same tables, what rights
will prevail?
Toni
*** Sent via Developersdex http://www.codecomments.com ***
Rights are addtive, so additional "allow" rulwes which ensure that the user
will have a greater access to the data, except the deny roles which prevent
him regardless in how many roles he is in to allow him doing something.
HTH, Jens Suessmeyer.
"Toni" wrote:

> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.codecomments.com ***
>
|||Toni
Both, unless you give them DENY permission.
"Toni" <teibner@.SQLallina.com> wrote in message
news:OQNBKc5hFHA.1248@.TK2MSFTNGP12.phx.gbl...
> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.codecomments.com ***
|||Thank you for the quick response. I don't have any denies so that tells
me what I need to know.
Thanks!
Toni
*** Sent via Developersdex http://www.codecomments.com ***

Permissions Question

I've got a permission question for you. In SQL Server 2000, if a user
is given a read-only role (which is select to all the tables in a db)
AND another role that allows updates to those same tables, what rights
will prevail?
Toni
*** Sent via Developersdex http://www.developersdex.com ***Rights are addtive, so additional "allow" rulwes which ensure that the user
will have a greater access to the data, except the deny roles which prevent
him regardless in how many roles he is in to allow him doing something.
HTH, Jens Suessmeyer.
"Toni" wrote:
> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***
>|||Toni
Both, unless you give them DENY permission.
"Toni" <teibner@.SQLallina.com> wrote in message
news:OQNBKc5hFHA.1248@.TK2MSFTNGP12.phx.gbl...
> I've got a permission question for you. In SQL Server 2000, if a user
> is given a read-only role (which is select to all the tables in a db)
> AND another role that allows updates to those same tables, what rights
> will prevail?
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***

Wednesday, March 21, 2012

Permissions on Views

I've created two new views along with a new login that I'd like to only have
select permission on these new views. I've created the views and granted
select permission to the new user to these views. The first view, which
invloves only one table, works fine. The second is giving me problems and I
don't quite understand why.
The 2nd view joins 2 tables, which are owned by dbo, with 2 views, which are
owned by another user. I've verified that the view references the correct
owners and I've granted select permission to the user to this view, but I
keep getting select permission denied on the 4 objects being referenced in
the view, unless I grant select permissions to the objects that I want to
restrict.
What am I missing here?
Any help is appreciated
Thanks
Mike
--
MG"MGeles" <michael.geles@.thomson.com> wrote in message
news:79C5BB72-2C87-4D86-87FC-71B4F388AF2F@.microsoft.com...
> I've created two new views along with a new login that I'd like to only
> have
> select permission on these new views. I've created the views and granted
> select permission to the new user to these views. The first view, which
> invloves only one table, works fine. The second is giving me problems and
> I
> don't quite understand why.
> The 2nd view joins 2 tables, which are owned by dbo, with 2 views, which
> are
> owned by another user. I've verified that the view references the correct
> owners and I've granted select permission to the user to this view, but I
> keep getting select permission denied on the 4 objects being referenced in
> the view, unless I grant select permissions to the objects that I want to
> restrict.
> What am I missing here?
>
The ownership chain is broken.
See
Using Ownership Chains
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adminsql/ad_security_4iyb.asp
Davidsql

Permissions on views

Although I have granted select permissions on the views in my database that
are the recordesource for reports in a visual basic application, I cannot
open the reports from the application. I get the VB error 1005 ("can't open
recordset"). It would seem to be a permissions problem because everything
else in the app works fine except the reports that are based on views. I
haven't found anything in Books on Line that has solved the problem. The D
B
was developed in sql 2000 and the compatability level for this DB in sql 200
5
is 80. I'm grateful for any help.Pam
Make sure that you connect with the "right" user from application to run
reports.
Why do you have 80 compatibilty level for SQL Server 2005?
"Pam Davey" <PamDavey@.discussions.microsoft.com> wrote in message
news:16199EB3-EC0B-4A5E-B68F-1EEF73DDDF60@.microsoft.com...
> Although I have granted select permissions on the views in my database
> that
> are the recordesource for reports in a visual basic application, I cannot
> open the reports from the application. I get the VB error 1005 ("can't
> open
> recordset"). It would seem to be a permissions problem because everything
> else in the app works fine except the reports that are based on views. I
> haven't found anything in Books on Line that has solved the problem. The
> DB
> was developed in sql 2000 and the compatability level for this DB in sql
> 2005
> is 80. I'm grateful for any help.|||Hi!
Create view with view_metadata attribute.
Micle.
"Pam Davey" <PamDavey@.discussions.microsoft.com> wrote in message
news:16199EB3-EC0B-4A5E-B68F-1EEF73DDDF60@.microsoft.com...
> Although I have granted select permissions on the views in my database
> that
> are the recordesource for reports in a visual basic application, I cannot
> open the reports from the application. I get the VB error 1005 ("can't
> open
> recordset"). It would seem to be a permissions problem because everything
> else in the app works fine except the reports that are based on views. I
> haven't found anything in Books on Line that has solved the problem. The
> DB
> was developed in sql 2000 and the compatability level for this DB in sql
> 2005
> is 80. I'm grateful for any help.|||Hi Uri-
Thank you. I am connecting with the correct user. I have the database of
interset that resides on SQL Server 2005 set to compatability level 80 so
that it will have backward compatability with SQL server 2000 on which it wa
s
developed.
"Uri Dimant" wrote:

> Pam
> Make sure that you connect with the "right" user from application to run
> reports.
> Why do you have 80 compatibilty level for SQL Server 2005?
>
> "Pam Davey" <PamDavey@.discussions.microsoft.com> wrote in message
> news:16199EB3-EC0B-4A5E-B68F-1EEF73DDDF60@.microsoft.com...
>
>|||Hi Micle-
Thank you for your input. Unfortunely, it didn't seem to make any
difference. I get the same error. Oddly, I can get to all the underlying
tables that make up the view. I just can't get to the view, even when it's
created as you suggested.
"Micle" wrote:

> Hi!
> Create view with view_metadata attribute.
> Micle.
>
> "Pam Davey" <PamDavey@.discussions.microsoft.com> wrote in message
> news:16199EB3-EC0B-4A5E-B68F-1EEF73DDDF60@.microsoft.com...
>
>|||Pam Davey (PamDavey@.discussions.microsoft.com) writes:
> Although I have granted select permissions on the views in my database
> that are the recordesource for reports in a visual basic application, I
> cannot open the reports from the application. I get the VB error 1005
> ("can't open recordset"). It would seem to be a permissions problem
> because everything else in the app works fine except the reports that
> are based on views. I haven't found anything in Books on Line that has
> solved the problem. The DB was developed in sql 2000 and the
> compatability level for this DB in sql 2005 is 80. I'm grateful for any
> help.
Do the reports work when you run it on SQL 2000? Hav you verified that the
queries work when you run them from Query Analyzer or Management Studio.
Could you post the code you are using?
I would not expect a permissons problem, unless you are doing a poor job
of handling errors from SQL server. Nevertheless, here is a kind of shot
in the dark that you can try:
GRANT VIEW DEFINITION ON SCHEMA::dbo TO <user>
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks for your help Erland. Yes, the queries work in SQL 2000 as well as
when run from the Query Analyzer and Management Studio.
There's very little code actually. I have a 3rd party report control from
Component One that renders reports from report definitions stored in an xml
file. I've checked the xml file and the various report definitions within th
e
file have the correct queries named as their recordsource.
Thanks for the "GRANT..." thought. Didn't make a difference though. Still, I
appreciate your help.
"Erland Sommarskog" wrote:

> Pam Davey (PamDavey@.discussions.microsoft.com) writes:
> Do the reports work when you run it on SQL 2000? Hav you verified that the
> queries work when you run them from Query Analyzer or Management Studio.
> Could you post the code you are using?
> I would not expect a permissons problem, unless you are doing a poor job
> of handling errors from SQL server. Nevertheless, here is a kind of shot
> in the dark that you can try:
> GRANT VIEW DEFINITION ON SCHEMA::dbo TO <user>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx
>|||Pam Davey (PamDavey@.discussions.microsoft.com) writes:
> Thanks for your help Erland. Yes, the queries work in SQL 2000 as well as
> when run from the Query Analyzer and Management Studio.
> There's very little code actually. I have a 3rd party report control
> from Component One that renders reports from report definitions stored
> in an xml file. I've checked the xml file and the various report
> definitions within the file have the correct queries named as their
> recordsource.
> Thanks for the "GRANT..." thought. Didn't make a difference though.
> Still, I appreciate your help.
I'm afraid that there is very little to work on. Maybe the best is to
contact the vendor.
All I can really suggest is to use Profiler to eavesdrop on what the
report tool sends to SQL Server. You can include Error events in
the trace, so you can see if any errors are reported.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Okay, I'll give it a try. Thank you again.
"Erland Sommarskog" wrote:

> Pam Davey (PamDavey@.discussions.microsoft.com) writes:
> I'm afraid that there is very little to work on. Maybe the best is to
> contact the vendor.
> All I can really suggest is to use Profiler to eavesdrop on what the
> report tool sends to SQL Server. You can include Error events in
> the trace, so you can see if any errors are reported.
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx
>|||"Pam Davey" <PamDavey@.discussions.microsoft.com> wrote in message
news:DEB58306-AC92-45FA-A657-A08EA6CD6D2A@.microsoft.com...[vbcol=seagreen]
> Okay, I'll give it a try. Thank you again.
> "Erland Sommarskog" wrote:
>
A good reality check would be to execute the application under an account
that's an admin (as in NT account that is a member of the sysadmin fixed
server role) on the SQL box. If it still doesn't work, the reason is not
permissions! If it does work under admin privileges, the next reality check
might be to grant all to guest; if that works revoke the last grant and
grant all to public.
Another tactic would be to write a quickie script to open a recordset on
that view, and execute it from an end-user-level login context -- that will
allow you to see any error output that the report control may be eating.
Divide and conquer, that's the game. :-)
Good Luck,
Mark
[vbcol=seagreen]

Permissions not saved

HI,
SQL2k ( SP3a) ( I also install SP3a for the tools on my
workstation)
I encounter a strange problem when I try to set permissions on VIEW
Grant select on dbo.view_name to PUBLIC
Deny insert, update, delete on dbo.view_name to PUBLIC
With QA, when I execute thoses 2 commands ( Grant and Deny), those
commands are =93completed successfully=94 BUT =85. when I go to EM / Vi=
ew /
properties / permissions, nothing have been saved for PUBLIC role. If
I change persmission by clicking , go out of EM and when I go back in,
the permissions are back the way they were before I changed them
(nothing is saved)
Also, when I execute "sp_helprotect view_name" the result is:
Server: Msg 15330, Level 11, State 1, Procedure sp_helprotect, Line 346
There are no matching rows on which to report.
I tried with a =93user =93 instead of the PUBLIC role, and I have the sam=
e
problem.
I tries using SA account, and I have the same problem
I tried working on others worksations or on the console without success
I tried re-install =93sp3a=94 for the tools on my workstation without
success.
I will try to use PROFILER tomorrow.
Is it a bug with SP3a ? Is it a collation problem ?
Any idea '
Thank you
DannyHi Danny,
I believe I know the answer to part of your information. When EXEC
sp_helprotect <view name> returns a result set "There are no matching rows
on which to report", it means that a view by the name specificed does not
exist in the current database. Double check the database name listed in
the drop down at the top of Query Analyzer. Verify the database name shown
is the database name that contains the view name.
The second part of your information is more difficult to test. If you
register a server via SQL Enterprise Manager and you register using an
account that is part of the System Admin server role, you should be able to
make any permissions changes you wish and have them stick. My suspicion is
you registered SQL Server in SQL Enterprise Manager with an account that is
not a member of the System Admin role.
Please let me know what role the account holds within SQL Server.
Thanks.
Gary Whitley
This posting is provided "AS IS" with no warranties, and confers no rights.|||Thank you Gary,
I Verified that the BD's name shown is the database name that contains th=
e view
name.
I Verified that I registered SQL Server in SQL Enterprise Manager with an=
account that is
a member of the System Admin role. I also tried to register SQL Server in=
SQL
Enterprise Manager ans QA with "SA" .
Additional information: I have 2 similars DB on the same server machine.
For the first one, persmissions are saved and I can verify with SP_Helpro=
tect
For the other one, permissions are not saved and SP_Helprotect returns a=
result set "There are no matching rows on which to report",
Tonight dbreindex and Updatestat will be executed
Thank you
Danny
"Gary Whitley [MSFT]" a =E9crit :
quote:

> Hi Danny,
> I believe I know the answer to part of your information. When EXEC
> sp_helprotect <view name> returns a result set "There are no matching r=

ows
quote:

> on which to report", it means that a view by the name specificed does n=

ot
quote:

> exist in the current database. Double check the database name listed i=

n
quote:

> the drop down at the top of Query Analyzer. Verify the database name s=

hown
quote:

> is the database name that contains the view name.
> The second part of your information is more difficult to test. If you
> register a server via SQL Enterprise Manager and you register using an
> account that is part of the System Admin server role, you should be abl=

e to
quote:

> make any permissions changes you wish and have them stick. My suspicio=

n is
quote:

> you registered SQL Server in SQL Enterprise Manager with an account tha=

t is
quote:

> not a member of the System Admin role.
> Please let me know what role the account holds within SQL Server.
> Thanks.
> Gary Whitley
> This posting is provided "AS IS" with no warranties, and confers no rig=

hts.|||Danny,
Thank you for using the Microsoft newsgroups as your source for technical
information. In this particular case the complexity of your issue will
require in-depth troubleshooting and will not be best served by newsgroup
support. Please go to;
http://support.microsoft.com/common...=fh;en-us;cntac
tms
Select your region from the map and follow the instructions for contacting
our telephone support centers in your area.
Thank you.
Gary Whitley
This posting is provided "AS IS" with no warranties, and confers no rights.

Tuesday, March 20, 2012

permissions mystery: ActiveDirectory issue?

I don't see why my user cannot see a view when she can see the tables that
underlie it and has also been granted select permission on the view.
exec sp_grantlogin [OURDOMAIN\user99]
exec sp_grantdbaccess [OURDOMAIN\user99], 'SARAH'
exec sp_addrole 'TheRole'
exec sp_addrolemember 'TheRole', 'SARAH'create view TestView
as select * from table1
inner join table2
on t1.id = t2.anotherid
grant select on table1 to TheRole
grant select on table2 to TheRole
grant select on TestView to TheRole
User SARAH can see the tables but not the view.
We're using SQL Server 2000 and Windows 2003 Server with ActiveDirectory.
Thanks
TimoI've installed Query Analyzer on the user's desktop, and she CAN see the
view. So the problem has to do with the client application (Access 2000 ADP)
and/or ActiveDirectory. Our domain admin put the Access 2000 ADP on his PC
and he can see the view fine.
Timo
"Timo" <Timo@.unspam.biz> wrote in message
news:eRc2XxAeFHA.2420@.TK2MSFTNGP12.phx.gbl...
> I don't see why my user cannot see a view when she can see the tables that
> underlie it and has also been granted select permission on the view.
> exec sp_grantlogin [OURDOMAIN\user99]
> exec sp_grantdbaccess [OURDOMAIN\user99], 'SARAH'
> exec sp_addrole 'TheRole'
> exec sp_addrolemember 'TheRole', 'SARAH'create view TestView
> as select * from table1
> inner join table2
> on t1.id = t2.anotherid
>
> grant select on table1 to TheRole
> grant select on table2 to TheRole
> grant select on TestView to TheRole
> User SARAH can see the tables but not the view.
> We're using SQL Server 2000 and Windows 2003 Server with ActiveDirectory.
> Thanks
> Timo
>

Monday, March 12, 2012

permissions for new user to use stored procedure

Using SS2000 SP4. If I have a table and revoke select, insert, update and
delete to public on the table. Then create a new user and don't give them an
y
permissions to the table. If I create stored procedures to select from,
insert into, update and delete from the table do I only have to give execute
permissions on the stored procedure to the new user for the user to be able
to execute those stored procedures? Am I correct in saying that the new user
doesn't have to have any kind of permissions on the table itself?
Thanks,
--
Dan D.That's correct. Indeed, you didn't need to revoke or deny anything on the
underlying tables, since a user has no permissions on an object by default.
All you have to do is grant EXEC permission on the proc.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:D2083411-5A89-4F04-B1A5-373403FE75EC@.microsoft.com...
Using SS2000 SP4. If I have a table and revoke select, insert, update and
delete to public on the table. Then create a new user and don't give them
any
permissions to the table. If I create stored procedures to select from,
insert into, update and delete from the table do I only have to give execute
permissions on the stored procedure to the new user for the user to be able
to execute those stored procedures? Am I correct in saying that the new user
doesn't have to have any kind of permissions on the table itself?
Thanks,
--
Dan D.|||Wouldn't the user have permissions on the table because they are in the
'public' role by default?
Also, is the same true with views? If the user has select permission to the
view they don't need select on the underlying table?
Thanks,
--
Dan D.
"Tom Moreau" wrote:

> That's correct. Indeed, you didn't need to revoke or deny anything on the
> underlying tables, since a user has no permissions on an object by default
.
> All you have to do is grant EXEC permission on the proc.
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON Canada
> ..
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:D2083411-5A89-4F04-B1A5-373403FE75EC@.microsoft.com...
> Using SS2000 SP4. If I have a table and revoke select, insert, update and
> delete to public on the table. Then create a new user and don't give them
> any
> permissions to the table. If I create stored procedures to select from,
> insert into, update and delete from the table do I only have to give execu
te
> permissions on the stored procedure to the new user for the user to be abl
e
> to execute those stored procedures? Am I correct in saying that the new us
er
> doesn't have to have any kind of permissions on the table itself?
> Thanks,
> --
> Dan D.
>|||Let's say you create a table. By default, there are no permissions on the
table - even to the public role. Same goes for any other object.
You can grant permission on a view without granting permission on the
underlying tables. (This gets messed up if you're not the owner of the
underlying tables, though.)
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"Dan D." <DanD@.discussions.microsoft.com> wrote in message
news:EC40014C-0943-4CC3-9FDD-BFE610495BFD@.microsoft.com...
Wouldn't the user have permissions on the table because they are in the
'public' role by default?
Also, is the same true with views? If the user has select permission to the
view they don't need select on the underlying table?
Thanks,
--
Dan D.
"Tom Moreau" wrote:

> That's correct. Indeed, you didn't need to revoke or deny anything on the
> underlying tables, since a user has no permissions on an object by
> default.
> All you have to do is grant EXEC permission on the proc.
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON Canada
> ..
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:D2083411-5A89-4F04-B1A5-373403FE75EC@.microsoft.com...
> Using SS2000 SP4. If I have a table and revoke select, insert, update and
> delete to public on the table. Then create a new user and don't give them
> any
> permissions to the table. If I create stored procedures to select from,
> insert into, update and delete from the table do I only have to give
> execute
> permissions on the stored procedure to the new user for the user to be
> able
> to execute those stored procedures? Am I correct in saying that the new
> user
> doesn't have to have any kind of permissions on the table itself?
> Thanks,
> --
> Dan D.
>|||I understand. Thanks.
--
Dan D.
"Tom Moreau" wrote:

> Let's say you create a table. By default, there are no permissions on the
> table - even to the public role. Same goes for any other object.
> You can grant permission on a view without granting permission on the
> underlying tables. (This gets messed up if you're not the owner of the
> underlying tables, though.)
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON Canada
> ..
> "Dan D." <DanD@.discussions.microsoft.com> wrote in message
> news:EC40014C-0943-4CC3-9FDD-BFE610495BFD@.microsoft.com...
> Wouldn't the user have permissions on the table because they are in the
> 'public' role by default?
> Also, is the same true with views? If the user has select permission to th
e
> view they don't need select on the underlying table?
> Thanks,
> --
> Dan D.
>
> "Tom Moreau" wrote:
>
>

Friday, March 9, 2012

Permissions

Hi
How to solve next problem
a) I have database TEST
b) Login and user - webguest
Now i need set SELECT permission to all tables and views and INSERT,UPDATE,
DELETE perimissions
on couple tables.
I have tried management studio but no luck )
Regards;
Red
Hi
Is 'webguest' member of db_owner database role? Add him to this role
"Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and
> INSERT,UPDATE, DELETE perimissions
> on couple tables.
> I have tried management studio but no luck )
>
> Regards;
> Red
>
|||yes webguest is db_owner
but setting all permissions manually is veeeery hard there are over 100
tables in db.
how to set GRANT SELECT all tables and DENY UPDATE,INSERT,DELETE to all
tables
and then manually set GRANT INSERT, UPDATE, DELETE to specific tables
Red.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:elI30eFdHHA.4188@.TK2MSFTNGP02.phx.gbl...
> Hi
> Is 'webguest' member of db_owner database role? Add him to this role
> "Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
> news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
>
|||You can run the following select statement within the database you wish to
grant the permission to - then execute the resulting code.
select 'grant select,insert,update,delete on ' + name + ' to webguest' +
char(13) + ';' from sysobjects where xtype in( 'U','V')
Thanks,
Scott H.
"Redivivus" wrote:

> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and INSERT,UPDATE,
> DELETE perimissions
> on couple tables.
> I have tried management studio but no luck )
>
> Regards;
> Red
>
>

Permissions

Hi
How to solve next problem
a) I have database TEST
b) Login and user - webguest
Now i need set SELECT permission to all tables and views and INSERT,UPDATE,
DELETE perimissions
on couple tables.
I have tried management studio but no luck )
Regards;
RedHi
Is 'webguest' member of db_owner database role? Add him to this role
"Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and
> INSERT,UPDATE, DELETE perimissions
> on couple tables.
> I have tried management studio but no luck )
>
> Regards;
> Red
>|||yes webguest is db_owner
but setting all permissions manually is veeeery hard there are over 100
tables in db.
how to set GRANT SELECT all tables and DENY UPDATE,INSERT,DELETE to all
tables
and then manually set GRANT INSERT, UPDATE, DELETE to specific tables
Red.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:elI30eFdHHA.4188@.TK2MSFTNGP02.phx.gbl...
> Hi
> Is 'webguest' member of db_owner database role? Add him to this role
> "Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
> news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
>|||You can run the following select statement within the database you wish to
grant the permission to - then execute the resulting code.
select 'grant select,insert,update,delete on ' + name + ' to webguest' +
char(13) + ';' from sysobjects where xtype in( 'U','V')
--
Thanks,
Scott H.
"Redivivus" wrote:

> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and INSERT,UPDATE
,
> DELETE perimissions
> on couple tables.
> I have tried management studio but no luck )
>
> Regards;
> Red
>
>

Permissions

All,
How can we grant the permissions to the user to SELECT from some view on one
database which is pulling the data from several tables on another database
but hide the structure of these tables for this user? The problem is the
following, if we don't grant the permission to SELECT from these tables then
the user can't run these views, but if the user can select from these tables
then he sees the table structure, that's what we wanted to avoid.
The view itself is encrypted, so it's safe to expose it to the user.
Any good ideas?
Just D.Might consider creating the view in the database where the tables exist and
then reference the view as database.owner.view Also, you might consider
using a stored procedure to encapsulate the SELECTs if more logic or
auditing is required. And TMK, I don't think SQLs built-in WITH ENCRYPTION
is completely secure.
HTH
Jerry
"Dmitri Shvetsov" <no@.spam.please> wrote in message
news:CL3dg.20313$XV5.19438@.fed1read10...
> All,
> How can we grant the permissions to the user to SELECT from some view on
> one database which is pulling the data from several tables on another
> database but hide the structure of these tables for this user? The problem
> is the following, if we don't grant the permission to SELECT from these
> tables then the user can't run these views, but if the user can select
> from these tables then he sees the table structure, that's what we wanted
> to avoid.
> The view itself is encrypted, so it's safe to expose it to the user.
> Any good ideas?
> Just D.
>|||If you're using SQL 2000 SP3 or newer, you can enable cross-database
ownership chaining. Check out BOL for implementing and security caveats.
HTH
Vern Rabe
"Jerry Spivey" wrote:

> Might consider creating the view in the database where the tables exist an
d
> then reference the view as database.owner.view Also, you might consider
> using a stored procedure to encapsulate the SELECTs if more logic or
> auditing is required. And TMK, I don't think SQLs built-in WITH ENCRYPTIO
N
> is completely secure.
> HTH
> Jerry
> "Dmitri Shvetsov" <no@.spam.please> wrote in message
> news:CL3dg.20313$XV5.19438@.fed1read10...
>
>|||Dima
Add the user to the "source database" and GRANT them SELECT permissions on
VIEW
"Dmitri Shvetsov" <no@.spam.please> wrote in message
news:CL3dg.20313$XV5.19438@.fed1read10...
> All,
> How can we grant the permissions to the user to SELECT from some view on
> one database which is pulling the data from several tables on another
> database but hide the structure of these tables for this user? The problem
> is the following, if we don't grant the permission to SELECT from these
> tables then the user can't run these views, but if the user can select
> from these tables then he sees the table structure, that's what we wanted
> to avoid.
> The view itself is encrypted, so it's safe to expose it to the user.
> Any good ideas?
> Just D.
>|||Uri,
Thanks for your answer but that doesn't work, the SQL engine barks when I
try to select anything from these views explaining that the user has no
permissions on the target DB or if even if it doesn't complaint then it
doesn't show anything, just column names and no data at all. So it doesn't
work. That was the very first idea that I tried. Adding the SP and granting
EXEC permissions to these SP is not very good because it's not flexible
enough, although we can add some arguments to these SPs to restrict the data
to avoid pulling the whole DB.
Btw, I found this article below after I posted my question to this
newsgroup.
http://www.databasejournal.com/feat...10894_2246271_2
Maybe this is the way to go, we'll try it tomorrow. It's a little bit
similar to what we were doing, but who knows, there are always some ways
ahead.
Granting the permissions per column for each table will kill me, I tried to
think about it but this is the worst way reserved for "just in case".
Just D.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:epcKo37fGHA.452@.TK2MSFTNGP02.phx.gbl...
> Dima
> Add the user to the "source database" and GRANT them SELECT permissions
> on VIEW
>|||Yes, have you read my post?
I said you need to add the user to the taget database and GRANT to them
SELECT PERMISSION on VIEW
ps . create a view on target database as well
"Just D" <no@.spam.please> wrote in message
news:97ddg.20579$XV5.8839@.fed1read10...
> Uri,
> Thanks for your answer but that doesn't work, the SQL engine barks when I
> try to select anything from these views explaining that the user has no
> permissions on the target DB or if even if it doesn't complaint then it
> doesn't show anything, just column names and no data at all. So it doesn't
> work. That was the very first idea that I tried. Adding the SP and
> granting EXEC permissions to these SP is not very good because it's not
> flexible enough, although we can add some arguments to these SPs to
> restrict the data to avoid pulling the whole DB.
> Btw, I found this article below after I posted my question to this
> newsgroup.
> http://www.databasejournal.com/feat...10894_2246271_2
> Maybe this is the way to go, we'll try it tomorrow. It's a little bit
> similar to what we were doing, but who knows, there are always some ways
> ahead.
> Granting the permissions per column for each table will kill me, I tried
> to think about it but this is the worst way reserved for "just in case".
> Just D.
>
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:epcKo37fGHA.452@.TK2MSFTNGP02.phx.gbl...
>

Permissions

Hi
How to solve next problem
a) I have database TEST
b) Login and user - webguest
Now i need set SELECT permission to all tables and views and INSERT,UPDATE,
DELETE perimissions
on couple tables.
I have tried management studio but no luck :))
Regards;
RedHi
Is 'webguest' member of db_owner database role? Add him to this role
"Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and
> INSERT,UPDATE, DELETE perimissions
> on couple tables.
> I have tried management studio but no luck :))
>
> Regards;
> Red
>|||yes webguest is db_owner
but setting all permissions manually is veeeery hard there are over 100
tables in db.
how to set GRANT SELECT all tables and DENY UPDATE,INSERT,DELETE to all
tables
and then manually set GRANT INSERT, UPDATE, DELETE to specific tables
Red.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:elI30eFdHHA.4188@.TK2MSFTNGP02.phx.gbl...
> Hi
> Is 'webguest' member of db_owner database role? Add him to this role
> "Redivivus" <meelis.lilbok@.deltmar.ee> wrote in message
> news:uGnVUsEdHHA.3632@.TK2MSFTNGP02.phx.gbl...
> > Hi
> >
> > How to solve next problem
> >
> > a) I have database TEST
> > b) Login and user - webguest
> > Now i need set SELECT permission to all tables and views and
> > INSERT,UPDATE, DELETE perimissions
> > on couple tables.
> >
> > I have tried management studio but no luck :))
> >
> >
> > Regards;
> > Red
> >
>|||You can run the following select statement within the database you wish to
grant the permission to - then execute the resulting code.
select 'grant select,insert,update,delete on ' + name + ' to webguest' +
char(13) + ';' from sysobjects where xtype in( 'U','V')
--
Thanks,
Scott H.
"Redivivus" wrote:
> Hi
> How to solve next problem
> a) I have database TEST
> b) Login and user - webguest
> Now i need set SELECT permission to all tables and views and INSERT,UPDATE,
> DELETE perimissions
> on couple tables.
> I have tried management studio but no luck :))
>
> Regards;
> Red
>
>

Wednesday, March 7, 2012

Permission to view system tables in master database

I am trying to avoid the "Permission denied" error message when a proc tries
to select records from one of the system tables in a SQL Server 2005 master
database.
Sepcifically I need to be able to capture the IP address of the calling user
which I can get from sys.dm_exec_connections.
I have struggled with this for some time and could not find anything
directly on point in MSDN. However, I was able to cobble something together
that "appears" to work as shown below.
I am hoping that someone who has specific experience with this issue might
be able to comment on my approach and point out any issues or deficiencies.
Thanks
Dave
/*
Creating a proc that can view server state
1. Create a special login and grant it VIEW SERVER STATE permissions in
Master DB
2. Create user for special login in the User DB
3. Create proc that accesses system tables using "WITH EXECUTE AS" the
special user
4. Create a cerificate in the User DB
5. Sign the proc with the cerificate
6. Create the cerificate from backup in the Master DB
7. Create an authorization login mapped to the certificate in the Master DB
8. Grant AUTHENTICATE SERVER to the authorization login in the Master DB
9. Grant EXECUTE perms on the proc to users in the User DB
*/
--0. Create a test database
USE master
CREATE DATABASE testcert
GO
--1. Create a special login and grant it VIEW SERVER STATE permissions in
Master DB
CREATE LOGIN myViewUser WITH PASSWORD = '!Pa55word!' ;
GO
USE master
GO
GRANT VIEW ANY DEFINITION TO myViewUser ;
GO
GRANT VIEW SERVER STATE TO myViewUser ;
GO
--
--2. Create user for special login in the User DB
USe testCert
GO
CREATE USER myViewUser FOR LOGIN myViewUser ;
GO
--
--3. Create proc that accesses system tables using "WITH EXECUTE AS" the
special user
IF object_id('myPROC') IS NOT NULL
DROP PROCEDURE myPROC
GO
CREATE PROCEDURE myPROC
WITH EXECUTE AS 'myViewUser'
AS
BEGIN
DECLARE @.ip Varchar(60)
SELECT @.ip=client_net_address
FROM sys.dm_exec_connections
WHERE session_id=@.@.spid
PRINT @.ip
END
GO
--
--Test: cannot execute - The user does not have permission to perform this
action.
EXEC myPROC
--4. Create a cerificate in the User DB
CREATE CERTIFICATE myCERT
ENCRYPTION BY PASSWORD = '!Pa55word!'
WITH SUBJECT = 'Test cert',
EXPIRY_DATE = '12/05/2010';
GO
BACKUP CERTIFICATE myCERT TO FILE = 'myCERT.cer' ;
--5. Sign the proc with the cerificate
ADD SIGNATURE TO myPROC
BY CERTIFICATE myCERT
WITH PASSWORD = '!Pa55word!'
GO
--6. Create the cerificate from backup in the Master DB
USE master --Permissions at the server scope can only be granted when the
current database is master
CREATE CERTIFICATE myCert FROM FILE = 'myCERT.cer'
--7. Create an authorization login mapped to the certificate in the Master D
B
CREATE LOGIN myAuthLogin
FROM CERTIFICATE mycert ;
GO
--
--8. Grant AUTHENTICATE SERVER to the authorization login in the Master DB
-- WIEW permissions are server-level permissions, grant AUTHENTICATE SERVER
to certificate mapped login
GRANT AUTHENTICATE SERVER TO myAuthLogin
GO
--test
USE testCert
--I can execute proc
EXEC myPROC
--create some users
CREATE LOGIN testuser1 WITH PASSWORD = '!Pa55word!' ;
CREATE USER testuser1 FOR LOGIN testuser1 ;
CREATE LOGIN testuser2 WITH PASSWORD = '!Pa55word!' ;
CREATE USER testuser2 FOR LOGIN testuser2 ;
--test
execute as login = 'testuser1'
select suser_name()
--cannot execute: EXECUTE permission denied on object 'myPROC', database
'testcert', schema 'dbo'.
EXEC myPROC
revert
--
--9. Grant EXECUTE perms on the proc to users in the User DB
GRANT EXECUTE ON myPROC TO testuser1
--test again
execute as login = 'testuser1'
select suser_name()
--now can execute
EXEC myPROC
revert
execute as login = 'testuser2'
select suser_name()
--but others cabnnnot until granted perms on the proc
EXEC myPROC
revert
--Clean UP
USE testCert
GO
DROP USER testuser1
DROP LOGIN testuser1
GO
DROP USER testuser2
DROP LOGIN testuser2
GO
DROP PROC myPROC
GO
DROP USER myViewUser
DROP LOGIN myViewUser
GO
DROP CERTIFICATE myCERT
GO
USE master
GO
DROP LOGIN myAuthLogin
DROP CERTIFICATE myCERT
GO
DROP DATABASE testcert
GO
EXEC sp_configure 'show advanced options', 1 ;
GO
RECONFIGURE ;
GO
EXEC sp_configure 'xp_cmdshell', 1 ;
GO
RECONFIGURE ;
GO
--EXEC xp_cmdshell 'DIR "D:\SQLDATA\MSSQL.1\*.cer"' ;
--EXEC xp_cmdshell 'del "D:\SQLDATA\MSSQL.1\myCert.cer"' ;
EXEC xp_cmdshell 'DIR "C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\*.cer"' ;
EXEC xp_cmdshell 'del "C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\myCert.cer"' ;
EXEC xp_cmdshell 'DIR "C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\*.cer"' ;
GO
EXEC sp_configure 'xp_cmdshell', 0 ;
GO
RECONFIGURE ;After further searching I found a cleaner way to accomplish what I need to
do. See example below.
Although I am not entirely sure why this line is necessary:
-- now that we signed the procedure, we can drop the private key
alter certificate certSignCreatePrincipal remove private key;
/*
Laurentiu Cristofor's blog
SQL Server 2005: procedure signing demo
http://blogs.msdn.com/lcris/archive.../15/429631.aspx
*/
create database demo;
use demo;
CREATE LOGIN alice WITH PASSWORD = '!Pa55word!' ;
CREATE USER alice FOR LOGIN alice
-- create a procedure that selects from sys.dm_exec_connections
create procedure sp_CreatePrincipal
AS
DECLARE @.ip Varchar(60)
SELECT @.ip=client_net_address
FROM sys.dm_exec_connections
WHERE session_id=@.@.spid
PRINT @.ip
go
-- now use this newly added procedure
-- to create a low privileged principal
EXEC sp_CreatePrincipal
-- we'll now want alice to be able to use the procedure and create new
principals
-- but without granting her directly the permissions
grant execute on sp_CreatePrincipal to alice;
-- verify that alice cannot create principals
execute as login = 'alice';
EXEC sp_CreatePrincipal
revert;
-- first, we'll need to create a database master key
create master key encryption by password = 'Apufe@.))%';
-- create a certificate to sign the procedure
create certificate certSignCreatePrincipal with subject = 'for signing
procedure sp_CreatePrincipal';
-- sign procedure sp_CreatePrincipal
add signature to sp_CreatePrincipal by certificate certSignCreatePrincipal;
-- now that we signed the procedure, we can drop the private key
alter certificate certSignCreatePrincipal remove private key;
-- backup certificate to file; it will be used later to put the certificate
in master
backup certificate certSignCreatePrincipal to file =
'certSignCreatePrincipal.cer';
-- create and map a user to the certificate
--? create user u_certSignCreatePrincipal from certificate
certSignCreatePrincipal;
-- create the same certificate in master now
use master;
create certificate certSignCreatePrincipal from file =
'certSignCreatePrincipal.cer';
-- create and map a login to the certificate
create login l_certSignCreatePrincipal from certificate
certSignCreatePrincipal;
GRANT VIEW SERVER STATE TO l_certSignCreatePrincipal;
-- we're done!
use demo;
-- check that the certificate in demo matches the one in master
select c.name
from sys.certificates c
JOIN master.sys.certificates mc ON c.thumbprint = mc.thumbprint;
-- verify that alice can now create principals
execute as login = 'alice';
sp_CreatePrincipal
revert;
-- cleanup
drop user u_certSignCreatePrincipal;
drop login l_certSignCreatePrincipal;
drop procedure sp_CreatePrincipal;
drop certificate certSignCreatePrincipal;
drop user alice;
drop login alice;
drop user bob;
drop login bob;
EXEC sp_configure 'xp_cmdshell', 1 ;
RECONFIGURE ;
EXEC xp_cmdshell 'del "C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\certSignCreatePrincipal.cer"' ;
EXEC xp_cmdshell 'DIR "C:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\Data\*.cer"' ;
use master;
drop certificate certSignCreatePrincipal;
drop database demo;|||Dave (Dave@.discussions.microsoft.com) writes:
> I am trying to avoid the "Permission denied" error message when a proc
> tries to select records from one of the system tables in a SQL Server
> 2005 master database.
> Sepcifically I need to be able to capture the IP address of the calling
> user which I can get from sys.dm_exec_connections.
> I have struggled with this for some time and could not find anything
> directly on point in MSDN. However, I was able to cobble something
> together that "appears" to work as shown below.
> I am hoping that someone who has specific experience with this issue
> might be able to comment on my approach and point out any issues or
> deficiencies.
Yes, you have to do something you did, although you don't the EXECUTE
AS thing. Certificate signing is all you need. I have an article on
my web site that show how you can do this:
http://www.sommarskog.se/grantperm.html
Since you seem to have grasped some of this already, you may want to
rush through until the first BULK INSERT example.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks Erland Sommarskog.
That is a very useful web site at http://www.sommarskog.se/
However, one thing I am not clear on; why do you back up the private key and
Laurentiu drops it? What is the risk of dropping it like he did?
Erland Sommarskog's example:
BACKUP CERTIFICATE reloadcert TO FILE = 'C:\temp\reloadcert.cer'
WITH PRIVATE KEY (FILE = 'C:\temp\reloadcert.pvk' ,
ENCRYPTION BY PASSWORD = 'Tomorrow never knows',
DECRYPTION BY PASSWORD = 'All you need is love')
Laurentiu Cristofor's example:
alter certificate certSignCreatePrincipal remove private key;
backup certificate certSignCreatePrincipal to file =
'certSignCreatePrincipal.cer';|||Dave (Dave@.discussions.microsoft.com) writes:
> That is a very useful web site at http://www.sommarskog.se/
> However, one thing I am not clear on; why do you back up the private key
> and Laurentiu drops it? What is the risk of dropping it like he did?
>
> Erland Sommarskog's example:
> BACKUP CERTIFICATE reloadcert TO FILE = 'C:\temp\reloadcert.cer'
> WITH PRIVATE KEY (FILE = 'C:\temp\reloadcert.pvk' ,
> ENCRYPTION BY PASSWORD = 'Tomorrow never knows',
> DECRYPTION BY PASSWORD = 'All you need is love')
>
> Laurentiu Cristofor's example:
> alter certificate certSignCreatePrincipal remove private key;
> backup certificate certSignCreatePrincipal to file =
> 'certSignCreatePrincipal.cer';
If I do it in one way, and Laurentiu another, Laurentiu is likely to be
right! (Unless it's the usage of EXECUTE AS, where we are known to
disagree. :-)
I will have to admit that I was not aware of the REMOVE PRIVATE KEY
clause. I used what I was able to get to work. Dropping the private key
seems to be a better idea. However, as I understand it, it calls for a
different order of things. To wit, I first create the certificate and the
login in master, export the cert, and then move to the target database
where I sign the procedure. As I need the privte key to sign, I do need
the private key at this point.
I should definitely spend some time reworking the example - I only
need to find that time. :-)
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Permission to execute fn_get_sql

Dear All,

I want to assign privelege to execute the following in SQL Server 2000 SP4 + 2187

select * from ::fn_get_sql(0x01000700AC38820138A1786C0400000000000000)

I have tried the following:-

use master

go

grant select on fn_get_sql to <user>

I am getting error:

Server: Msg 208, Level 16, State 11, Line 1
Invalid object name 'fn_get_sql'.

I believe that ::fn_get_sql is an administrative use only function, and that permission is limited to either sysadmin or db_databaseowner. (I think the former...)

|||

You require VIEW SERVER STATE permission to use it. Please refer to BOL (http://msdn2.microsoft.com/en-us/library/ms189451.aspx) for detailed information.

-Raul Garcia

SDE/T

SQL Server Engine

Saturday, February 25, 2012

Permission issues

Dear all,

I have 500 tables in a database.

How can i grant select ,update and insert permission to a user on all 500 tables of the database at once.

Thanks

Mohd Sufian

The fixed database role db_datareader will work for SELECT.

You can use a cursor. ...might want to use sp_executesql

DECLARE @.User sysname

DECLARE @.Table nvarchar(500)

DECLARE @.cmd nvarchar(2000)

SET @.User = 'MyUser'

SET @.cmd = ''

DECLARE GrantUser CURSOR

LOCAL

FAST_FORWARD

FOR

SELECT s.Name + + '.' + o.name

FROM sys.objects o

INNER JOIN sys.schemas s

ON o.schema_id =s.schema_id

WHERE type = 'U'

IF EXISTS (SELECT * FROM sys.sysusers WHERE name = @.User)

BEGIN

OPEN GrantUser

FETCH NEXT FROM GrantUser INTO @.Table

WHILE @.@.FETCH_STATUS = 0

BEGIN

SET @.cmd = ''

SET @.cmd = ' GRANT SELECT ON ' + @.Table + ' TO' + @.User

SET @.cmd = @.cmd + ' GRANT UPDATE ON ' + @.Table + ' TO' + @.User

SET @.cmd = @.cmd + ' GRANT INSERT ON ' + @.Table + ' TO' + @.User

EXEC (@.cmd)

FETCH NEXT FROM GrantUser INTO @.Table

END

CLOSE GrantUser

DEALLOCATE GrantUser

END

|||

If this is SQL SERVER 2005, you can grant all these permissions at the database level itself.

So,

GRANT INSERT ON DATABASE::Database_Name to User_Name

GRANT UPDATE ON DATABASE::Database_Name to User_Name

GRANT SELECT ON DATABASE::Database_Name to User_Name

You must be connected to the database on which you are granting the permissions

permission issue with tempdb works fine in SQL2000 but not SQL2005

the following SQL works fine in SQL2000 but gets a permissions error when run on SQL2005:

IF not exists (SELECT * FROM tempdb.dbo.sysindexes WHERE NAME = 'PK_tblGuidContractMove')

BEGIN

IF @.DEBUG = 1 PRINT 'airsp_CopyContracts.PK_tblGuidContractMove'

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidSource GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidDestination GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidContractMove GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove WITH NOCHECK ADD

CONSTRAINT [PK_tblGuidContractMove] PRIMARY KEY CLUSTERED

(

[guidSource],

[guidDestination],

[guidContractMove]

) ON [PRIMARY]')

END

The user permissions are set the same in both 2000 and 2005 can you please explain what changed and what are the minimum permissions need for the user to be able to make these changes to the temporary table which the user created.

Moving to Transact-SQL from SSIS.

permission issue with temp db

the following SQL works fine in SQL2000 but gets a permissions error when run on SQL2005:

IF not exists (SELECT * FROM tempdb.dbo.sysindexes WHERE NAME = 'PK_tblGuidContractMove')

BEGIN

IF @.DEBUG = 1 PRINT 'airsp_CopyContracts.PK_tblGuidContractMove'

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidSource GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidDestination GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove ALTER COLUMN guidContractMove GUID NOT NULL')

EXECUTE('ALTER TABLE #tblGuidContractMove WITH NOCHECK ADD

CONSTRAINT [PK_tblGuidContractMove] PRIMARY KEY CLUSTERED

(

[guidSource],

[guidDestination],

[guidContractMove]

) ON [PRIMARY]')

END

The user permissions are set the same in both 2000 and 2005 can you please explain what changed and what are the minimum permissions need for the user to be able to make these changes to the temporary table which the user created.


Can you post a repro and the exact error message. 'alter table' right must be granted on the table for the user to change the meta data.