Showing posts with label modify. Show all posts
Showing posts with label modify. Show all posts

Wednesday, March 28, 2012

Read, modify table (locking) question

I am quite new to MS SQL, and I want to read rows from a todo table, and
when a row has been processed, I want to delete that row.

Ages ago in MySQL I would probably have locked the table, select, process
a row, delete a row, unlock the table.

I have been reading through the documentation from MS SQL, but it's not
clear what exactly I should do.

Since I want to lock only one table in the select (the others just provide
data, and are not modified), what's a good solution?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.htmlJohn Bokma <john@.castleamber.com> wrote:

> I am quite new to MS SQL, and I want to read rows from a todo table,
> and when a row has been processed, I want to delete that row.
> Ages ago in MySQL I would probably have locked the table, select,
> process a row, delete a row, unlock the table.
> I have been reading through the documentation from MS SQL, but it's
> not clear what exactly I should do.
> Since I want to lock only one table in the select (the others just
> provide data, and are not modified), what's a good solution?

what I came up with:

BEGIN TRANSACTION
SELECT TOP 10 .... FROM A WITH(ROWLOCK,HOLDLOCK), B, C WHERE ...
....
... delete each row in A in TOP 10
END TRANSACTION

what I want to prevent is that more then one process selects 10 rows, and
starts to delete rows (from A) that are selected by any of the other ones.

to me, a rowlock is sufficient, and fine grained enough, and the hold lock
holds it to the end of transaction.

Am I right?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html|||John Bokma (john@.castleamber.com) writes:
> John Bokma <john@.castleamber.com> wrote:
>> I am quite new to MS SQL, and I want to read rows from a todo table,
>> and when a row has been processed, I want to delete that row.
>>
>> Ages ago in MySQL I would probably have locked the table, select,
>> process a row, delete a row, unlock the table.
>>
>> I have been reading through the documentation from MS SQL, but it's
>> not clear what exactly I should do.
>>
>> Since I want to lock only one table in the select (the others just
>> provide data, and are not modified), what's a good solution?
> what I came up with:
> BEGIN TRANSACTION
> SELECT TOP 10 .... FROM A WITH(ROWLOCK,HOLDLOCK), B, C WHERE ...
> ...
> ... delete each row in A in TOP 10
> END TRANSACTION
> what I want to prevent is that more then one process selects 10 rows, and
> starts to delete rows (from A) that are selected by any of the other ones.
> to me, a rowlock is sufficient, and fine grained enough, and the hold lock
> holds it to the end of transaction.
> Am I right?

Difficult to say with the small amount of information, but it does not
seem quite right.

The smallest change you should do is to is to remove ROWLOCK, but insert
UPDLOCK instead. ROWLOCK is sort of meaningless. Either you have a good
index to locate the rows, and you will get rowlocks. Or you don't have
good indexes, and SQL Server will have to lock the entire table.

UPDLOCK is a shared lock that does not block other readers, but it
blocks others that try to use UPDLOCK. With only HOLDLOCK and you have
two processes coming to the place at the same time, will lock the same
10 ten rows, and then when they to delete, they will block each other.

But there may be other things you could consider. It could be the case
that application locks are a better choice. An application lock is a
lock on a user-defined resource (that is a text string) which is handled
by the Lock Manager. But I know too little about your application to
tell whether it would fit it here.

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

Monday, March 26, 2012

read only Stored Procedures?

Is there a way to make Stored Procedures "read only" or make it so no one can
modify the SP?Yes, just don't give users who shouldn't be to change procedures permissions
to do so. If it is an existing problem, remove the relevant users from the
sysadmin, db_owner and db_ddladmin roles. Then you can either REVOKE or DENY
the CREATE PROCEDURE statement to the users.
--
Jacco Schalkwijk
SQL Server MVP
"J Jetson" <JJetson@.discussions.microsoft.com> wrote in message
news:638520E1-FB4D-4960-8345-56A174249A45@.microsoft.com...
> Is there a way to make Stored Procedures "read only" or make it so no one
> can
> modify the SP?

Wednesday, March 7, 2012

RDA with Identity column.

Hi Everyone:

I am new to Mobile programming. I am now working on a mobile project. I encounter an issue when I sync the data:

cause I can't modify the schema, so I have to use RDA instead of Merge replication on sql server 2005.

However, There is an identiy column on each table I will pull them down to local mobile database. And I will use those identity columns to connect tables. Even worse, the photo's new name will combine the photoID which is an identity column. There would an issue, if i sync data, the photoID would be same on different local mobile databases. And there would be generate same identity value when users sync data.

How can I avoid those issues? If you have any good ideas, please help me out so that I can meet the project deadline.

Thanks

James

Anyone knows how to manually handle identity management.

Cause my identity column will clash when Sync data.

James

|||

You need to call

ALTER TABLE <TableName> ALTER COLUMN <ColumnName> <ColumnDataType> IDENTITY(<Seed>,<Step>)

to set the different identity on the pulled table.

Note, only Seed and Step can be changed in the above table.

Thanks,

Laxmi Narsimha Rao ORUGANTI

|||

Hi Laxmi,

Thank you for your reply. I realize that I can use the command you gave to change the SEED range to avoid the clash. But if I change seed, when I push the data back to Remote SQL server 2005 database, should I disable the identity column on remote database, then push mobile client table data, then set identity column back on remote DB. It seems lots of work to manually handle identity column clash. Do we have any alternative way to do it, since I can't use merge replication to change the database schema, what about XML web service? I think should be same, but if we use Web Service, the performance should be down 10-15%, that's why i don't wanna use it since we choose SQL server 2005 as our database.

Your reply is really appreciated. Thanks!

James

|||

I need to point is that someone will work on remote db through classical ASP. If I change the identity Seed. It will effect mobile client?

If anybody has the same issues?

Thanks

James

|||

No need to worry about disabling the IDENTITY on server. RDA while pushing automatically does "SET IDENTITY_INSERT <TableName> ON/OFF" on SQL Server to get the same IDENTITY column values onto the server table.

Thanks,

Laxmi

|||

Thank Laxmi,

your reply does help me. I will try it out. But we have 200 hundred of Mobile clients. We have to set the identity range for each one.

Any other good ideas for solving the identity clash.

Thanks.

James

|||That is why there is Merge Replication! Why not go for that?|||

Because I can't modify the Sql server 2005 DB schema. They are using identity ID. I wish I could use Merge replication. I can save my code lines as well.

Thank you . Laxmi. You are the rock!

James

|||Same here, merge replication is not a option.

Anyone found a suitable solution for this challenge?

Thanx
|||What I have done is to set the ID range for each person. So that they won't clash.

RDA with Identity column.

Hi Everyone:

I am new to Mobile programming. I am now working on a mobile project. I encounter an issue when I sync the data:

cause I can't modify the schema, so I have to use RDA instead of Merge replication on sql server 2005.

However, There is an identiy column on each table I will pull them down to local mobile database. And I will use those identity columns to connect tables. Even worse, the photo's new name will combine the photoID which is an identity column. There would an issue, if i sync data, the photoID would be same on different local mobile databases. And there would be generate same identity value when users sync data.

How can I avoid those issues? If you have any good ideas, please help me out so that I can meet the project deadline.

Thanks

James

Anyone knows how to manually handle identity management.

Cause my identity column will clash when Sync data.

James

|||

You need to call

ALTER TABLE <TableName> ALTER COLUMN <ColumnName> <ColumnDataType> IDENTITY(<Seed>,<Step>)

to set the different identity on the pulled table.

Note, only Seed and Step can be changed in the above table.

Thanks,

Laxmi Narsimha Rao ORUGANTI

|||

Hi Laxmi,

Thank you for your reply. I realize that I can use the command you gave to change the SEED range to avoid the clash. But if I change seed, when I push the data back to Remote SQL server 2005 database, should I disable the identity column on remote database, then push mobile client table data, then set identity column back on remote DB. It seems lots of work to manually handle identity column clash. Do we have any alternative way to do it, since I can't use merge replication to change the database schema, what about XML web service? I think should be same, but if we use Web Service, the performance should be down 10-15%, that's why i don't wanna use it since we choose SQL server 2005 as our database.

Your reply is really appreciated. Thanks!

James

|||

I need to point is that someone will work on remote db through classical ASP. If I change the identity Seed. It will effect mobile client?

If anybody has the same issues?

Thanks

James

|||

No need to worry about disabling the IDENTITY on server. RDA while pushing automatically does "SET IDENTITY_INSERT <TableName> ON/OFF" on SQL Server to get the same IDENTITY column values onto the server table.

Thanks,

Laxmi

|||

Thank Laxmi,

your reply does help me. I will try it out. But we have 200 hundred of Mobile clients. We have to set the identity range for each one.

Any other good ideas for solving the identity clash.

Thanks.

James

|||That is why there is Merge Replication! Why not go for that?|||

Because I can't modify the Sql server 2005 DB schema. They are using identity ID. I wish I could use Merge replication. I can save my code lines as well.

Thank you . Laxmi. You are the rock!

James

|||Same here, merge replication is not a option.

Anyone found a suitable solution for this challenge?

Thanx|||What I have done is to set the ID range for each person. So that they won't clash.

RDA with Identity column.

Hi Everyone:

I am new to Mobile programming. I am now working on a mobile project. I encounter an issue when I sync the data:

cause I can't modify the schema, so I have to use RDA instead of Merge replication on sql server 2005.

However, There is an identiy column on each table I will pull them down to local mobile database. And I will use those identity columns to connect tables. Even worse, the photo's new name will combine the photoID which is an identity column. There would an issue, if i sync data, the photoID would be same on different local mobile databases. And there would be generate same identity value when users sync data.

How can I avoid those issues? If you have any good ideas, please help me out so that I can meet the project deadline.

Thanks

James

Anyone knows how to manually handle identity management.

Cause my identity column will clash when Sync data.

James

|||

You need to call

ALTER TABLE <TableName> ALTER COLUMN <ColumnName> <ColumnDataType> IDENTITY(<Seed>,<Step>)

to set the different identity on the pulled table.

Note, only Seed and Step can be changed in the above table.

Thanks,

Laxmi Narsimha Rao ORUGANTI

|||

Hi Laxmi,

Thank you for your reply. I realize that I can use the command you gave to change the SEED range to avoid the clash. But if I change seed, when I push the data back to Remote SQL server 2005 database, should I disable the identity column on remote database, then push mobile client table data, then set identity column back on remote DB. It seems lots of work to manually handle identity column clash. Do we have any alternative way to do it, since I can't use merge replication to change the database schema, what about XML web service? I think should be same, but if we use Web Service, the performance should be down 10-15%, that's why i don't wanna use it since we choose SQL server 2005 as our database.

Your reply is really appreciated. Thanks!

James

|||

I need to point is that someone will work on remote db through classical ASP. If I change the identity Seed. It will effect mobile client?

If anybody has the same issues?

Thanks

James

|||

No need to worry about disabling the IDENTITY on server. RDA while pushing automatically does "SET IDENTITY_INSERT <TableName> ON/OFF" on SQL Server to get the same IDENTITY column values onto the server table.

Thanks,

Laxmi

|||

Thank Laxmi,

your reply does help me. I will try it out. But we have 200 hundred of Mobile clients. We have to set the identity range for each one.

Any other good ideas for solving the identity clash.

Thanks.

James

|||That is why there is Merge Replication! Why not go for that?|||

Because I can't modify the Sql server 2005 DB schema. They are using identity ID. I wish I could use Merge replication. I can save my code lines as well.

Thank you . Laxmi. You are the rock!

James

|||Same here, merge replication is not a option.

Anyone found a suitable solution for this challenge?

Thanx|||What I have done is to set the ID range for each person. So that they won't clash.