Friday, March 30, 2012

More recursive questions

I have a list of categories that I have to find the path to each
my table is set up like
ID CategoryNum CategoryName ParentID
1 AA Top Level 1 0
2 AB Top Level 2 0
3 BA Second Level 1 1
I need my query/stored proc to return
Tope Level 1/Second Level 1 if I pass in the integer of 3
I can do this in programming but cant seem to wrap my head around it in SQL Server
TIA for the helpHi,
Is your requirement to get a Tree structure from SQL Server?
SQL Server has got FOR XML clause which will return the data in hierarchial manner. But it does have implementaiton in returning n-level tree. You have to use EXPLICIT method which will require you to do a lot of coding to get a hierarchial Tree.
May be if you can explain more on the format you would like to get the data, can help.
Thanks.|||no really just an arraylist (working in vb) of the output (Top Level 1/Second Level 1 etc. etc. ) - trying to do it on sqlserver because the recursive code (even thought the maximum level is 5) is taking too long to run
|||Dylan,
Try the link below most things you want to do with Arrays in SQL is covered by Joe Celko, browse his book in the link at your local book store. Hope this helps.
http://www.onlamp.com/pub/a/onlamp/2004/08/05/hierarchical_sql.html|||okay some of that makes sense - doing this in SQL is really really new to me - the 'Searching For Superiors' section seems to cover what I need but his example uses a path string to show the path to the node - I dont have that I am using a child sub child method
oh this is so fun|||Joe Celko is an ANSI SQL purist so what you are looking for he did not cover because he thinks most things you do in the database is covered by ANSI SQL. This new link I have given you before he uses T-SQL and there is code files you can download to play with. BTW click on 2000 so you don't have to go through old code for earlier versions. Hope this helps
http://www.sommarskog.se/arrays-in-sql.html#iterative|||

You can always utilize recursion within self-joining tables. For example a function to "expand" the hierarchy (assuming the table name is CATEGORIES)

CREATE Function [dbo].[GetParent](@.currentid int,@.includestartnode bit)
returns @.results TABLE(id int,orderid int)
AS

BEGIN

DECLAre @.level int;
SET @.level=-1


IF @.includestartnode=1
BEGIN
INSERT INTO @.results
VALUES(@.currentid,0)
END


WHILE EXISTS (SELECT parentid FROM CATEGORIES WHEREid=@.currentid)
BEGIN

SELECT @.currentid=parentId
FROM CATEGORIES
WHEREid=@.currentid

IF @.@.ROWCOUNT=0
BEGIN
BREAK
END

INSERT INTO @.results(id,orderid)
SELECT @.currentid, @.level
FROM CATEGORIES
WHEREid=@.currentid

SET @.level=@.level - 1

END

RETURN

END

Using this with a call:

SELECT idFROM dbo.GetParent(3,1)

ORDERby orderid

would return
ID
===
1
3
As you can see it returns the ID list of the hierarchical nodes so it is rather generic function (returns nodes who have parent-child relationship) going with "bottom-to-up" principle. You could utilize this in getting the "path"
==
DECLARE @.pathnvarchar(1000)

SELECT @.path=COALESCE(@.path+'/','')+

+CAST(CategoryNameasnvarchar)

from CATEGORIES cINNERJOIN dbo.GetParent(3,1) tmp

ON c.id=tmp.id

ORDERBY orderid

select @.path


Returns

Top level 1/Second level 1

Note that of course, if you want, you can combine getting the data into the function or just on plain proc to do the job. E.g you can do the recutrsive stuff which I here do in function, also in stored proc if you want to.

More Questions....Relationships

Ok heres the scenario:

3 tables

Movie - MovieID, MovieName, Starring1, Starring 2, Director

Director - MovieID, DirectorID, DirectorName

Actors - MovieID, ActorName

In bold are the names of the tables --

in bold+italic are the PK - Primary Keys.

Ive tried to create a relationship between:

1. Movie - MovieID to Director -MovieID - Movie is related to the director

2. Actors - MovieID to Movie-MovieID - Actor is related to Movie

Second relationship failed for some reason.

Im trying to achieve this result:

Movie = Pulp Fiction

Director = Quentin Tan

Actor = Sam L Jackson

Once i do reports etc.....What am i doing wrong here as i think its my relationships im having difficulty with?

Thank you in advance

try:

Select movieName,

DirectorName,

actorName

from movie m

join director d

on m.movieId = d.movieId

and m.movieName = 'Pulp Fiction'

and d.directorName = 'Quentin Tan'

join actor a

on m.movieId = a.movieId

and a.actorName = 'Sam L Jackson'

an alternative is:

Select movieName,

DirectorName,

actorName

from movie m

join director d

on m.movieId = d.movieId

join actor a

on m.movieId = a.movieId

where m.movieName = 'Pulp Fiction'

and d.directorName = 'Quentin Tan'

and a.actorName = 'Sam L Jackson'

|||Thanks but am i on the right lines about creating these relationships in the way i described or would you do anything differently?|||

Assuming that a movie has only one director, something like this:

Director -- DirectorID (PK), DirecorName

Actor -- ActorID (PK), ActorName

Movie -- MovieID (PK), MovieName, DirectorID

Cast -- MovieID, Order, Part, ActorID, (PK: MovieID, ActorID, Part)

The "Cast" table is necessary so that you can list all of the actors of the movie. I added in "Order" so that you can control "Promotion Order" or "Billing Order" or whatever you want to call it -- it is a control so that you can control the order in which the actors might be listed in credits.

I added in "Part" for movies in which somebody like Eddie Murphy might play multiple parts.

From my perspective the biggest problem here is the "Cast" table as I have made it. I still don't feel that it is general enough. For instance, say that you want to use this as the basis for listing movie credits. You want to include Producers, stunt coordinators, etc. It would be best to generalize the "Cast" table into a "Role" table and along the way establish "Subcategories" of the "Role" table. But now we are getting ahead of ourselves. This should get you started.

More Questions on Permission

Thanks Hari, That helps a lot.
Do I have a way to know previlage details about an Object?
such as is the SELECT on a table been Granted, revoked or
denied?
Greg Chang
Subject: Re: Questions about syspermissions
From: "Hari" <hari_prasad_k@.hotmail.com> Sent:
6/24/2004 9:29:17 PM
HI,
Have a look into sysprotects table, column action
contains the granted
previlages for each objects.
The explanation for Action column:-
26 = REFERENCES
178 = CREATE FUNCTION
193 = SELECT
195 = INSERT
196 = DELETE
197 = UPDATE
198 = CREATE TABLE
203 = CREATE DATABASE
207 = CREATE VIEW
222 = CREATE PROCEDURE
224 = EXECUTE
228 = BACKUP DATABASE
233 = CREATE DEFAULT
235 = BACKUP LOG
236 = CREATE RULE
Simply you can execute the below system procedure to get
the objct level
previlages:-
sp_helprotect <object_name>
Thanks
Hari
MCDBA
"Greg Chang" <anonymous@.discussions.microsoft.com> wrote
in message
news:210fa01c45a0e$23130eb0$a501280a@.phx.gbl...
> is syspermissions table contains the Permissions of
> delete, select, insert, update, execute and DRI on all
> the objects?
> how do I use this syspermissions table? For example,
> which column stored the INSERT Privileges?
> thanks
> Greg Chang
..
See the BOL for information about the sp_helprotect stored procedure.
Jim
"Greg Chang" <anonymous@.discussions.microsoft.com> wrote in message
news:21e9701c45d1b$c99a0170$a601280a@.phx.gbl...
> Thanks Hari, That helps a lot.
> Do I have a way to know previlage details about an Object?
> such as is the SELECT on a table been Granted, revoked or
> denied?
> Greg Chang
>
> Subject: Re: Questions about syspermissions
> From: "Hari" <hari_prasad_k@.hotmail.com> Sent:
> 6/24/2004 9:29:17 PM
> HI,
> Have a look into sysprotects table, column action
> contains the granted
> previlages for each objects.
> The explanation for Action column:-
> 26 = REFERENCES
> 178 = CREATE FUNCTION
> 193 = SELECT
> 195 = INSERT
> 196 = DELETE
> 197 = UPDATE
> 198 = CREATE TABLE
> 203 = CREATE DATABASE
> 207 = CREATE VIEW
> 222 = CREATE PROCEDURE
> 224 = EXECUTE
> 228 = BACKUP DATABASE
> 233 = CREATE DEFAULT
> 235 = BACKUP LOG
> 236 = CREATE RULE
>
> Simply you can execute the below system procedure to get
> the objct level
> previlages:-
> sp_helprotect <object_name>
> --
> Thanks
> Hari
> MCDBA
> "Greg Chang" <anonymous@.discussions.microsoft.com> wrote
> in message
> news:210fa01c45a0e$23130eb0$a501280a@.phx.gbl...
>
> .
>
|||What is BOL?

>--Original Message--
>See the BOL for information about the sp_helprotect
stored procedure.
>Jim
>"Greg Chang" <anonymous@.discussions.microsoft.com> wrote
in message[vbcol=seagreen]
>news:21e9701c45d1b$c99a0170$a601280a@.phx.gbl...
Object?[vbcol=seagreen]
or[vbcol=seagreen]
get[vbcol=seagreen]
wrote[vbcol=seagreen]
all
>
>.
>
|||See the BOL for information about the sp_helprotect stored procedure.
Jim
"Greg Chang" <anonymous@.discussions.microsoft.com> wrote in message
news:21e9701c45d1b$c99a0170$a601280a@.phx.gbl...
> Thanks Hari, That helps a lot.
> Do I have a way to know previlage details about an Object?
> such as is the SELECT on a table been Granted, revoked or
> denied?
> Greg Chang
>
> Subject: Re: Questions about syspermissions
> From: "Hari" <hari_prasad_k@.hotmail.com> Sent:
> 6/24/2004 9:29:17 PM
> HI,
> Have a look into sysprotects table, column action
> contains the granted
> previlages for each objects.
> The explanation for Action column:-
> 26 = REFERENCES
> 178 = CREATE FUNCTION
> 193 = SELECT
> 195 = INSERT
> 196 = DELETE
> 197 = UPDATE
> 198 = CREATE TABLE
> 203 = CREATE DATABASE
> 207 = CREATE VIEW
> 222 = CREATE PROCEDURE
> 224 = EXECUTE
> 228 = BACKUP DATABASE
> 233 = CREATE DEFAULT
> 235 = BACKUP LOG
> 236 = CREATE RULE
>
> Simply you can execute the below system procedure to get
> the objct level
> previlages:-
> sp_helprotect <object_name>
> --
> Thanks
> Hari
> MCDBA
> "Greg Chang" <anonymous@.discussions.microsoft.com> wrote
> in message
> news:210fa01c45a0e$23130eb0$a501280a@.phx.gbl...
>
> .
>
|||BOL = Books OnLine, which is the documentation for SQL Server. You can find
it in the SQL Server program group, and you can download the latest updated
version from www.microsoft.com/sql
Jacco Schalkwijk
SQL Server MVP
"Greg Chang" <anonymous@.discussions.microsoft.com> wrote in message
news:2239e01c45d22$4877f9b0$a301280a@.phx.gbl...[vbcol=seagreen]
> What is BOL?
> stored procedure.
> in message
> Object?
> or
> get
> wrote
> all
|||What is BOL?

>--Original Message--
>See the BOL for information about the sp_helprotect
stored procedure.
>Jim
>"Greg Chang" <anonymous@.discussions.microsoft.com> wrote
in message[vbcol=seagreen]
>news:21e9701c45d1b$c99a0170$a601280a@.phx.gbl...
Object?[vbcol=seagreen]
or[vbcol=seagreen]
get[vbcol=seagreen]
wrote[vbcol=seagreen]
all
>
>.
>
|||hi Greg,
"Greg Chang" <anonymous@.discussions.microsoft.com> ha scritto nel messaggio
news:2239e01c45d22$4877f9b0$a301280a@.phx.gbl...
> What is BOL?
BOL stands for Books On Line, the official on-line guide for SQL Server, you
can download, in localized version at
http://www.microsoft.com/sql/techinf...2000/books.asp
it's the best documentation ever about SQL Server...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.8.0 - DbaMgr ver 0.54.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||BOL = Books OnLine, which is the documentation for SQL Server. You can find
it in the SQL Server program group, and you can download the latest updated
version from www.microsoft.com/sql
Jacco Schalkwijk
SQL Server MVP
"Greg Chang" <anonymous@.discussions.microsoft.com> wrote in message
news:2239e01c45d22$4877f9b0$a301280a@.phx.gbl...[vbcol=seagreen]
> What is BOL?
> stored procedure.
> in message
> Object?
> or
> get
> wrote
> all
|||hi Greg,
"Greg Chang" <anonymous@.discussions.microsoft.com> ha scritto nel messaggio
news:2239e01c45d22$4877f9b0$a301280a@.phx.gbl...
> What is BOL?
BOL stands for Books On Line, the official on-line guide for SQL Server, you
can download, in localized version at
http://www.microsoft.com/sql/techinf...2000/books.asp
it's the best documentation ever about SQL Server...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.8.0 - DbaMgr ver 0.54.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

More questions about porting from MySQL to MS SQL

1) In several tables, in my MySQL version, I created columns using
something like the following:

`ab_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,

This allowed me to ensure that when a record is either added or edited,
the value in the field is set to the current date and time. I.E.,
ab_timestamp is given the current date and time when a record is
created, and then it is updated to the date and time at which the
record is updated. I learned the hard way that MS SQL does not like
"on update CURRENT_TIMESTAMP". So, it looks like MS SQL will allow me
to initialize ab_timestamp to the current date and time, but not
automatically update it to the date and time at which the record is
updated. I have plenty of code to port that depends on the behaviour
supported by MySQL. DO I have to modify all that code, or is there a
way to get MS SQL to provide it? (Yes, I know 'timestamp' is
deprecated in MS SQL and that I should use datetime instead, and in
fact have already done so.)

2) I began with a single SQL script that creates all the tables, views,
functions and triggers the database needs. On trying to get MS SQL to
accept it, I encountered a number of error messages saying that CREATE
FUNCTION and CREATE VIEW need to be the first statement in a script.
Why? I know I can work around this odd constraint by putting each
function and view (and IIRC trigger) into its own script, but that
seems like a make work effort imposed for some unknown reason by MS
SQL, unless there is another way to get around it.

3) I see, in the documentation for CREATE FUNCTION, functions are not
allowed to use a timestamp for either a parameter or a return value.
This is in reference to a pair of scalar functions I am using which
need to manipulate date and time values. For the purpose of
clarification, is this documentation refering to all date/time data
types, or only the deprecated timestamp type? As examples, consider
one function that needs to return the most recent date in a date column
in a specific table, or another function that computes a date from a
date and an offset (e.g. if called with the value returned by the first
function as the first argument and '-7' as the second, returns the date
of the day that is a week earlier than that date). These two functions
are frequently used in the SQL code I'm trying to port and I really
don't want to complicate so many of those statements if I don't have
to.

Thanks

TedTed wrote:

Quote:

Originally Posted by

1) In several tables, in my MySQL version, I created columns using
something like the following:
>
`ab_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
>
This allowed me to ensure that when a record is either added or edited,
the value in the field is set to the current date and time. I.E.,
ab_timestamp is given the current date and time when a record is
created, and then it is updated to the date and time at which the
record is updated. I learned the hard way that MS SQL does not like
"on update CURRENT_TIMESTAMP". So, it looks like MS SQL will allow me
to initialize ab_timestamp to the current date and time, but not
automatically update it to the date and time at which the record is
updated. I have plenty of code to port that depends on the behaviour
supported by MySQL. DO I have to modify all that code, or is there a
way to get MS SQL to provide it? (Yes, I know 'timestamp' is
deprecated in MS SQL and that I should use datetime instead, and in
fact have already done so.)
>
2) I began with a single SQL script that creates all the tables, views,
functions and triggers the database needs. On trying to get MS SQL to
accept it, I encountered a number of error messages saying that CREATE
FUNCTION and CREATE VIEW need to be the first statement in a script.
Why? I know I can work around this odd constraint by putting each
function and view (and IIRC trigger) into its own script, but that
seems like a make work effort imposed for some unknown reason by MS
SQL, unless there is another way to get around it.
>
3) I see, in the documentation for CREATE FUNCTION, functions are not
allowed to use a timestamp for either a parameter or a return value.
This is in reference to a pair of scalar functions I am using which
need to manipulate date and time values. For the purpose of
clarification, is this documentation refering to all date/time data
types, or only the deprecated timestamp type? As examples, consider
one function that needs to return the most recent date in a date column
in a specific table, or another function that computes a date from a
date and an offset (e.g. if called with the value returned by the first
function as the first argument and '-7' as the second, returns the date
of the day that is a week earlier than that date). These two functions
are frequently used in the SQL code I'm trying to port and I really
don't want to complicate so many of those statements if I don't have
to.
>
Thanks
>
Ted


1) You can use an UPDATE trigger to simulate the same functionality.
Usually though it is better to use stored procedures to perform all
your data access. That way you can easily include the timestamp as part
of your update procs.

2) You don't need separate scripts for each View / Function. You do
need separate batches. A batch is separated using the GO keyword in
Query Analyzer.

3) Don't confuse TIMESTAMP with DATETIME. They are not at all the same!
You are referring to DATETIME values, which ARE permitted as parameters
and return values in functions. BTW, you don't need to write a function
to do date arithmetic - it already exists as a built-in function:
DATEADD().

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Ted (r.ted.byers@.rogers.com) writes:

Quote:

Originally Posted by

1) In several tables, in my MySQL version, I created columns using
something like the following:
>
`ab_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
>
This allowed me to ensure that when a record is either added or edited,
the value in the field is set to the current date and time. I.E.,
ab_timestamp is given the current date and time when a record is
created, and then it is updated to the date and time at which the
record is updated. I learned the hard way that MS SQL does not like
"on update CURRENT_TIMESTAMP". So, it looks like MS SQL will allow me
to initialize ab_timestamp to the current date and time, but not
automatically update it to the date and time at which the record is
updated. I have plenty of code to port that depends on the behaviour
supported by MySQL. DO I have to modify all that code, or is there a
way to get MS SQL to provide it?


You will have to modify all that code. To have to be portable is indeed
painful, and a good start is to pay attention to what is in ANSI standards,
what is commonly supported. And not to the least to what are extensions
added by a certain vendor. My knowledge of ANSI and other engines are
poor (since I live in a sheltered world where I only need to support
SQL Server), but I would suspect that the ON UPDATE clause for the
default constraint is properitary to MySQL.

The way to do this in SQL Server is to use a trigger. Not that triggers
usually are very portable...

You can also modify the UPDATE statements so that they read:

UPDATE tbl
SET ...,
ab_timestamp = DEFAULT,
WHERE ...

I believe this syntax is portable.

Quote:

Originally Posted by

(Yes, I know 'timestamp' is deprecated in MS SQL and that I should use
datetime instead, and in fact have already done so.)


timestamp is not deprecated in SQL Server, but it's a completely different
data type, which is used to implement optimistic locking. A timestamp
is an 8-bit value is updated each time the row is updated and it's
unique within the database. Further more timestamp value are monotonically
increasing. But there is no correlation with time. Timestamp is
proprietary to SQL Server, so you should probably stay away from it
entirely.

Quote:

Originally Posted by

2) I began with a single SQL script that creates all the tables, views,
functions and triggers the database needs. On trying to get MS SQL to
accept it, I encountered a number of error messages saying that CREATE
FUNCTION and CREATE VIEW need to be the first statement in a script.
Why? I know I can work around this odd constraint by putting each
function and view (and IIRC trigger) into its own script, but that
seems like a make work effort imposed for some unknown reason by MS
SQL, unless there is another way to get around it.


Not a separate script, but a separate batch. Batches are separated with
"go" in all query tools. The graphic tools permit you to specify a
different batch separator.

Why? Because else it would be difficult to tell where a procedure ends.
Say that you have:

CREATE PROCEDURE ... AS
...
CREATE TABLE ...

Is that CREATE TABLE part of the procedure or not? (Yes, if you have
BEGIN END it's clear. But of legacy BEGIN END is not required in
stored procedures.)

Quote:

Originally Posted by

3) I see, in the documentation for CREATE FUNCTION, functions are not
allowed to use a timestamp for either a parameter or a return value.


As noted above, there is little reason for your to use the timestamp
data type.

--
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

More questions about data mining plug-in algorithms?

Hi, all here,

I am having a more considertaion about Data Mining plug-in algorithms. When we say we are going to embed a uesr plug-in algorithm, so what is the context for that ? I mean in which case then we thing we need to embed a user plug-in algortihm? I know when we say we are going to embed a user costomermized plug-in algorithm, it means we want something more costomized. But what kind of customized features are generally concerned? Is it independant for different market sectors?

I dont think we can just try to embed a plug-in algorithm then compete it with avaialble algorithms to see which one is with better prediction accuracy?

Would please someone here give me some guidances about that?

Essentially, you would use a customized plug-in algorithm when one of the built-in algorithms doesn't perform the task you want. There are a wide variety of DM algorithms, and we have nine built in. For example, you may want hierarchical clustering, or link analysis, or a classification algorithm particularly suited towards fraud. Another example is an algorithm that allows you to specify additional penalties for specific incorrect predictions.

Considering your last question, yes, you can create a plug-in algorithm and compare it with the built in's to see which has better accuracy

|||

Hi, Jamie, thanks a lot for your very helpful guidance.

With best regards,

more questions

Lets say on tbDeposits I have a Index that is on both depositdue and deposit paid

why does this statement

Select * from tbDeposits Deposits where Deposits.DateDue IS NOT NULL
AND Deposits.DatePaid IS NULL

have 2 table scans in it? one for 83 percent?

A NULL value is 'unknown' and cannot be efficiently indexed.

Any time you are trying to find the absence of something, you have to look at everything you have to see if it is missing. That may require a table scan or it may require an index scan. (IF it is the clustered index key, then an index scan is still a table scan.)

sql

More Question on Permissions

I like to thanks all of you helps.
but I still can't find information I need among
sysprotects, sp_helpprotect,
INFORMATION_SCHEMA.table_priviledges, and
INFORMATION_SCHEMA.column_priviledges.
What I need to know is if a user don't have certain
priviledge, was it been revoked or denied?
For example, if a user has no UPDATE privildge on a
table's one column, was it been denied or revoked(never
been granted)?
Greg Chang
hi Greg,
"Greg Chang" <anonymous@.discussions.microsoft.com> ha scritto nel messaggio
news:2276c01c45d2c$35de6cd0$a501280a@.phx.gbl...
> I like to thanks all of you helps.
> but I still can't find information I need among
> sysprotects, sp_helpprotect,
> INFORMATION_SCHEMA.table_priviledges, and
> INFORMATION_SCHEMA.column_priviledges.
> What I need to know is if a user don't have certain
> priviledge, was it been revoked or denied?
> For example, if a user has no UPDATE privildge on a
> table's one column, was it been denied or revoked(never
> been granted)?
it all depends on user database role membership too...
SET NOCOUNT ON
CREATE DATABASE GREG
GO
USE GREG
GO
EXEC sp_adduser @.loginame = 'roby'
, @.name_in_db = 'roby'
GO
CREATE TABLE dbo.Protected (
Id INT NOT NULL ,
Data VARCHAR( 10 )
)
GO
GRANT SELECT, INSERT, UPDATE, DELETE ON [dbo].[Protected] ([Id]) TO [roby]
DENY INSERT, UPDATE, DELETE ON [dbo].[Protected] ([Data]) TO [roby]
GO
EXEC sp_helprotect @.name = [Protected]
GO
SETUSER 'roby'
SELECT * FROM [dbo].[Protected]
SELECT [Id] FROM [dbo].[Protected]
GO
SETUSER
REVOKE ALL ON [dbo].[Protected] TO [roby]
GO
SETUSER 'roby'
SELECT * FROM [dbo].[Protected]
SELECT [Id] FROM [dbo].[Protected]
GO
SETUSER
GO
USE master
GO
DROP DATABASE GREG
--<---
Granted database access to 'roby'.
Owner Object Grantee Grantor ProtectType Action Column
-- -- -- -- -- -- --
dbo Protected roby dbo Deny Delete .
dbo Protected roby dbo Deny Insert .
dbo Protected roby dbo Deny Update Data
dbo Protected roby dbo Grant Select Id
dbo Protected roby dbo Grant Update Id
(5 row(s) affected)
Server: Msg 230, Level 14, State 1, Line 1
SELECT permission denied on column 'Data' of object 'Protected', database 'G
REG', owner 'dbo'.
Id
Server: Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'Protected', database 'GREG', owner
'dbo'.
Server: Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'Protected', database 'GREG', owner
'dbo'.
as you can see, user "roby" does not belong to particular database role, so
it has no explicit access to base table [dbo].[Protected]... he has benn
granted SELECT, INSERT, DELETE, UPDATE permission only on
[dbo].[Protected].[Id], while explicit DENY has ben set on
[dbo].[Protected].[Data] column...
and even after revoking all privleges, he still has no access to the base
table...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.8.0 - DbaMgr ver 0.54.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||hi Greg,
"Greg Chang" <anonymous@.discussions.microsoft.com> ha scritto nel messaggio
news:2276c01c45d2c$35de6cd0$a501280a@.phx.gbl...
> I like to thanks all of you helps.
> but I still can't find information I need among
> sysprotects, sp_helpprotect,
> INFORMATION_SCHEMA.table_priviledges, and
> INFORMATION_SCHEMA.column_priviledges.
> What I need to know is if a user don't have certain
> priviledge, was it been revoked or denied?
> For example, if a user has no UPDATE privildge on a
> table's one column, was it been denied or revoked(never
> been granted)?
it all depends on user database role membership too...
SET NOCOUNT ON
CREATE DATABASE GREG
GO
USE GREG
GO
EXEC sp_adduser @.loginame = 'roby'
, @.name_in_db = 'roby'
GO
CREATE TABLE dbo.Protected (
Id INT NOT NULL ,
Data VARCHAR( 10 )
)
GO
GRANT SELECT, INSERT, UPDATE, DELETE ON [dbo].[Protected] ([Id]) TO [roby]
DENY INSERT, UPDATE, DELETE ON [dbo].[Protected] ([Data]) TO [roby]
GO
EXEC sp_helprotect @.name = [Protected]
GO
SETUSER 'roby'
SELECT * FROM [dbo].[Protected]
SELECT [Id] FROM [dbo].[Protected]
GO
SETUSER
REVOKE ALL ON [dbo].[Protected] TO [roby]
GO
SETUSER 'roby'
SELECT * FROM [dbo].[Protected]
SELECT [Id] FROM [dbo].[Protected]
GO
SETUSER
GO
USE master
GO
DROP DATABASE GREG
--<---
Granted database access to 'roby'.
Owner Object Grantee Grantor ProtectType Action Column
-- -- -- -- -- -- --
dbo Protected roby dbo Deny Delete .
dbo Protected roby dbo Deny Insert .
dbo Protected roby dbo Deny Update Data
dbo Protected roby dbo Grant Select Id
dbo Protected roby dbo Grant Update Id
(5 row(s) affected)
Server: Msg 230, Level 14, State 1, Line 1
SELECT permission denied on column 'Data' of object 'Protected', database 'G
REG', owner 'dbo'.
Id
Server: Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'Protected', database 'GREG', owner
'dbo'.
Server: Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'Protected', database 'GREG', owner
'dbo'.
as you can see, user "roby" does not belong to particular database role, so
it has no explicit access to base table [dbo].[Protected]... he has benn
granted SELECT, INSERT, DELETE, UPDATE permission only on
[dbo].[Protected].[Id], while explicit DENY has ben set on
[dbo].[Protected].[Data] column...
and even after revoking all privleges, he still has no access to the base
table...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.8.0 - DbaMgr ver 0.54.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply