Showing posts with label table. Show all posts
Showing posts with label table. Show all posts

Friday, March 30, 2012

Pessimistic locking

I am attempting to try a pesimistic lock, meaning that i want to lock a row or table for a period of time and then relase it when i am done. To test this i wrote the following:

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANsaction
Select * From configurationitem WITH (ROWLOCK,xlock)
where name = 'NextReceiptNumber' and category = 'AR';

Declare @.i int
set @.i = 0
while @.i < 300000
Begin
print @.i
set @.i = @.i + 1
end
COMMIT TRANsaction

To test, while the above is looping i open another query window and select from the same table using the following:

Select ConfigurationItemValue From configurationitem where ItemID = 418

This does not work because this query returns IMMEDIATELY. However, if I change the query to the following:

Select ConfigurationItemValue From configurationitem where name = 'NextReceiptNumber' and category = 'AR';

It does not return until the transaction query above is finished (which is the way it should work).

So, my question is, why does it not lock when i select by a primary key but lock when i do NOT select by a primary key (ItemID is a primary key).

thanks in advance.

Ok, I think we are missing something here. Is the primary key value for this row = 418? You should only have an exclusive lock on the row.

This is the table that I tested with, and it did wait when I looked for 418, and not for any other row. Any query that requires a table scan (snapshot isolation not withstanding) will not be able to complete (which would be the case for a query that looks for name and category, no matter what your values are.) This is because other queries will take a lock on every row in the table eventually and will get stuck on the locked rows.

drop table configurationItem
go
create table configurationItem
(
itemId int primary key,
name varchar(100),
category char(2),
configurationItemValue varchar(10)
)
insert into configurationItem
select 418,'NextReceiptNumber','AR','sals'
union all
select 2,'asldfjlka','AT','sals'
union all
select 3,'aqjsadklfaj','DR','sals'
union all
select 4,'ao2ioi23jkasd','DD','sals'
union all
select 5,'alifdjald','CD','sals'
union all
select 6,'ajsdflkasdlkja','CF','sals'
union all
select 7,'juqoiwfewoijlk','TT','sals'
union all
select 8,'asdancas','QR','sals'


|||Using XLOCK in SELECT statements will not prevent reads from happening. This is because SQL Server has a special optimization under read committed isolation level that checks if the row is dirty or not and ignores the xlock if the row has not changed. Since this is acceptable under the read committed isolation level semantics it is by design. So you will have to use a more aggressive locking hint like UPDLOCK with ROWLOCK. But what are you trying that requires such pessimistic locking strategies? Why do you want to do row-by-row procedural processing? Can't you use set-based operations instead?

Wednesday, March 28, 2012

Person.Coontact - AdventureWorks

I have the AdventureWorks database in sql server 2005.
The Table Person.Contact is empty. I have the .csv file for the data for
this table. This .csv seems to be xml data.
How can I get this particular .csv into this table?
I basically would like to somehow populate this particular table.
Thanks
If you look at the installation script for AdventureWorks (instawdb.sql),
you'll find a series of BULK INSERT statements that load all the .csv files
for the database. Here's the statement for the Person.Contact table. I
simply modified the statement to point to the location of the .csv file on
my server.
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWorks
OLTP\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
However, unless you know for sure that the Person.Contact table is the only
empty table, you may want to recreate the entire database by running the
installation script. The instructions for doing this are in Books Online in
the topic "Reinstalling Sample Databases From Scripts".
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>I have the AdventureWorks database in sql server 2005.
> The Table Person.Contact is empty. I have the .csv file for the data for
> this table. This .csv seems to be xml data.
> How can I get this particular .csv into this table?
> I basically would like to somehow populate this particular table.
> Thanks
|||Ran the following script:
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
Database Scripts\awdb\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
It gave the following error. thanks
XML Validation: Declaration not found for element
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord:ContactRecord'. Location: /*:AdditionalContactInfo[1]/*:ContactRecord[1]
"Gail Erickson [MS]" wrote:

> If you look at the installation script for AdventureWorks (instawdb.sql),
> you'll find a series of BULK INSERT statements that load all the .csv files
> for the database. Here's the statement for the Person.Contact table. I
> simply modified the statement to point to the location of the .csv file on
> my server.
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWorks
> OLTP\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> However, unless you know for sure that the Person.Contact table is the only
> empty table, you may want to recreate the entire database by running the
> installation script. The instructions for doing this are in Books Online in
> the topic "Reinstalling Sample Databases From Scripts".
> --
> Gail Erickson [MS]
> SQL Server Documentation Team
> This posting is provided "AS IS" with no warranties, and confers no rights
> "farshad" <farshad@.discussions.microsoft.com> wrote in message
> news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>
>
|||> It gave the following error. thanks
Then you have more problems than just an empty table. You need to follow
the instructions in the Books Online topic "Reinstalling Sample Databases
From Scripts". This will drop the database and recreate it.
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:9D8C349E-B64E-4D5D-B4D9-DF7A0D04C917@.microsoft.com...[vbcol=seagreen]
> Ran the following script:
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
> Database Scripts\awdb\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> It gave the following error. thanks
> XML Validation: Declaration not found for element
> 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord:ContactRecord'.
> Location: /*:AdditionalContactInfo[1]/*:ContactRecord[1]
>
> "Gail Erickson [MS]" wrote:

Person.Coontact - AdventureWorks

I have the AdventureWorks database in sql server 2005.
The Table Person.Contact is empty. I have the .csv file for the data for
this table. This .csv seems to be xml data.
How can I get this particular .csv into this table?
I basically would like to somehow populate this particular table.
ThanksIf you look at the installation script for AdventureWorks (instawdb.sql),
you'll find a series of BULK INSERT statements that load all the .csv files
for the database. Here's the statement for the Person.Contact table. I
simply modified the statement to point to the location of the .csv file on
my server.
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWorks
OLTP\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
However, unless you know for sure that the Person.Contact table is the only
empty table, you may want to recreate the entire database by running the
installation script. The instructions for doing this are in Books Online in
the topic "Reinstalling Sample Databases From Scripts".
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>I have the AdventureWorks database in sql server 2005.
> The Table Person.Contact is empty. I have the .csv file for the data for
> this table. This .csv seems to be xml data.
> How can I get this particular .csv into this table?
> I basically would like to somehow populate this particular table.
> Thanks|||Ran the following script:
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
Database Scripts\awdb\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
It gave the following error. thanks
XML Validation: Declaration not found for element
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecor
d:ContactRecord'. Location: /*:AdditionalContactInfo[1]/*:ContactRecord&
#91;1]
"Gail Erickson [MS]" wrote:

> If you look at the installation script for AdventureWorks (instawdb.sql),
> you'll find a series of BULK INSERT statements that load all the .csv file
s
> for the database. Here's the statement for the Person.Contact table. I
> simply modified the statement to point to the location of the .csv file on
> my server.
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWor
ks
> OLTP\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> However, unless you know for sure that the Person.Contact table is the onl
y
> empty table, you may want to recreate the entire database by running the
> installation script. The instructions for doing this are in Books Online
in
> the topic "Reinstalling Sample Databases From Scripts".
> --
> Gail Erickson [MS]
> SQL Server Documentation Team
> This posting is provided "AS IS" with no warranties, and confers no rights
> "farshad" <farshad@.discussions.microsoft.com> wrote in message
> news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>
>|||> It gave the following error. thanks
Then you have more problems than just an empty table. You need to follow
the instructions in the Books Online topic "Reinstalling Sample Databases
From Scripts". This will drop the database and recreate it.
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:9D8C349E-B64E-4D5D-B4D9-DF7A0D04C917@.microsoft.com...[vbcol=seagreen]
> Ran the following script:
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
> Database Scripts\awdb\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> It gave the following error. thanks
> XML Validation: Declaration not found for element
> 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRec
ord:ContactRecord'.
> Location: /*:AdditionalContactInfo[1]/*:ContactRecord[1]
>
> "Gail Erickson [MS]" wrote:
>

Person.Coontact - AdventureWorks

I have the AdventureWorks database in sql server 2005.
The Table Person.Contact is empty. I have the .csv file for the data for
this table. This .csv seems to be xml data.
How can I get this particular .csv into this table?
I basically would like to somehow populate this particular table.
ThanksIf you look at the installation script for AdventureWorks (instawdb.sql),
you'll find a series of BULK INSERT statements that load all the .csv files
for the database. Here's the statement for the Person.Contact table. I
simply modified the statement to point to the location of the .csv file on
my server.
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWorks
OLTP\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
However, unless you know for sure that the Person.Contact table is the only
empty table, you may want to recreate the entire database by running the
installation script. The instructions for doing this are in Books Online in
the topic "Reinstalling Sample Databases From Scripts".
--
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>I have the AdventureWorks database in sql server 2005.
> The Table Person.Contact is empty. I have the .csv file for the data for
> this table. This .csv seems to be xml data.
> How can I get this particular .csv into this table?
> I basically would like to somehow populate this particular table.
> Thanks|||Ran the following script:
EXECUTE (N'BULK INSERT [Person].[Contact]
FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
Database Scripts\awdb\Contact.csv''
WITH (
CHECK_CONSTRAINTS,
CODEPAGE=''ACP'',
DATAFILETYPE=''widechar'',
FIELDTERMINATOR=''+|'',
ROWTERMINATOR=''&|\n'',
KEEPIDENTITY,
TABLOCK
);');
It gave the following error. thanks
XML Validation: Declaration not found for element
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord:ContactRecord'. Location: /*:AdditionalContactInfo[1]/*:ContactRecord[1]
"Gail Erickson [MS]" wrote:
> If you look at the installation script for AdventureWorks (instawdb.sql),
> you'll find a series of BULK INSERT statements that load all the .csv files
> for the database. Here's the statement for the Person.Contact table. I
> simply modified the statement to point to the location of the .csv file on
> my server.
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server\90\Tools\Samples\AdventureWorks
> OLTP\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> However, unless you know for sure that the Person.Contact table is the only
> empty table, you may want to recreate the entire database by running the
> installation script. The instructions for doing this are in Books Online in
> the topic "Reinstalling Sample Databases From Scripts".
> --
> Gail Erickson [MS]
> SQL Server Documentation Team
> This posting is provided "AS IS" with no warranties, and confers no rights
> "farshad" <farshad@.discussions.microsoft.com> wrote in message
> news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
> >I have the AdventureWorks database in sql server 2005.
> > The Table Person.Contact is empty. I have the .csv file for the data for
> > this table. This .csv seems to be xml data.
> > How can I get this particular .csv into this table?
> > I basically would like to somehow populate this particular table.
> > Thanks
>
>|||> It gave the following error. thanks
Then you have more problems than just an empty table. You need to follow
the instructions in the Books Online topic "Reinstalling Sample Databases
From Scripts". This will drop the database and recreate it.
--
Gail Erickson [MS]
SQL Server Documentation Team
This posting is provided "AS IS" with no warranties, and confers no rights
"farshad" <farshad@.discussions.microsoft.com> wrote in message
news:9D8C349E-B64E-4D5D-B4D9-DF7A0D04C917@.microsoft.com...
> Ran the following script:
> EXECUTE (N'BULK INSERT [Person].[Contact]
> FROM ''C:\Program Files\Microsoft SQL Server 2005 AdventureWorks Sample
> Database Scripts\awdb\Contact.csv''
> WITH (
> CHECK_CONSTRAINTS,
> CODEPAGE=''ACP'',
> DATAFILETYPE=''widechar'',
> FIELDTERMINATOR=''+|'',
> ROWTERMINATOR=''&|\n'',
> KEEPIDENTITY,
> TABLOCK
> );');
> It gave the following error. thanks
> XML Validation: Declaration not found for element
> 'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactRecord:ContactRecord'.
> Location: /*:AdditionalContactInfo[1]/*:ContactRecord[1]
>
> "Gail Erickson [MS]" wrote:
>> If you look at the installation script for AdventureWorks (instawdb.sql),
>> you'll find a series of BULK INSERT statements that load all the .csv
>> files
>> for the database. Here's the statement for the Person.Contact table. I
>> simply modified the statement to point to the location of the .csv file
>> on
>> my server.
>> EXECUTE (N'BULK INSERT [Person].[Contact]
>> FROM ''C:\Program Files\Microsoft SQL
>> Server\90\Tools\Samples\AdventureWorks
>> OLTP\Contact.csv''
>> WITH (
>> CHECK_CONSTRAINTS,
>> CODEPAGE=''ACP'',
>> DATAFILETYPE=''widechar'',
>> FIELDTERMINATOR=''+|'',
>> ROWTERMINATOR=''&|\n'',
>> KEEPIDENTITY,
>> TABLOCK
>> );');
>> However, unless you know for sure that the Person.Contact table is the
>> only
>> empty table, you may want to recreate the entire database by running the
>> installation script. The instructions for doing this are in Books Online
>> in
>> the topic "Reinstalling Sample Databases From Scripts".
>> --
>> Gail Erickson [MS]
>> SQL Server Documentation Team
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights
>> "farshad" <farshad@.discussions.microsoft.com> wrote in message
>> news:44F58336-3BAD-4EC9-868B-0FD6AD3DA921@.microsoft.com...
>> >I have the AdventureWorks database in sql server 2005.
>> > The Table Person.Contact is empty. I have the .csv file for the data
>> > for
>> > this table. This .csv seems to be xml data.
>> > How can I get this particular .csv into this table?
>> > I basically would like to somehow populate this particular table.
>> > Thanks
>>sql

Persisting a SqlDataReader in a SqlFunction enumerator

I have built a Table Valued SqlFunction which streams out filtered results from a table. The way that I have implemented this is by using a custom class which implements the IEnumerator interface.

The class internally stores a SqlDataReader and a SqlConnection as private member variables. The Class initializer (ie: the New function) creates a SqlCommand which is executed with ExecuteReader into the SqlDataReader and returns.

The IEnumerator.MoveNext method loops through the SqlDataReader until it finds a result which matches the heuristic filter, and the IEnumerator.Current method returns the current result from the SqlDataReader.

Unfortunately as soon as the initializer returns the SqlDataReader is automatically closed, and I can't figure out why. I've debugged through this and at the end of the Initializer the SqlDataReader is definately open, and as soon as first call to MoveNext is run it is closed.

Can anyone offer any suggestions on how I can fix this.

In the interim i've had to load all of the filtered results into a temporary ArrayList in the Initializer, which defeats the purpose of streaming out the results.

"You can use the context connection in the initialization method ..., but not in the method that fills rows (the method pointed to by the FillRowMethodName attribute property)." (copy/pasted from Managed Data Access Inside SQL Server with ADO.NET and SQLCLR), so you cannot loop through the SqlDataReader inside FillRow method.

Solutions are:

Implement the filtering using T-SQL (if possible)|||I'm not using the FillRow method to access the SqlDataReader; it is just passed a copy of the object (it's a UDT in this case) which the IEnumerator.Current method returns. I am looping through the SqlDataReader in the IEnumerator.MoveNext method.

My code looks something like this (with large chunks of logic cut out):

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Runtime.InteropServices
Imports System.Collections
Imports System.Security
Imports System.Net

<Assembly: AllowPartiallyTrustedCallers()>

Public Class MyListIterator
Implements IEnumerator

Private oIncludes(0) As MySQLType
Private oExcludes(0) As MySQLType
Private oColumnMeta As SqlMetaData
Private oConn As SqlConnection
Private oDR As SqlDataReader
Private bMoreRecords As Boolean = False
Private iChannelAccountID As Integer

Public Sub New(ByVal ChannelAccountID As Integer)
iChannelAccountID = ChannelAccountID
InitReader()
End Sub

Private Sub InitReader()
oConn = New SqlConnection("context connection=true")
Debug("Starting InitReader")
Dim oCommand As SqlCommand
Dim oMyCode As MySQLType

' Set up the filter arrays (oIncludes and oExcludes) here
' ....
' end filter array setup

' now we'll just loop through all of the My codes and return those that match
oCommand = New SqlCommand("SELECT MyCode FROM MyCodes", oConn)
Dim oColumnMeta As New SqlMetaData("MyCode", SqlDbType.Udt, GetType(MySQLType))

oConn.Open()
oDR = oCommand.ExecuteReader()
bMoreRecords = True
' dont' need to get the first item
' the initial position of the result set is defined as "before the beginning"
End Sub

Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
Get
If bMoreRecords Then
Return oDR(0)
Else
Return Nothing
End If
End Get
End Property

Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
If bMoreRecords AndAlso Not oDR.IsClosed Then
' find the next included code
Do While oDR.Read
If CType(oDR(0), MySQLType).IsIncluded(oIncludes, oExcludes) Then Return True
Loop
End If
bMoreRecords = False
Return False ' no more records
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
bMoreRecords = False
If Not oDR Is Nothing AndAlso oDR.IsClosed Then oDR.Close()
If Not oConn Is Nothing Then oConn.Close()
InitReader()
End Sub

End Class

Partial Public Class UserDefinedFunctions
<Microsoft.SqlServer.Server.SqlFunction(TableDefinition:="MyCode MySQLType", _
IsPrecise:=True, _
IsDeterministic:=False, _
DataAccess:=DataAccessKind.Read, _
FillRowMethodName:="FillMyListRow")> _
Public Shared Function GetMyList(ByVal ChannelAccountID As Integer) As IEnumerator
Return New MyListIterator(ChannelAccountID)
End Function

Public Shared Sub FillMyListRow(ByVal oMyInCode As Object, <Out()> ByRef oMyOutCode As MySQLType)
If Not oMyInCode Is Nothing Then
oMyOutCode = CType(oMyInCode, MySQLType)
Else
oMyOutCode = MySQLType.Null
End If
End Sub
End Class|||

Further to my last post, I can't use TSQL to create the filters because the logic is too complex (it relies on heirarchical data and complex type rules).

I have created the function as a SQLCLR stored procedure successfully however I was hoping to use a SqlFunction so that I could perform joins (eg: SELECT [whatever] FROM [mytable] WHERE [myfield] IN MyFunction(@.ChannelAccountID)).

One alternative is that I figure out how to join select results with the results returned from a stored procedure, however i haven't figured out how to do that without executing the stored procedure into a temporary table (and even that is simply what I guess that you could do; i haven't actually tried at this point).

I have also implemented a scalar valued CLR function which checks the filters, however this is unoptimal because I need to build the filter arrays again for each and every row in the table (about 20,000 rows). This performs too slow for my liking.

Cheers

Beric Holt

http://buzzrick.true.geek.nz

Persistent autoincrementing value, not attached to row insertion?

I need to get a unique value to use for a record *before* the record is added to a table. It doesn't have to be contiguous with existing records, but it must always be unique, has to be persistent over multiple instantiations of an ASP.NET application, and has to work in that sort of a multi-session environment (where other sessions could need additional unique values before the first session gets around to actually adding a record to the table).

I considered generating and using unique CLSIDs for this, but the resulting value also needs to become part of the filename of some files that are being saved to the disk (and those names also saved in the table), and including text CLSIDs along with other filename data would make for some unpleasantly long and difficult to work with filenames.

I also don't think there's a practical way for me to use a trigger associated with an identity column for this, because I need to save files to disk using the unique value before I even know if the record will in fact end up being added to the table, and what's more, the numbers and names of those files will vary in ways that might be difficult to handle in a stored procedure.

What I'm thinking I will have to do is create a separate database table called something like "UniqueIDGen". This table would have a single record in it with a single integer value, initialized to a value of 1. Then, each time an ID is needed, this one record would be locked, read and incremented by 1. The only reason for doing it this way instead of with an application variable, as I see it, is that the values need to be unique and continue incrementing in perpetuity, no matter how many times the ASP.NET application is recycled or the server is rebooted.

But I still have to wonder if there might be a more efficient method provided by SQL Server for this type of unique value generation ... something that is equally as persistent without requiring an entire table with only a single record to be allocated to such a basic task. Does anyone know of a more elegant solution for this?do a hash of the current date.time.milliseconds, or a unix timestamp in the backend of the asp.net, unless you plan on data being entered in the same second.|||Is there no better option based upon SQL Server or some other persistent technology, other than the workable but awkward solution I mentioned? For some reason I thought there might be a non-table-based identity value, or something like that, specifically for situations like this. Perhaps that is something I'm remembering from some other RDBMS software I dealt with in the past, and not SQL Server?|||Hi,

You could do this, it is a bit like Oracle's NextVal:

CREATE TABLE dbo.Sequences
(
ID int NOT NULL
)

INSERT INTO Sequences (ID) VALUES (0)

CREATE PROCEDURE dbo.NextValue
@.ID int output
AS
UPDATE Sequences SET ID = ID + 1, @.ID = ID + 1
RETURN

And call the stored proc to get the ID.


private void Button1_Click(object sender, System.EventArgs e)
{
string connectionString = @."Server=wpeude-masonix2;Database=TestDatabase;User ID=sa;Password=sa;Trusted_Connection=False";

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "dbo.NextValue";
command.CommandType = CommandType.StoredProcedure;

SqlParameter param = new SqlParameter("@.ID", SqlDbType.Int, 4);
param.Direction = ParameterDirection.Output;
command.Parameters.Add(param);

connection.Open();
command.ExecuteNonQuery();
int pk = (int)param.Value;
connection.Close();
lblNextValue.Text = pk.ToString();
}

The ID would be unique in the Database. You could change the sproc to use NEWID() and have a varchar column instead, if you wanted to use a GUID.

A.|||asmason,

Your example is precisely the solution I proposed in my original post (unless I did an insufficient job of describing what I was thinking of). If there's no method available that's superior to creating a table for this purpose, then I'll go ahead and do it that way.

Thanks for taking the time to write it up.|||Couldn't you take and make a stored procedure that makes it for you? Basically when you go to insert a value you do a :


select @.IDVariable = Max(IDField) from Table

Then when you insert:

 Insert into Table (IDField) Values (@.IDVariable)

I have done something very similar to this in several projects and it remains unique and it is available when you create it.|||Regarding the suggestion of:

select @.IDVariable = Max(IDField) from Table

... and then ...
Insert into Table (IDField) Values (@.IDVariable)

I don't think I should do that. In my case, fair amount of time (seconds or even minutes) can potentially elapse between the time I need the unique ID (the first line) and the actual INSERT (the second line). What happens when, when session #1 is sitting at some point in between those two statements, and session #2 comes along and performs the first statement to get an ID for itself? It will get the same one that the first session got ... and then. if both sessions end up inserting a new row using the provided ID ... *biff*, collisions.

In fact, generally it seems like this wouldn't be wholly safe in any multi-threaded environment (even a delay of milliseconds could theoretically lead to a collision, though it might be much less likely than in my current project). Maybe it would be okay if both statements were encased within a single transaction, but that doesn't allow the ID value to be used for things outside of the database, which is a requirement of what I'm working on.

Also, I can only assume that the performance of the MAX function degrades as the number of rows in the table increases, so that may be a bit of a negative as well, at least compared to the standalone "ID table" method.

For what it is worth, I've already gone ahead and implemented a table for this task, very similar to the code that asmason included above, and it works very nicely. I did add one thing: a "counter_name" field, so that the same table could be used to maintain additional unique counters or values if I run across similiar needs again in the future. Thus, the statement in my stored procedure looks like this:

UPDATE AppCounters SET id = id + 1, @.RETURNID = id + 1 WHERE counter_name = @.ctrname

Persistence Of Temporary Tables

Is it possible to create a temporary table in a 'parent' stored procedure and then access it from a 'child' or nested stored procedure? Bearing in mind that the child proc will definitely be called by the parent proc.

Look at the article from Erland:

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

HTH, jens Suessmeyer.

sql

Persist Progress tab?

is there a way to capture what gets written to the progress tab to a table, or is it overwritten on each execution as xml somewhere so it can be saved kind of like an odometer? the mission is to be able to audit the results of a load, or even if it doesnt turn out to be a load, then document the run.

similarly, is there a way to watch what executes to see what actually runs during a merge join transform, iow, something that exposes ssis to trace? how do i find it?

thanks

drew

You have perf counters but they don't go down as low as individual components (I don't think).

I like the idea of persisting load metadata. Maybe suggest that at the Product Feedback Center.

-Jamie

|||

thanks...as much as id like to take credit for it i got the idea from Kimball and Caserta, quite a great book...though i dont know how workable their recommendations wrt overhead are, not having tried or built any of them yet, but having worked in darkness before makes it all the more appealing to actually try a method for a change <g>.

i thought i would read about their practice recommendations first before diving into the tool. i understand there is another Kimball tome written wrt ssis, so i am up to chapter nine in the first one, and when i finish with the abstraction, i will get into the concrete. here, i have read everything up to last September, and am working my way through the list.

not that you need it (<g>) but i did find a great teaching resource at

https://www.microsoftlearning.com - Course 2943: Updating Your ETL Skills to Microsfot SQL Server Integration Services.

i was very pleased with the lab, and intend to use it as the backbone of my exploration of all the new stuff the new product.

best

drew

perplexed by queries

I have a database of patients visiting a doctor.
Each patient has personal info in a PATIENT table
Each visit has visit info in a VISIT table, linked to the patient table
Each surgery performed has an entry in the SURGERY table, linked to the
visit table (because some visits have multiple surgeries, some have one, and
others have none)
Here's the problem: I need a query that shows each patient's most recent
visit to the doctor.
I made the query, but it would return all visits--but only when surgery was
performed. I guess the null value from a visit with no surgery made it not
return anything. How can I get it to show me only the most recent visit
record, regardless of if surgery was performed? is there some sql code I'm
missing?
Thanks,select *
from patient p join visit v on p.patientid=v.patientid
where v.visitid exists(select * from surgery s where s.visitid=visitid)
-oj
"Scott" <Scott@.discussions.microsoft.com> wrote in message
news:8D6AA643-BDE4-4BFA-8235-B77F3D46F3E6@.microsoft.com...
>I have a database of patients visiting a doctor.
> Each patient has personal info in a PATIENT table
> Each visit has visit info in a VISIT table, linked to the patient table
> Each surgery performed has an entry in the SURGERY table, linked to the
> visit table (because some visits have multiple surgeries, some have one,
> and
> others have none)
> Here's the problem: I need a query that shows each patient's most recent
> visit to the doctor.
> I made the query, but it would return all visits--but only when surgery
> was
> performed. I guess the null value from a visit with no surgery made it not
> return anything. How can I get it to show me only the most recent visit
> record, regardless of if surgery was performed? is there some sql code I'm
> missing?
> Thanks,|||PLEASE POST DDL and the query that you are currently using!
Without ANY idea of what your tables look like
or what the column names are,
this is of course a guess, but...
Select Patient, VisitDate
From Visits V Join Patients P
On P.PatientPK = V.PatientFK
Where V.VisitDate =
(Select Max(VisitDate)
From Visits
Where PatientFK = V.PatientFK)
"Scott" wrote:

> I have a database of patients visiting a doctor.
> Each patient has personal info in a PATIENT table
> Each visit has visit info in a VISIT table, linked to the patient table
> Each surgery performed has an entry in the SURGERY table, linked to the
> visit table (because some visits have multiple surgeries, some have one, a
nd
> others have none)
> Here's the problem: I need a query that shows each patient's most recent
> visit to the doctor.
> I made the query, but it would return all visits--but only when surgery wa
s
> performed. I guess the null value from a visit with no surgery made it not
> return anything. How can I get it to show me only the most recent visit
> record, regardless of if surgery was performed? is there some sql code I'm
> missing?
> Thanks,|||oops...forgot the 'last visit' bit...
select *
from patient p join visit v on p.patientid=v.patientid
where v.visitid exists(select * from surgery s where s.visitid=visitid)
and v.visitdate=(select max(v2.visitdate) from visit v2 where
v2.patientid=v.patientid)
-oj
"oj" <nospam_ojngo@.home.com> wrote in message
news:ex%231W3VRFHA.3628@.TK2MSFTNGP12.phx.gbl...
> select *
> from patient p join visit v on p.patientid=v.patientid
> where v.visitid exists(select * from surgery s where s.visitid=visitid)
> --
> -oj
>
> "Scott" <Scott@.discussions.microsoft.com> wrote in message
> news:8D6AA643-BDE4-4BFA-8235-B77F3D46F3E6@.microsoft.com...
>|||You wouldn't mention surgery if it was irrelevant, so my guess is
select
PATIENT.*, VISIT.*, SURGERY.*
from PATIENT join VISIT
on PATIENT.PatientID = VISIT.PatientID
left outer join SURGERY
on SURGERY.VisitID = VISIT.VisitID
where VISIT.VisitID = (
select top 1 VisitID
from VISIT
where VISIT.PatientID = PATIENT.PatientID
)
Of course this is all a guess, since you posted no details.
What you were probably missing was the outer join, so
patient visits are returned whether or not there was an
associated surgery.
Steve Kass
Drew University
Scott wrote:

>I have a database of patients visiting a doctor.
>Each patient has personal info in a PATIENT table
>Each visit has visit info in a VISIT table, linked to the patient table
>Each surgery performed has an entry in the SURGERY table, linked to the
>visit table (because some visits have multiple surgeries, some have one, an
d
>others have none)
>Here's the problem: I need a query that shows each patient's most recent
>visit to the doctor.
>I made the query, but it would return all visits--but only when surgery was
>performed. I guess the null value from a visit with no surgery made it not
>return anything. How can I get it to show me only the most recent visit
>record, regardless of if surgery was performed? is there some sql code I'm
>missing?
>Thanks,
>|||Oops - forgot the ORDER BY in the subquery:
select
PATIENT.*, VISIT.*, SURGERY.*
from PATIENT join VISIT
on PATIENT.PatientID = VISIT.PatientID
left outer join SURGERY
on SURGERY.VisitID = VISIT.VisitID
where VISIT.VisitID = (
select top 1 VisitID
from VISIT
where VISIT.PatientID = PATIENT.PatientID
order by VisitDate desc -- ***This line left out of previous reply***
)
Scott wrote:

>I have a database of patients visiting a doctor.
>Each patient has personal info in a PATIENT table
>Each visit has visit info in a VISIT table, linked to the patient table
>Each surgery performed has an entry in the SURGERY table, linked to the
>visit table (because some visits have multiple surgeries, some have one, an
d
>others have none)
>Here's the problem: I need a query that shows each patient's most recent
>visit to the doctor.
>I made the query, but it would return all visits--but only when surgery was
>performed. I guess the null value from a visit with no surgery made it not
>return anything. How can I get it to show me only the most recent visit
>record, regardless of if surgery was performed? is there some sql code I'm
>missing?
>Thanks,
>

Monday, March 26, 2012

Permit view and deny table?

This has probably been asked before, but is there a way to allow a user to
access a view but deny access to the underlying table? If so, how is this
done (even as a kluge)?
Thanks!For a user to be given select permissions on a view but not a table
referenced by the view, the view and underlying table must have the same
owner. This forms an ownership chain. As long as that is unbroken then the
permission check will be on the view and not the underlying table. By not
granting explict permissions on your tables and the user will not be able to
select from them.
HTH
Jasper Smith (SQL Server MVP)
http://www.sqldbatips.com
I support PASS - the definitive, global
community for SQL Server professionals -
http://www.sqlpass.org
"Neil W." <neilw@.netlib.com> wrote in message
news:uflVC0shEHA.3320@.TK2MSFTNGP11.phx.gbl...
> This has probably been asked before, but is there a way to allow a user to
> access a view but deny access to the underlying table? If so, how is this
> done (even as a kluge)?
> Thanks!
>

permissions to see design view

Is there a way to allow users to see the design view of a table without
having dbo permissions?
ThanksI'm not familiar with the design view, but you can use sp_help to view
the table structure from Query Analyzer.

Simon

Friday, March 23, 2012

permissions resetting on a View

I have a View that pulls data from one table.
I have assigned a Role with Select-only permission for
that View.
For some reason the permissions for that Role/View keep
getting deleted, so I have to go back and re-grant Select
access over and over again.
Anybody know why this is happening, and is there any way
to prevent it? There should be no change to the
permissions for that Role at all.
TIA,
TerrellWhen you delete a Role or a View, or any other object for that matter, you
also delete the permissions associated with those objects. If you what to
keep the permissions for the View then don't delete the view and recreate,
but instead just ALTER the view. When you ALTER an object the permissions
that are associated with the object stay intact.
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2afa201c46819$3b6e4510$a501280a@.phx
.gbl...
> I have a View that pulls data from one table.
> I have assigned a Role with Select-only permission for
> that View.
> For some reason the permissions for that Role/View keep
> getting deleted, so I have to go back and re-grant Select
> access over and over again.
> Anybody know why this is happening, and is there any way
> to prevent it? There should be no change to the
> permissions for that Role at all.
> TIA,
> Terrell|||
>--Original Message--
>When you delete a Role or a View, or any other object for
that matter, you
>also delete the permissions associated with those
objects. If you what to
>keep the permissions for the View then don't delete the
view and recreate,
>but instead just ALTER the view. When you ALTER an
object the permissions
>that are associated with the object stay intact.
Greg, we aren't changing the view or the roles. It's just
that from time to time the Select permission on that View
gets removed.
Question: when you use sp_refreshview does that actually
delete the view and recreate it? I can't set up an ALTER
inside a sproc (because the ALTER has to be the first line
in a batch, but the CREATE PROCEDURE statement has to
execute before it), which is why I'm using sp_refreshview.
Thanks again,
Terrell|||Since the sp_renameview is a system store procedure, I'm not exactly sure
whether it drops and recreates the view. I did a little test I did, when
you run the sp_refreshview it appears to keep the permissions on a view.
If you really want to create or alter a view via a stored procedure you can
do that with dynamic SQL. Something like so:
create procedure yoursp as
declare @.cmd char(1000)
set @.cmd = 'alter view yourview as select bing, bang, boom from yourtable'
exec(@.cmd)
-- rest of sp
--
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2b1a001c4682f$259d8b90$a501280a@.phx
.gbl...
>
> that matter, you
> objects. If you what to
> view and recreate,
> object the permissions
> Greg, we aren't changing the view or the roles. It's just
> that from time to time the Select permission on that View
> gets removed.
> Question: when you use sp_refreshview does that actually
> delete the view and recreate it? I can't set up an ALTER
> inside a sproc (because the ALTER has to be the first line
> in a batch, but the CREATE PROCEDURE statement has to
> execute before it), which is why I'm using sp_refreshview.
> Thanks again,
> Terrell

permissions resetting on a View

I have a View that pulls data from one table.
I have assigned a Role with Select-only permission for
that View.
For some reason the permissions for that Role/View keep
getting deleted, so I have to go back and re-grant Select
access over and over again.
Anybody know why this is happening, and is there any way
to prevent it? There should be no change to the
permissions for that Role at all.
TIA,
Terrell
When you delete a Role or a View, or any other object for that matter, you
also delete the permissions associated with those objects. If you what to
keep the permissions for the View then don't delete the view and recreate,
but instead just ALTER the view. When you ALTER an object the permissions
that are associated with the object stay intact.
----
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2afa201c46819$3b6e4510$a501280a@.phx.gbl...
> I have a View that pulls data from one table.
> I have assigned a Role with Select-only permission for
> that View.
> For some reason the permissions for that Role/View keep
> getting deleted, so I have to go back and re-grant Select
> access over and over again.
> Anybody know why this is happening, and is there any way
> to prevent it? There should be no change to the
> permissions for that Role at all.
> TIA,
> Terrell
|||
>--Original Message--
>When you delete a Role or a View, or any other object for
that matter, you
>also delete the permissions associated with those
objects. If you what to
>keep the permissions for the View then don't delete the
view and recreate,
>but instead just ALTER the view. When you ALTER an
object the permissions
>that are associated with the object stay intact.
Greg, we aren't changing the view or the roles. It's just
that from time to time the Select permission on that View
gets removed.
Question: when you use sp_refreshview does that actually
delete the view and recreate it? I can't set up an ALTER
inside a sproc (because the ALTER has to be the first line
in a batch, but the CREATE PROCEDURE statement has to
execute before it), which is why I'm using sp_refreshview.
Thanks again,
Terrell
|||Since the sp_renameview is a system store procedure, I'm not exactly sure
whether it drops and recreates the view. I did a little test I did, when
you run the sp_refreshview it appears to keep the permissions on a view.
If you really want to create or alter a view via a stored procedure you can
do that with dynamic SQL. Something like so:
create procedure yoursp as
declare @.cmd char(1000)
set @.cmd = 'alter view yourview as select bing, bang, boom from yourtable'
exec(@.cmd)
-- rest of sp
----
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2b1a001c4682f$259d8b90$a501280a@.phx.gbl...
> that matter, you
> objects. If you what to
> view and recreate,
> object the permissions
> Greg, we aren't changing the view or the roles. It's just
> that from time to time the Select permission on that View
> gets removed.
> Question: when you use sp_refreshview does that actually
> delete the view and recreate it? I can't set up an ALTER
> inside a sproc (because the ALTER has to be the first line
> in a batch, but the CREATE PROCEDURE statement has to
> execute before it), which is why I'm using sp_refreshview.
> Thanks again,
> Terrell

permissions resetting on a View

I have a View that pulls data from one table.
I have assigned a Role with Select-only permission for
that View.
For some reason the permissions for that Role/View keep
getting deleted, so I have to go back and re-grant Select
access over and over again.
Anybody know why this is happening, and is there any way
to prevent it? There should be no change to the
permissions for that Role at all.
TIA,
TerrellWhen you delete a Role or a View, or any other object for that matter, you
also delete the permissions associated with those objects. If you what to
keep the permissions for the View then don't delete the view and recreate,
but instead just ALTER the view. When you ALTER an object the permissions
that are associated with the object stay intact.
--
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2afa201c46819$3b6e4510$a501280a@.phx.gbl...
> I have a View that pulls data from one table.
> I have assigned a Role with Select-only permission for
> that View.
> For some reason the permissions for that Role/View keep
> getting deleted, so I have to go back and re-grant Select
> access over and over again.
> Anybody know why this is happening, and is there any way
> to prevent it? There should be no change to the
> permissions for that Role at all.
> TIA,
> Terrell|||>--Original Message--
>When you delete a Role or a View, or any other object for
that matter, you
>also delete the permissions associated with those
objects. If you what to
>keep the permissions for the View then don't delete the
view and recreate,
>but instead just ALTER the view. When you ALTER an
object the permissions
>that are associated with the object stay intact.
Greg, we aren't changing the view or the roles. It's just
that from time to time the Select permission on that View
gets removed.
Question: when you use sp_refreshview does that actually
delete the view and recreate it? I can't set up an ALTER
inside a sproc (because the ALTER has to be the first line
in a batch, but the CREATE PROCEDURE statement has to
execute before it), which is why I'm using sp_refreshview.
Thanks again,
Terrell|||Since the sp_renameview is a system store procedure, I'm not exactly sure
whether it drops and recreates the view. I did a little test I did, when
you run the sp_refreshview it appears to keep the permissions on a view.
If you really want to create or alter a view via a stored procedure you can
do that with dynamic SQL. Something like so:
create procedure yoursp as
declare @.cmd char(1000)
set @.cmd = 'alter view yourview as select bing, bang, boom from yourtable'
exec(@.cmd)
-- rest of sp
--
----
----
--
Need SQL Server Examples check out my website at
http://www.geocities.com/sqlserverexamples
"Terrell Miller" <millerto@.bellsouth.net> wrote in message
news:2b1a001c4682f$259d8b90$a501280a@.phx.gbl...
> >--Original Message--
> >When you delete a Role or a View, or any other object for
> that matter, you
> >also delete the permissions associated with those
> objects. If you what to
> >keep the permissions for the View then don't delete the
> view and recreate,
> >but instead just ALTER the view. When you ALTER an
> object the permissions
> >that are associated with the object stay intact.
> Greg, we aren't changing the view or the roles. It's just
> that from time to time the Select permission on that View
> gets removed.
> Question: when you use sp_refreshview does that actually
> delete the view and recreate it? I can't set up an ALTER
> inside a sproc (because the ALTER has to be the first line
> in a batch, but the CREATE PROCEDURE statement has to
> execute before it), which is why I'm using sp_refreshview.
> Thanks again,
> Terrell

Permissions Problem using Dynamic SQL

Hi all!

I've got a problem where I have created a stored procedure (using MS SQL Server 2000) that does a temporary table creation:
CREATE #tmpData ( [some_fields] )
and then it uses dynamic SQL to populate the data
SELECT @.ExecStr =
'INSERT INTO #tmpData
SELECT * FROM tData
WHERE [some_condition]
ORDER BY ' + @.SortColumn /* input parm to the stored proc */
EXEC (@.ExecStr)

I get a permissions error 229 when I try to run this because my user only has execute permissions for the stored procedure within the database. The only thing that I've found so far that will fix this is if I change the user's permissions to db_owner, which I don't want to do.

I've tried to explicitly grant permission within the stored proc, but since the object (the temp table) does not actually reside in the database, that gives me an error as well (4610: You can only grant or revoke permissions on objects in the current database.).

Is there anything else I can do? I really don't want to have to give the user that much freedom within the database, and removing the dynamic SQL really isn't a viable option either.

Thanks in advance for your help!
CatDynamic SQL executes within its own scope, not that of the stored procedure. So it does not inherit the stored procedures rights, and only operates under the connection's rights.
You'll need to grant READ permission to the user on table tData.|||Try this ... put your insert before your exec ...


SET NOCOUNT ON
USE master

create table #temp (dbname sysname)

declare @.sql nvarchar(255)
select @.sql = 'select name from sysdatabases'

insert into #temp
exec sp_executesql @.sql

select * from #temp

drop table #temp|||Thanks, Tom!

That was a good idea. Unfortunately it still gives me the same error.

Thanks again! If you have any other ideas, let me know.
Cat|||Your user is going to have to have select permissions on tData.|||Excellent suggestion.
You'll need to grant READ permission to the user on table tData.|||Thanks everyone!

I did get this running with just the SELECT permissions on the data tables involved. Although this is still not ideal from a security perspective, it is better than having to give them db_owner. I appreciate the feedback!

Thanks again,
Cat|||If security is a big issue, you could create a view based upon the table showing only the required columns and filtered rows, and then reference the view in your dynamic SQL. Then you can grant permissions on the view rather than on the table.|||Excellent suggestion.

Good advice is always worth repeating :cool:|||You could kludge your way around part of the problem using something like:DECLARE @.i INT

SET @.i = 1

SELECT o.name
FROM dbo.sysobjects AS o
ORDER BY
CASE @.i
WHEN 1 THEN o.name
WHEN 2 THEN Str(o.id, 20)
ELSE Convert(CHAR(30), crdate, 121)
END-PatP|||If security is a big issue,

I have to go change my pants now

"If"...good lord

Wednesday, March 21, 2012

Permissions Problem using Dynamic SQL

Hi all!

I've got a problem where I have created a stored procedure (using MS SQL Server 2000) that does a temporary table creation:

Code Snippet

CREATE #tmpData ( [some_fields] )

and then it uses dynamic SQL to populate the data

Code Snippet

SELECT @.ExecStr =
'INSERT INTO #tmpData
SELECT * FROM tData
WHERE [some_condition]
ORDER BY ' + @.SortColumn /* input parm to the SP */
EXEC (@.ExecStr)

I get a permissions error 229 when I try to run this because my user only has execute permissions for the stored procedure within the database. The only thing that I've found so far that will fix this is if I change the user's permissions to db_owner, which I don't want to do.

I've tried to explicitly grant permission within the stored proc, but since the object (the temp table) does not actually reside in the database, that gives me an error as well (4610: You can only grant or revoke permissions on objects in the current database.).

Is there anything else I can do? I really don't want to have to give the user that much freedom within the database, and removing the dynamic SQL really isn't a viable option either.

Thanks in advance for your help!

Cat

If you are using SQL 2000, you have no choice. To use dynamic SQL requires a high level of permissions.

IF you are using SQL 2005, explore the 'EXECUTE AS' property.

Refer to Books Online, Topic: 'EXECUTE AS'

permissions on a column

hi,

i run a sqlserver 2000 and im having problems setting a permission a
column in a table..

under a database i have a User that has dataread rights on each table
in the database, but in one table i want to prevent the user from
seeing a column in one perticular table.

i have created the user under security and then i choose the database
user - properties..and i set a X in the specified column...

when i log on as the datareader user i cant see any colummn at all in
the table..

what have a done wrong?

Steve[posted and mailed, vnligen svara i nys]

steve (stebo@.privat.utfors.se) writes:
> i run a sqlserver 2000 and im having problems setting a permission a
> column in a table..
> under a database i have a User that has dataread rights on each table
> in the database, but in one table i want to prevent the user from
> seeing a column in one perticular table.
> i have created the user under security and then i choose the database
> user - properties..and i set a X in the specified column...
> when i log on as the datareader user i cant see any colummn at all in
> the table..
> what have a done wrong?

Used a GUI instead of looking up the commands in Books Online. GUIs may
do the what you expect, or they may do something else. The command to
use is DENY. Here is an example:

use tempdb
go
exec sp_addlogin accesstest, secret
exec sp_adduser accesstest
exec sp_addrolemember db_datareader, accesstest
go
CREATE TABLE tbl (a int NOT NULL, b varchar(23) NOT NULL)
INSERT tbl (a, b) VALUES (9, 'Top secret')
go
SETUSER 'accesstest'
go
SELECT * FROM tbl
go
SETUSER
go
DENY ALL ON tbl (b) TO accesstest
go
SETUSER 'accesstest'
go
SELECT * FROM tbl
go
SETUSER
go
DROP TABLE tbl
EXEC sp_dropuser accesstest
EXEC sp_droplogin accesstest

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||thanks for your help!

I will try this, what do you think about using a VIEW, is this a good choice?

BR

Steve
--

Erland Sommarskog <esquel@.sommarskog.se> wrote in message news:<Xns959DF3D2FD2D0Yazorman@.127.0.0.1>...
> [posted and mailed, vnligen svara i nys]
> steve (stebo@.privat.utfors.se) writes:
> > i run a sqlserver 2000 and im having problems setting a permission a
> > column in a table..
> > under a database i have a User that has dataread rights on each table
> > in the database, but in one table i want to prevent the user from
> > seeing a column in one perticular table.
> > i have created the user under security and then i choose the database
> > user - properties..and i set a X in the specified column...
> > when i log on as the datareader user i cant see any colummn at all in
> > the table..
> > what have a done wrong?
> Used a GUI instead of looking up the commands in Books Online. GUIs may
> do the what you expect, or they may do something else. The command to
> use is DENY. Here is an example:
> use tempdb
> go
> exec sp_addlogin accesstest, secret
> exec sp_adduser accesstest
> exec sp_addrolemember db_datareader, accesstest
> go
> CREATE TABLE tbl (a int NOT NULL, b varchar(23) NOT NULL)
> INSERT tbl (a, b) VALUES (9, 'Top secret')
> go
> SETUSER 'accesstest'
> go
> SELECT * FROM tbl
> go
> SETUSER
> go
> DENY ALL ON tbl (b) TO accesstest
> go
> SETUSER 'accesstest'
> go
> SELECT * FROM tbl
> go
> SETUSER
> go
> DROP TABLE tbl
> EXEC sp_dropuser accesstest
> EXEC sp_droplogin accesstest|||steve (stebo@.privat.utfors.se) writes:
> thanks for your help!
> I will try this, what do you think about using a VIEW, is this a good
> choice?

I don't know your business problem, so I cannot comment on that.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Permissions not Saving

I am having a strange problem with certain databases.
Some of the permissions I set for certain tables are not
saving. I right click on the table,move over "All tasks"
and select "Manage Permissions". I tick the permissions I
want and click ok. When I exit and go back in the
permissions I have set are gone.
Can anyone explain.Hi,
Not sure of the issue in Enterprise manager. Can you try giving the
permissions using GRANT statement from Query analyzer.
How to give prev.
GRANT SELECT on table_name to <user_name>
GRANT INSERT on table_name to <User_name>
GRANT UPDATE on table_name to <user_name>
GRANT DELETE on table_name to <User_name>
GRANT ALL on table_name to <user_name>
GRANT INSERT,SELECT on table_name to <User_name>
Procedures
GRANT EXECUTE on proc_name to <user_name>
Have a look into grant statement in books online for more previlage details
Thanks
Hari
MCDBA
"Nobster" <Norbert_Armstrong@.dub.Invesco.com> wrote in message
news:15a6001c44713$30c84b00$a301280a@.phx
.gbl...
> I am having a strange problem with certain databases.
> Some of the permissions I set for certain tables are not
> saving. I right click on the table,move over "All tasks"
> and select "Manage Permissions". I tick the permissions I
> want and click ok. When I exit and go back in the
> permissions I have set are gone.
> Can anyone explain.|||I have seen something similar before. Try testing the permissions in QA as
they may well have been correctly set. In my case when I used sp_helpprotect
or looked directly at sysprotects the permissions had been entered but EM
didn't display them correctly. Some time ago I did once 'debug' this
behaviour using profiler and posted up my findings in the replication group,
but unfortunately a search doesn't reveal them. Anyway if this corresponds
to your situation and you do profile it, please post up your findings.
Regards,
Paul Ibison

Tuesday, March 20, 2012

permissions gone missing in sysprotects

Hi all,
I have upgarded db from sql 7.0 to sql 2000 and I notice that load of
records have gone missing in sysprotects table ( something like from 5000
records down to 60 records only).
As a results, permission setting does not appear in objects permission
management in Enterprise Manager.
I know that the permission setting is still hold in syspermissions table,
but without the "green ticks" appears on the Enterprise manager interface, w
e
would not be able to know what permission we have assigned to each role or
user at all.
This problem only happen with the database that has been moved from 7.0 to
2000 though , if you move db from 2000 to 2000, it seems to be fine.
Does anyone else out there know how to fix this ?
cheers
JackPlease take a look at the following kb, especially at the transfer logins
section.
http://support.microsoft.com/kb/314546
-oj
"Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
news:2FEBE409-12C4-48D0-B076-5D5EAF607534@.microsoft.com...
> Hi all,
> I have upgarded db from sql 7.0 to sql 2000 and I notice that load of
> records have gone missing in sysprotects table ( something like from 5000
> records down to 60 records only).
> As a results, permission setting does not appear in objects permission
> management in Enterprise Manager.
> I know that the permission setting is still hold in syspermissions table,
> but without the "green ticks" appears on the Enterprise manager interface,
> we
> would not be able to know what permission we have assigned to each role or
> user at all.
> This problem only happen with the database that has been moved from 7.0 to
> 2000 though , if you move db from 2000 to 2000, it seems to be fine.
> Does anyone else out there know how to fix this ?
> cheers
> Jack
>|||Hi oj,
this is not orphan user problem because it even happens with the newly
created user. What happen is that , when I assigned permission setting to a
user (newly created or existing users, it don't matter) , the green ticks
don't hold, so when I go back to look at permission setting, they are all
blank, no green ticks indicate what permission was setting before.
However, the permission I assigned does work. A user cannot perform any task
(select, insert, etc) that is not allowed to.
The real problem of this "green ticks don't stay" issue is not about
permission, it is about "permission management".
Because I cannot see the "green ticks" , there is no way for me to know [or
to remember] what permission have I been assigned to roles, users.
Using "sp_helprotect" does not help either. Because sp_helprotect does get
results from sysprotects, but the permission I set only went into
"syspermissions", but not to "sysprotects".
Please note again, this "phenomenon" only occurs when one migrate database
from SQL 7.0 to SQL 2000. Migrate DB from SQL 2000 to SQL 2000 is fine.
I was wondering if any Microsoft SQL guru out there actually ever come
across this problem before, but I must admit that I am surprised I cannot
find any document that talk directly about how to solve this matter at all i
n
MSDN or Books on line or not even the entire web (search using google).
Any suggestions would be appreciated
Jack
"oj" wrote:

> Please take a look at the following kb, especially at the transfer logins
> section.
> http://support.microsoft.com/kb/314546
>
> --
> -oj
>
> "Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
> news:2FEBE409-12C4-48D0-B076-5D5EAF607534@.microsoft.com...
>
>|||Hello Jack,
I currently do not have access to a sql7 to confirm. Though, I've seen this
in the past when I upgraded to sql2k. At this point, I would suggest firing
up Profiler and see what is sent to the backend when you use EM to
change/set permission.
G'luck.
--
-oj
"Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
news:A3913B95-5F42-44D4-A8E4-A15E8A99D2C8@.microsoft.com...
> Hi oj,
> this is not orphan user problem because it even happens with the newly
> created user. What happen is that , when I assigned permission setting to
> a
> user (newly created or existing users, it don't matter) , the green ticks
> don't hold, so when I go back to look at permission setting, they are all
> blank, no green ticks indicate what permission was setting before.
> However, the permission I assigned does work. A user cannot perform any
> task
> (select, insert, etc) that is not allowed to.
> The real problem of this "green ticks don't stay" issue is not about
> permission, it is about "permission management".
> Because I cannot see the "green ticks" , there is no way for me to know
> [or
> to remember] what permission have I been assigned to roles, users.
> Using "sp_helprotect" does not help either. Because sp_helprotect does
> get
> results from sysprotects, but the permission I set only went into
> "syspermissions", but not to "sysprotects".
> Please note again, this "phenomenon" only occurs when one migrate database
> from SQL 7.0 to SQL 2000. Migrate DB from SQL 2000 to SQL 2000 is fine.
> I was wondering if any Microsoft SQL guru out there actually ever come
> across this problem before, but I must admit that I am surprised I cannot
> find any document that talk directly about how to solve this matter at all
> in
> MSDN or Books on line or not even the entire web (search using google).
> Any suggestions would be appreciated
> Jack
> "oj" wrote:
>|||Hi Oj,
yes, I did fire up profiler, that is why I can tell that the permission
setting did not go into sysprotects but only go into syspermissions.
And even if I can see how EM insert permission record into system tables,
nothing I can do to change the statement to make it sync both sysprotects an
d
syspermissions (which is what it normally does).
I can manully hack into sysprotects table to insert the missing records to
make it match up with sysusers, syspermissions and sysobjects, but that is
not the solution anyway, because I would then have to do it everytime I work
on user's permission.
any clue ?
Jack
"oj" wrote:

> Hello Jack,
> I currently do not have access to a sql7 to confirm. Though, I've seen thi
s
> in the past when I upgraded to sql2k. At this point, I would suggest firin
g
> up Profiler and see what is sent to the backend when you use EM to
> change/set permission.
> G'luck.
> --
> -oj
>
> "Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
> news:A3913B95-5F42-44D4-A8E4-A15E8A99D2C8@.microsoft.com...
>
>|||First, I mispoke on my last comment. I should have said "I have not seen".
Anyway, do sp_dbcmptlevel on the upgraded database to see if it's set to 80.
I really can't think of a good reason for it not to insert into sysprotects
if it's upgraded from sql7 and its current compatibility level is set to 80.
If needs to, please give PSS a call. You won't be charged if it's a bug.
-oj
"Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
news:19F1D9C1-0324-4F82-B26E-99A4BC0AC99B@.microsoft.com...
> Hi Oj,
> yes, I did fire up profiler, that is why I can tell that the permission
> setting did not go into sysprotects but only go into syspermissions.
> And even if I can see how EM insert permission record into system tables,
> nothing I can do to change the statement to make it sync both sysprotects
> and
> syspermissions (which is what it normally does).
> I can manully hack into sysprotects table to insert the missing records to
> make it match up with sysusers, syspermissions and sysobjects, but that is
> not the solution anyway, because I would then have to do it everytime I
> work
> on user's permission.
> any clue ?
> Jack
>
> "oj" wrote:
>|||what is PSS ?
"oj" wrote:

> First, I mispoke on my last comment. I should have said "I have not seen".
> Anyway, do sp_dbcmptlevel on the upgraded database to see if it's set to 8
0.
> I really can't think of a good reason for it not to insert into sysprotect
s
> if it's upgraded from sql7 and its current compatibility level is set to 8
0.
> If needs to, please give PSS a call. You won't be charged if it's a bug.
> --
> -oj
>
> "Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
> news:19F1D9C1-0324-4F82-B26E-99A4BC0AC99B@.microsoft.com...
>
>|||Jack,
It's MS Product Support Services.
For SQL:
http://support.microsoft.com/oas/de...392&gprid=36498
-oj
"Jack Yao" <JackYao@.discussions.microsoft.com> wrote in message
news:85998831-E11F-41F9-B6ED-1EAC3176432C@.microsoft.com...
> what is PSS ?
> "oj" wrote:
>

permissions gone missing in sysprotects

Hi there,
I have found the bugs when upgarded DB from sql 7.0 to sql 2000 and I
noticed that load of records have gone missing in sysprotects table (
something like from 5000 records down to 60 records only).
As a results, permission setting does not appear in objects permission
management in Enterprise Manager.
I know that the permission setting is still hold in syspermissions table,
but without the "green ticks" appears on the Enterprise manager interface, w
e
would not be able to know what permission we have assigned to each role or
user at all.
This problem only happen with the database that has been moved from 7.0 to
2000 though , if you move db from 2000 to 2000, it seems to be fine.
Does anyone else out there know how to fix this ?
cheers
JackI have never heard of this one.
One way I could see having problems is if you had modified
system tables. That's the only scenario I can think of but I
would have thought you'd get an error in the upgrade
process.
As already suggested in one of the other groups, you should
contact Microsoft Product Support if you feel you have a
bug.
-Sue
On Wed, 26 Jan 2005 20:45:02 -0800, "Jack Yao"
<JackYao@.discussions.microsoft.com> wrote:

>Hi there,
>I have found the bugs when upgarded DB from sql 7.0 to sql 2000 and I
>noticed that load of records have gone missing in sysprotects table (
>something like from 5000 records down to 60 records only).
>As a results, permission setting does not appear in objects permission
>management in Enterprise Manager.
>I know that the permission setting is still hold in syspermissions table,
>but without the "green ticks" appears on the Enterprise manager interface,
we
>would not be able to know what permission we have assigned to each role or
>user at all.
>This problem only happen with the database that has been moved from 7.0 to
>2000 though , if you move db from 2000 to 2000, it seems to be fine.
>Does anyone else out there know how to fix this ?
>cheers
>Jack|||Hi,
By any chance were these permissions on system objects in master.dbo?
AFAIK these were never preserved during upgrades.
Note - In Yukon, we now preserve permissions on system objects during the
upgrade process. So if you DENY EXECUTE TO PUBLIC on master.dbo.xp_cmdshell
,
we will remember that during the upgrade.
Regards,
Clifford Dibble
Program Manager
SQL Server Engine
"Sue Hoegemeier" wrote:

> I have never heard of this one.
> One way I could see having problems is if you had modified
> system tables. That's the only scenario I can think of but I
> would have thought you'd get an error in the upgrade
> process.
> As already suggested in one of the other groups, you should
> contact Microsoft Product Support if you feel you have a
> bug.
> -Sue
> On Wed, 26 Jan 2005 20:45:02 -0800, "Jack Yao"
> <JackYao@.discussions.microsoft.com> wrote:
>
>|||Hi Clifford ,
the missing permissions are not in the sysprotects in Master db either. Bear
in mind that these missing permissions are set specificly to the user
assigned to my production database, and these users do not exist in Master
database anyway.
As I said before , I noticed that, everytime I set permission to the
production database, it only goes into syspermissions, so I wrote this scrip
t
to list out permission on my production database, and it works fine.
********************
select sysusers.name [USER_NAME], sysobjects.name [OBJECTS] ,
case actadd
when 1 then 'SELECT ONLY'
when 2 then 'UPDATE ONLY'
when 3 then 'SELECT + UPDATE'
when 4 then 'DRI'
when 5 then 'SELECT + DRI'
when 8 then 'INSERT ONLY'
when 9 then 'SELECT + INSERT'
when 27 then 'SEL+INST+UPDT+DEL'
when 31 then 'SEL+INST+UPDT+DEL+DRI'
when 32 then 'SP EXECUTED'
END [PERMISSION]
from sysobjects
inner join syspermissions
on sysobjects.id = syspermissions.id
inner join sysusers
on sysusers.uid = syspermissions.grantee
and sysusers.name = 'myusername'
order by objects
***********************
So even though I cannot see the green tick in EM interface, I can still see
the setting permissions from that scripts.
Still, there is no way to resolve this issue, as far as I know anyway .. :-(
Jack
"Clifford Dibble" wrote:
[vbcol=seagreen]
> Hi,
> By any chance were these permissions on system objects in master.dbo?
> AFAIK these were never preserved during upgrades.
> Note - In Yukon, we now preserve permissions on system objects during the
> upgrade process. So if you DENY EXECUTE TO PUBLIC on master.dbo.xp_cmdshe
ll,
> we will remember that during the upgrade.
> Regards,
> Clifford Dibble
> Program Manager
> SQL Server Engine
>
> "Sue Hoegemeier" wrote:
>|||HI Jack,
Can you run the following query and report the result?
select id, uid, type from sysobjects where name = 'sysprotects'
select * from syscolumns where id = object_id('dbo.sysprotects')
Thanks
Andrew
SQL Server Engine
"Jack Yao" wrote:
[vbcol=seagreen]
> Hi Clifford ,
> the missing permissions are not in the sysprotects in Master db either. Be
ar
> in mind that these missing permissions are set specificly to the user
> assigned to my production database, and these users do not exist in Master
> database anyway.
> As I said before , I noticed that, everytime I set permission to the
> production database, it only goes into syspermissions, so I wrote this scr
ipt
> to list out permission on my production database, and it works fine.
> ********************
> select sysusers.name [USER_NAME], sysobjects.name [OBJECTS] ,
> case actadd
> when 1 then 'SELECT ONLY'
> when 2 then 'UPDATE ONLY'
> when 3 then 'SELECT + UPDATE'
> when 4 then 'DRI'
> when 5 then 'SELECT + DRI'
> when 8 then 'INSERT ONLY'
> when 9 then 'SELECT + INSERT'
> when 27 then 'SEL+INST+UPDT+DEL'
> when 31 then 'SEL+INST+UPDT+DEL+DRI'
> when 32 then 'SP EXECUTED'
> END [PERMISSION]
> from sysobjects
> inner join syspermissions
> on sysobjects.id = syspermissions.id
> inner join sysusers
> on sysusers.uid = syspermissions.grantee
> and sysusers.name = 'myusername'
> order by objects
> ***********************
> So even though I cannot see the green tick in EM interface, I can still se
e
> the setting permissions from that scripts.
> Still, there is no way to resolve this issue, as far as I know anyway .. :
-(
> Jack
>
> "Clifford Dibble" wrote:
>|||Hi Andrew,
sorry for the mess, it is difficult to show you the results of second query
in proper format since it has too many columns to show in this tiny space
here, but here we go:
the query select id, uid, type from sysobjects where name = 'sysprotects'
return the following results :
id uid type
23 1 S
the query "select * from syscolumns where id = object_id('dbo.sysprotects')"
return:
id 23 56 1 56 4 10 0 1 4 0 0 0 0 0 0 1 N
ULL 2 NULL -1553186121 0 56 7 NULL 1
0 0 0 0 0 NULL NULL
uid 23 52 1 52 2 5 0 2 8 0 0 0 0 0 0 2 N
ULL 6 NULL -1553186121 0 52 6 NULL 5
0 0 0 0 NULL NULL
action 23 48 1 48 1 3 0 3 10 0 0 0 0 0 0
3 NULL 8 NULL -1553186121 0 48 5 NU
LL 3 0 0 0 0 NULL NULL
protecttype 23 48 1 48 1 3 0 4 11 0 0 0
0 0 0 4 NULL 9 NULL -1553186121 0 48
5 NULL 3 0 0 0 0 NULL NULL
columns 23 165 2 165 4000 0 0 5 -1 0 0 0 0 0 0 5 NULL -1 NULL -1553186121 24
37 4 NULL 4000 NULL 0 0 1 NULL NULL
grantor 23 52 1 52 2 5 0 6 12 0 0 0 0 0
0 6 NULL 10 NULL -1553186121 0 52 6
NULL 5 0 0 0 0 NULL NULL
Please let me know how you go with it
Jack
"Andrew Zhu" wrote:
[vbcol=seagreen]
> HI Jack,
> Can you run the following query and report the result?
> select id, uid, type from sysobjects where name = 'sysprotects'
> select * from syscolumns where id = object_id('dbo.sysprotects')
> Thanks
> Andrew
> SQL Server Engine
> "Jack Yao" wrote:
>|||Hi Jack, I have experienced the same issue. On my site it was caused by
rights being granted to any object that has a type other than table,
view or proc. Use the following script to detect.
select b.name, b.type, a.* from syspermissions a, sysobjects b where
a.id = b.id
and b.type in ('k', ' c', 'd' , 'f', 'k', 'tr')
order by b.type
To resolve, you must remove the offending syspermissions entries before
upgrading. As soon as SQL2000 the upgrade hits one of the problem
entries it stops populating sysprotects. I cant see any way to resolve
the issue after the 2000 upgrade.
Use the following script to delete the problem entries.
sp_configure 'allow updates', 1
go
reconfigure with override
go
delete from syspermissions where id in (select b.id from syspermissions
a, sysobjects b where a.id = b.id
and b.type in ('k', ' c', 'd' , 'f', 'k', 'tr'))
go
sp_configure 'allow updates', 0
go
reconfigure with override
go
Hope this helps.
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!|||Hi Derek ,
That is exactly what I was talking about , the reason why sysprotects stop
populating the records.
Sadly , my situation is in the limbo since I have already gone through the
upgrade and the records in sysprotects are long gone (so re-upgrade ain't
gonna do any good).
Nevertheless , for the love of SQL :-) , do you habppen to know why SQL
stop populating sysprotects as soon as it hits those offending permissions ?
I mean, what make granting permission to those objects offend SQL at the
beginning with ?
ta
Jack
"Derek" wrote:

> Hi Jack, I have experienced the same issue. On my site it was caused by
> rights being granted to any object that has a type other than table,
> view or proc. Use the following script to detect.
> select b.name, b.type, a.* from syspermissions a, sysobjects b where
> a.id = b.id
> and b.type in ('k', ' c', 'd' , 'f', 'k', 'tr')
> order by b.type
> To resolve, you must remove the offending syspermissions entries before
> upgrading. As soon as SQL2000 the upgrade hits one of the problem
> entries it stops populating sysprotects. I cant see any way to resolve
> the issue after the 2000 upgrade.
> Use the following script to delete the problem entries.
> sp_configure 'allow updates', 1
> go
> reconfigure with override
> go
> delete from syspermissions where id in (select b.id from syspermissions
> a, sysobjects b where a.id = b.id
> and b.type in ('k', ' c', 'd' , 'f', 'k', 'tr'))
> go
> sp_configure 'allow updates', 0
> go
> reconfigure with override
> go
> Hope this helps.
>
> *** Sent via Developersdex http://www.codecomments.com ***
> Don't just participate in USENET...get rewarded for it!
>|||Hi Jack, my guess is that it stops populating sysprotects because SQL
server isn't designed to allow the setting of permissions on objects
like triggers, constraints, primary keys etc. You can't do it through
Enterprise Manager on 7.0 or 2000.
I have traced the problem on our system to a bad script that allocated
all permissions to all objects. During the upgrade, SQL 2000 probably
doesn't know what to do with the offending permission records and just
stops the load of sysprotects, which in turn means no further records
will go in when new permissions are assigned after the upgrade.
If you have already upgraded to 2000, I guess you could fix it by
creating a new database and copying over the objects and data, and then
setting the permissions. That way you have a new sysprotects table.
Cheers
Derek
*** Sent via Developersdex http://www.codecomments.com ***
Don't just participate in USENET...get rewarded for it!|||Hi Derek ,
not sure what you mean by "create new database and copy over the objects and
data".
Did you mean copy it with the copy database wizard or use the old
fashion "backup/restore" manually ?
Jack
"Derek" wrote:

> Hi Jack, my guess is that it stops populating sysprotects because SQL
> server isn't designed to allow the setting of permissions on objects
> like triggers, constraints, primary keys etc. You can't do it through
> Enterprise Manager on 7.0 or 2000.
> I have traced the problem on our system to a bad script that allocated
> all permissions to all objects. During the upgrade, SQL 2000 probably
> doesn't know what to do with the offending permission records and just
> stops the load of sysprotects, which in turn means no further records
> will go in when new permissions are assigned after the upgrade.
> If you have already upgraded to 2000, I guess you could fix it by
> creating a new database and copying over the objects and data, and then
> setting the permissions. That way you have a new sysprotects table.
> Cheers
> Derek
>
> *** Sent via Developersdex http://www.codecomments.com ***
> Don't just participate in USENET...get rewarded for it!
>