Showing posts with label application. Show all posts
Showing posts with label application. Show all posts

Friday, March 30, 2012

raiseerror does not raise exception

Hi friends

i've a stored proc (sql 2005) that'll raiseerror statement when something violated.

but my C# application that calls this stored proc does not any throw exception when this happens !!

i remember visual basic used to through an exception for this type of things.

is it different in C# and how do we handle this scenario ?

Thanks for ur ideas.

can you show us the SP code?

moving thread to the SQL Forums

|||am doing something like below

BEGIN TRY
BEGIN TRAN

/* one of below statements may result in error*/
insert into mytable1 values (...blah..)
insert into mytable1 values (...blah..)

COMMIT
END TRY
BEGIN CATCH
DECLARE

@.ErrorMessage VARCHAR(4000)
SELECT @.ErrorMessage = 'Message: '+ ERROR_MESSAGE();

raiserror (@.ErrorMessage)
END CATCH;

i used executescalar ,executereader (in C#) but none of them throw any exception but i could access data CATCH returns though|||

Hi prk,

You'll need to specify the severity and state during the RAISERROR call:

raiserror (@.ErrorMessage,16,1)

Cheers,

Rob

|||

Thanks Rob

will give that a try

Wednesday, March 28, 2012

RAID Configuration for SQL Server ...

Hi,
We are rolling out an ERP application from MS across our
organisation. And we are using MS SQL Server as the back
end. In this regard, I have few queries -
a) What would be the optimum RAID configuration for the
SQL Server database? Can someone point me towards any
guide?
b) Still we are in the initial stage of our roll out. So
we are not too sure about the growth of database. Is
there any tools available for estimating the size of
database?
Thanks in advance,
Harish MohanbabuHi
You may want to check out the book "SQL Server 2000 Performance Tuning
Technical Reference" which talks about this in some depth ISBN 0-7356-1270-6
Also you may want to browse the site:
http://www.sql-server-performance.com/
Such as:
http://www.sql-server-performance.com/ultimate_sql_server.asp
The topic "Estimating the Size of a Database" in books online should help
you decide how big the data may get:
mk:@.MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\cre
atedb.chm::/cm_8_des_02_2h45.htm
John
"Harish Mohanbabu" <anonymous@.discussions.microsoft.com> wrote in message
news:067c01c3cdfb$2d441db0$a101280a@.phx.gbl...
> Hi,
> We are rolling out an ERP application from MS across our
> organisation. And we are using MS SQL Server as the back
> end. In this regard, I have few queries -
> a) What would be the optimum RAID configuration for the
> SQL Server database? Can someone point me towards any
> guide?
> b) Still we are in the initial stage of our roll out. So
> we are not too sure about the growth of database. Is
> there any tools available for estimating the size of
> database?
> Thanks in advance,
> Harish Mohanbabu|||Thanks for your kind and quick reply ....
>--Original Message--
>Hi
>You may want to check out the book "SQL Server 2000
Performance Tuning
>Technical Reference" which talks about this in some
depth ISBN 0-7356-1270-6
>Also you may want to browse the site:
>http://www.sql-server-performance.com/
>Such as:
>http://www.sql-server-
performance.com/ultimate_sql_server.asp
>The topic "Estimating the Size of a Database" in books
online should help
>you decide how big the data may get:
>mk:@.MSITStore:C:\Program%20Files\Microsoft%20SQL%
20Server\80\Tools\Books\cre
>atedb.chm::/cm_8_des_02_2h45.htm
>John

Monday, March 26, 2012

RAID 1 Configuration

We are going to deploy a SQL Server 2000 for an
application which information is static. We just provide
queries for end users.
In order to minimize the backup procedure, we would like
to use RAID 1 mirroring configuration. Is this choice OK ?Your backup procedure has nothing to do with your choice of RAID
configuration (unless I'm missing something here). Performance and
Redundancy has.
Aramid
On Wed, 6 Apr 2005 22:50:08 -0700, "Peter"
<anonymous@.discussions.microsoft.com> wrote:
>We are going to deploy a SQL Server 2000 for an
>application which information is static. We just provide
>queries for end users.
>In order to minimize the backup procedure, we would like
>to use RAID 1 mirroring configuration. Is this choice OK ?|||We will make a backup of whole drive once a week as the
data is static.
The reason why we think RAID 1 is because we think that if
one of the disk breaks down, the other one still works
properly.
Re the performance issue, we would like to know what is
the pros and cons of RAID 1.
Thanks
>--Original Message--
>Your backup procedure has nothing to do with your choice
of RAID
>configuration (unless I'm missing something here).
Performance and
>Redundancy has.
>Aramid
>On Wed, 6 Apr 2005 22:50:08 -0700, "Peter"
><anonymous@.discussions.microsoft.com> wrote:
>>We are going to deploy a SQL Server 2000 for an
>>application which information is static. We just
provide
>>queries for end users.
>>In order to minimize the backup procedure, we would like
>>to use RAID 1 mirroring configuration. Is this choice
OK ?
>.
>|||Hi
The more spindles you have, the better your performance.
RAID-1 is good for performance and tolerance to failure, but RAID-10 (Or
0+1) is much better as it is striped and mirrored. So, you have the benefit
of Mirroring, plus you have multiple drives doing requests at the same time.
SCSI, not IDE or SATA.
Hardware mirroring, not software.
RAID-5 is evil, don't go there.
SQL Serrver does it's IO in 64k blocks (8 extents of 8k pages), so format
NTFS using 64Kb blocks.
Regards
Mike
"Peter" wrote:
> We will make a backup of whole drive once a week as the
> data is static.
> The reason why we think RAID 1 is because we think that if
> one of the disk breaks down, the other one still works
> properly.
> Re the performance issue, we would like to know what is
> the pros and cons of RAID 1.
> Thanks
> >--Original Message--
> >Your backup procedure has nothing to do with your choice
> of RAID
> >configuration (unless I'm missing something here).
> Performance and
> >Redundancy has.
> >
> >Aramid
> >
> >On Wed, 6 Apr 2005 22:50:08 -0700, "Peter"
> ><anonymous@.discussions.microsoft.com> wrote:
> >
> >>We are going to deploy a SQL Server 2000 for an
> >>application which information is static. We just
> provide
> >>queries for end users.
> >>
> >>In order to minimize the backup procedure, we would like
> >>to use RAID 1 mirroring configuration. Is this choice
> OK ?
> >
> >.
> >
>

RAID & Drive Type for Server?

I'm shopping for a dedicated server service to host my web application that
uses SQL Server 2000 (eventually 2005) with everything on one box (IIS, COM+
application, etc.) although backups will go to a NAS unit. My database is
currently less than 300mb but I would certainly want to accommodate a 3gb+
database and accommodate ten or a hundred concurrent web users as my
business hopefully improves.
One of the hosting companies offers a number of RAID choices. Their choices
include RAID 0 (not fault tolerant - won't be using that), RAID 1 with two
drives, RAID 5 with three drives, and RAID 10 with four drives. There is
apparently NOT the luxury of further configuration options like more drives
using one RAID configuration, mixing RAID flavors with more drives, etc.
There is also the choice between using SATA II drives (7200 or 10000 rpm,
250 to 750gbs - about 200gb more than I will ever need) or SA-SCSI (10 or
15K rpm, 73gb only)
With these constrained choices, what would be your recommendations for the
best redundancy and best fault tolerance where the speed of read-writes is
less of an issue (because no user would ever notice the difference until the
server was running at 50% cpu or more)?
My thought is the more drives the better, the fast the rpm the better, but I
don't know enough about whether SATA drives is the next new thing compared
to SCSI technology, and I don't know whether the difference between RAID 5
or 10 would really make any difference (besides decreasing the chance of
failure with one more drive).
Thanks for any recommendations or thoughts.Given the small size of your database, I'd probably go with RAID 1. It would
give you enough space, less expensive than RAID 10, lower overhead than RAID
5, and better protection than RAID 0. And again given what you described, I'd
probably not want to explore any new(er) technologies, and I would just go
with the mature SCSI, have it set up, get it running and forget about it.
Linchi
"Don Miller" wrote:
> I'm shopping for a dedicated server service to host my web application that
> uses SQL Server 2000 (eventually 2005) with everything on one box (IIS, COM+
> application, etc.) although backups will go to a NAS unit. My database is
> currently less than 300mb but I would certainly want to accommodate a 3gb+
> database and accommodate ten or a hundred concurrent web users as my
> business hopefully improves.
> One of the hosting companies offers a number of RAID choices. Their choices
> include RAID 0 (not fault tolerant - won't be using that), RAID 1 with two
> drives, RAID 5 with three drives, and RAID 10 with four drives. There is
> apparently NOT the luxury of further configuration options like more drives
> using one RAID configuration, mixing RAID flavors with more drives, etc.
> There is also the choice between using SATA II drives (7200 or 10000 rpm,
> 250 to 750gbs - about 200gb more than I will ever need) or SA-SCSI (10 or
> 15K rpm, 73gb only)
> With these constrained choices, what would be your recommendations for the
> best redundancy and best fault tolerance where the speed of read-writes is
> less of an issue (because no user would ever notice the difference until the
> server was running at 50% cpu or more)?
> My thought is the more drives the better, the fast the rpm the better, but I
> don't know enough about whether SATA drives is the next new thing compared
> to SCSI technology, and I don't know whether the difference between RAID 5
> or 10 would really make any difference (besides decreasing the chance of
> failure with one more drive).
> Thanks for any recommendations or thoughts.
>
>|||I tend to agree with Linchi given the small size of the db. Most if not all
of the data should be in cache anyway so there should be very little disk
I/O on a constant basis.
--
Andrew J. Kelly SQL MVP
"Don Miller" <nospam@.nospam.com> wrote in message
news:eAH7N0W9GHA.3348@.TK2MSFTNGP03.phx.gbl...
> I'm shopping for a dedicated server service to host my web application
> that
> uses SQL Server 2000 (eventually 2005) with everything on one box (IIS,
> COM+
> application, etc.) although backups will go to a NAS unit. My database is
> currently less than 300mb but I would certainly want to accommodate a 3gb+
> database and accommodate ten or a hundred concurrent web users as my
> business hopefully improves.
> One of the hosting companies offers a number of RAID choices. Their
> choices
> include RAID 0 (not fault tolerant - won't be using that), RAID 1 with two
> drives, RAID 5 with three drives, and RAID 10 with four drives. There is
> apparently NOT the luxury of further configuration options like more
> drives
> using one RAID configuration, mixing RAID flavors with more drives, etc.
> There is also the choice between using SATA II drives (7200 or 10000 rpm,
> 250 to 750gbs - about 200gb more than I will ever need) or SA-SCSI (10 or
> 15K rpm, 73gb only)
> With these constrained choices, what would be your recommendations for the
> best redundancy and best fault tolerance where the speed of read-writes is
> less of an issue (because no user would ever notice the difference until
> the
> server was running at 50% cpu or more)?
> My thought is the more drives the better, the fast the rpm the better, but
> I
> don't know enough about whether SATA drives is the next new thing compared
> to SCSI technology, and I don't know whether the difference between RAID 5
> or 10 would really make any difference (besides decreasing the chance of
> failure with one more drive).
> Thanks for any recommendations or thoughts.
>|||One small caveat though, if a small database does a lot of writes, you can
still stress the I/O subsystem.
Linchi
"Andrew J. Kelly" wrote:
> I tend to agree with Linchi given the small size of the db. Most if not all
> of the data should be in cache anyway so there should be very little disk
> I/O on a constant basis.
> --
> Andrew J. Kelly SQL MVP
> "Don Miller" <nospam@.nospam.com> wrote in message
> news:eAH7N0W9GHA.3348@.TK2MSFTNGP03.phx.gbl...
> > I'm shopping for a dedicated server service to host my web application
> > that
> > uses SQL Server 2000 (eventually 2005) with everything on one box (IIS,
> > COM+
> > application, etc.) although backups will go to a NAS unit. My database is
> > currently less than 300mb but I would certainly want to accommodate a 3gb+
> > database and accommodate ten or a hundred concurrent web users as my
> > business hopefully improves.
> >
> > One of the hosting companies offers a number of RAID choices. Their
> > choices
> > include RAID 0 (not fault tolerant - won't be using that), RAID 1 with two
> > drives, RAID 5 with three drives, and RAID 10 with four drives. There is
> > apparently NOT the luxury of further configuration options like more
> > drives
> > using one RAID configuration, mixing RAID flavors with more drives, etc.
> >
> > There is also the choice between using SATA II drives (7200 or 10000 rpm,
> > 250 to 750gbs - about 200gb more than I will ever need) or SA-SCSI (10 or
> > 15K rpm, 73gb only)
> >
> > With these constrained choices, what would be your recommendations for the
> > best redundancy and best fault tolerance where the speed of read-writes is
> > less of an issue (because no user would ever notice the difference until
> > the
> > server was running at 50% cpu or more)?
> >
> > My thought is the more drives the better, the fast the rpm the better, but
> > I
> > don't know enough about whether SATA drives is the next new thing compared
> > to SCSI technology, and I don't know whether the difference between RAID 5
> > or 10 would really make any difference (besides decreasing the chance of
> > failure with one more drive).
> >
> > Thanks for any recommendations or thoughts.
> >
> >
>
>|||Thanks for the quick reply yesterday and for your thoughts.
"Linchi Shea" <LinchiShea@.discussions.microsoft.com> wrote in message
news:A93579B9-CD81-4E39-89AA-8955C05CB114@.microsoft.com...
> One small caveat though, if a small database does a lot of writes, you can
> still stress the I/O subsystem.
> Linchi
> "Andrew J. Kelly" wrote:
> > I tend to agree with Linchi given the small size of the db. Most if not
all
> > of the data should be in cache anyway so there should be very little
disk
> > I/O on a constant basis.
> >
> > --
> > Andrew J. Kelly SQL MVP
> >
> > "Don Miller" <nospam@.nospam.com> wrote in message
> > news:eAH7N0W9GHA.3348@.TK2MSFTNGP03.phx.gbl...
> > > I'm shopping for a dedicated server service to host my web application
> > > that
> > > uses SQL Server 2000 (eventually 2005) with everything on one box
(IIS,
> > > COM+
> > > application, etc.) although backups will go to a NAS unit. My database
is
> > > currently less than 300mb but I would certainly want to accommodate a
3gb+
> > > database and accommodate ten or a hundred concurrent web users as my
> > > business hopefully improves.
> > >
> > > One of the hosting companies offers a number of RAID choices. Their
> > > choices
> > > include RAID 0 (not fault tolerant - won't be using that), RAID 1 with
two
> > > drives, RAID 5 with three drives, and RAID 10 with four drives. There
is
> > > apparently NOT the luxury of further configuration options like more
> > > drives
> > > using one RAID configuration, mixing RAID flavors with more drives,
etc.
> > >
> > > There is also the choice between using SATA II drives (7200 or 10000
rpm,
> > > 250 to 750gbs - about 200gb more than I will ever need) or SA-SCSI (10
or
> > > 15K rpm, 73gb only)
> > >
> > > With these constrained choices, what would be your recommendations for
the
> > > best redundancy and best fault tolerance where the speed of
read-writes is
> > > less of an issue (because no user would ever notice the difference
until
> > > the
> > > server was running at 50% cpu or more)?
> > >
> > > My thought is the more drives the better, the fast the rpm the better,
but
> > > I
> > > don't know enough about whether SATA drives is the next new thing
compared
> > > to SCSI technology, and I don't know whether the difference between
RAID 5
> > > or 10 would really make any difference (besides decreasing the chance
of
> > > failure with one more drive).
> > >
> > > Thanks for any recommendations or thoughts.
> > >
> > >
> >
> >
> >

Friday, March 23, 2012

RAD apps for db development

Hi
Are there any RAD apps to speed up one-many db application development that
are worth considering?
Thanks
Regards>> Are there any RAD apps to speed up one-many db application development
>> that are worth considering?
What exactly are you trying to achieve here? Rapid App Development is used a
variety of contexts and without knowing the specifics it is hard to comment.
--
Anith|||just sql server centric vb.net database apps
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:u0jDzQycIHA.4140@.TK2MSFTNGP04.phx.gbl...
>> Are there any RAD apps to speed up one-many db application development
>> that are worth considering?
> What exactly are you trying to achieve here? Rapid App Development is used
> a variety of contexts and without knowing the specifics it is hard to
> comment.
> --
> Anith
>|||Delphi, MS VC# 2005, ...etc.
"John" <John@.nospam.infovis.co.uk> wrote in message
news:%23SGxpDIdIHA.4144@.TK2MSFTNGP05.phx.gbl...
> just sql server centric vb.net database apps
> "Anith Sen" <anith@.bizdatasolutions.com> wrote in message
> news:u0jDzQycIHA.4140@.TK2MSFTNGP04.phx.gbl...
> >> Are there any RAD apps to speed up one-many db application development
> >> that are worth considering?
> >
> > What exactly are you trying to achieve here? Rapid App Development is
used
> > a variety of contexts and without knowing the specifics it is hard to
> > comment.
> >
> > --
> > Anith
> >
>

RAD apps for db development

Hi
Are there any RAD apps to speed up one-many db application development that
are worth considering?
Thanks
Regards
>> Are there any RAD apps to speed up one-many db application development[vbcol=seagreen]
What exactly are you trying to achieve here? Rapid App Development is used a
variety of contexts and without knowing the specifics it is hard to comment.
Anith
|||just sql server centric vb.net database apps
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:u0jDzQycIHA.4140@.TK2MSFTNGP04.phx.gbl...
> What exactly are you trying to achieve here? Rapid App Development is used
> a variety of contexts and without knowing the specifics it is hard to
> comment.
> --
> Anith
>

Race condition in SQL Server 2005

hi,
How SQL Server 2005 handel concurrent access to database.
for example. if two application user same data base and same user name to
access it. when both application attempts to access the database how Server
handle this race condition.
Thank in advance
with regards,
Anbu
On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
> hi,
> How SQL Server 2005 handel concurrent access to database.
> for example. if two application user same data base and same user name to
> access it. when both application attempts to access the database how Server
> handle this race condition.
> Thank in advance
> with regards,
> Anbu
What you describe is normal. A user's session is scoped to a
connection not a user name, so there is no special problem associated
with multiple users under the same user name accessing the same
database.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
|||Thanks David,
iam very new to SQL Server. can you refer any links where i can find details
about how race condition is handled by Server.
with regards,
Anbu
"David Portas" wrote:

> On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
>
> What you describe is normal. A user's session is scoped to a
> connection not a user name, so there is no special problem associated
> with multiple users under the same user name accessing the same
> database.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>
|||See Books Online topics related to locking and row versioning
(http://msdn2.microsoft.com/en-us/library/ms187101.aspx).
Keep in mind the scope that David mentioned when reading the documentation.
You will often see the terms "user" and "session" used interchaneably to
refer to a user's session on a database database connection. This is
completely unrelated to the login/user used to connect to SQL Server.
Hope this helps.
Dan Guzman
SQL Server MVP
"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...[vbcol=seagreen]
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
> with regards,
> Anbu
> "David Portas" wrote:
|||"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
>
Really this isn't a true race condition. (Well, I suppose it can be, but
what you seem to be getting at more is simply multiple accesses.)
Keep in mind like any decent RDBMS, SQL Server is designed to handle this.
At a very basic level, "whoever gets there first wins".
But you need to look up blocking and things like Committed vs. uncommitted
read, read serializable. etc.
[vbcol=seagreen]
> with regards,
> Anbu
> "David Portas" wrote:
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
sql

Race condition in SQL Server 2005

hi,
How SQL Server 2005 handel concurrent access to database.
for example. if two application user same data base and same user name to
access it. when both application attempts to access the database how Server
handle this race condition.
Thank in advance
with regards,
AnbuOn 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
> hi,
> How SQL Server 2005 handel concurrent access to database.
> for example. if two application user same data base and same user name to
> access it. when both application attempts to access the database how Serve
r
> handle this race condition.
> Thank in advance
> with regards,
> Anbu
What you describe is normal. A user's session is scoped to a
connection not a user name, so there is no special problem associated
with multiple users under the same user name accessing the same
database.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Thanks David,
iam very new to SQL Server. can you refer any links where i can find details
about how race condition is handled by Server.
with regards,
Anbu
"David Portas" wrote:

> On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
>
> What you describe is normal. A user's session is scoped to a
> connection not a user name, so there is no special problem associated
> with multiple users under the same user name accessing the same
> database.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||See Books Online topics related to locking and row versioning
(http://msdn2.microsoft.com/en-us/library/ms187101.aspx).
Keep in mind the scope that David mentioned when reading the documentation.
You will often see the terms "user" and "session" used interchaneably to
refer to a user's session on a database database connection. This is
completely unrelated to the login/user used to connect to SQL Server.
Hope this helps.
Dan Guzman
SQL Server MVP
"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...[vbcol=seagreen]
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
> with regards,
> Anbu
> "David Portas" wrote:
>|||"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
>
Really this isn't a true race condition. (Well, I suppose it can be, but
what you seem to be getting at more is simply multiple accesses.)
Keep in mind like any decent RDBMS, SQL Server is designed to handle this.
At a very basic level, "whoever gets there first wins".
But you need to look up blocking and things like Committed vs. uncommitted
read, read serializable. etc.
[vbcol=seagreen]
> with regards,
> Anbu
> "David Portas" wrote:
>
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html

Race condition in SQL Server 2005

hi,
How SQL Server 2005 handel concurrent access to database.
for example. if two application user same data base and same user name to
access it. when both application attempts to access the database how Server
handle this race condition.
Thank in advance
with regards,
AnbuOn 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
> hi,
> How SQL Server 2005 handel concurrent access to database.
> for example. if two application user same data base and same user name to
> access it. when both application attempts to access the database how Server
> handle this race condition.
> Thank in advance
> with regards,
> Anbu
What you describe is normal. A user's session is scoped to a
connection not a user name, so there is no special problem associated
with multiple users under the same user name accessing the same
database.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Thanks David,
iam very new to SQL Server. can you refer any links where i can find details
about how race condition is handled by Server.
with regards,
Anbu
"David Portas" wrote:
> On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
> > hi,
> >
> > How SQL Server 2005 handel concurrent access to database.
> > for example. if two application user same data base and same user name to
> > access it. when both application attempts to access the database how Server
> > handle this race condition.
> >
> > Thank in advance
> > with regards,
> > Anbu
>
> What you describe is normal. A user's session is scoped to a
> connection not a user name, so there is no special problem associated
> with multiple users under the same user name accessing the same
> database.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||See Books Online topics related to locking and row versioning
(http://msdn2.microsoft.com/en-us/library/ms187101.aspx).
Keep in mind the scope that David mentioned when reading the documentation.
You will often see the terms "user" and "session" used interchaneably to
refer to a user's session on a database database connection. This is
completely unrelated to the login/user used to connect to SQL Server.
Hope this helps.
Dan Guzman
SQL Server MVP
"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
> with regards,
> Anbu
> "David Portas" wrote:
>> On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
>> > hi,
>> >
>> > How SQL Server 2005 handel concurrent access to database.
>> > for example. if two application user same data base and same user name
>> > to
>> > access it. when both application attempts to access the database how
>> > Server
>> > handle this race condition.
>> >
>> > Thank in advance
>> > with regards,
>> > Anbu
>>
>> What you describe is normal. A user's session is scoped to a
>> connection not a user name, so there is no special problem associated
>> with multiple users under the same user name accessing the same
>> database.
>> --
>> David Portas, SQL Server MVP
>> Whenever possible please post enough code to reproduce your problem.
>> Including CREATE TABLE and INSERT statements usually helps.
>> State what version of SQL Server you are using and specify the content
>> of any error messages.
>> SQL Server Books Online:
>> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
>> --
>>|||"anbu" <anbu@.discussions.microsoft.com> wrote in message
news:53410F4E-C145-446F-8F28-DFB6A24F2823@.microsoft.com...
> Thanks David,
> iam very new to SQL Server. can you refer any links where i can find
> details
> about how race condition is handled by Server.
>
Really this isn't a true race condition. (Well, I suppose it can be, but
what you seem to be getting at more is simply multiple accesses.)
Keep in mind like any decent RDBMS, SQL Server is designed to handle this.
At a very basic level, "whoever gets there first wins".
But you need to look up blocking and things like Committed vs. uncommitted
read, read serializable. etc.
> with regards,
> Anbu
> "David Portas" wrote:
>> On 29 May, 07:00, anbu <a...@.discussions.microsoft.com> wrote:
>> > hi,
>> >
>> > How SQL Server 2005 handel concurrent access to database.
>> > for example. if two application user same data base and same user name
>> > to
>> > access it. when both application attempts to access the database how
>> > Server
>> > handle this race condition.
>> >
>> > Thank in advance
>> > with regards,
>> > Anbu
>>
>> What you describe is normal. A user's session is scoped to a
>> connection not a user name, so there is no special problem associated
>> with multiple users under the same user name accessing the same
>> database.
>> --
>> David Portas, SQL Server MVP
>> Whenever possible please post enough code to reproduce your problem.
>> Including CREATE TABLE and INSERT statements usually helps.
>> State what version of SQL Server you are using and specify the content
>> of any error messages.
>> SQL Server Books Online:
>> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
>> --
>>
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html

Friday, March 9, 2012

Quick Question regarding number of instances...

I am developing an application for a client who already has a liscensed SQL server running. They have a software package that requires an instance using "Windows Only" for the authentication mode. I need an instance to use "SQL Server and Windows" for my application to be able to speak with the server from other computers on their network (or at least I believe I do...).

My question is, "is there any restrictions on the 'number' of instances an institution can run on a computer that is liscensed to run the software"?

Alternately, if there is a limit, I need to figure out if it is possible to set up the application (VB6, btw) to connect to the server from outside the host computer if it is using "Windows Only" Authentication. The application will be installed on several computers in the client's network and I'm not familiar with how to dynamicly poll the 'current Windows Login' information.

It would be much simpler for the application to create a new instance with the desired authentication method, but is there a security issue in doing this? Would the data be any more at risk to outside corruption using the SQL server authentication mode?Number of named instances if default instance is already present is 15. Total number of instances including default instance cannot exceed 16.

Windows authentication is safer simply because there is no need to provide user ID and password in clear text. If you use OLE DB Provider then "Intgrated Security=SSPI'and for DSN-less connections, - "Trusted_Connection=Yes"

Saturday, February 25, 2012

Quick and easy question, Sql update method . . .

I am new to Sql, so I think this is a really easy question.

But I am working on a 2005 MSSQL database.

When I add columns to an existing application with data and tables already being used, and the new column will be set to Database Null.

What is an easy way to quickly add the data to all the rows in the table.

For example if I am adding a checkbox, I've been doing it manually, add the column, change all the rows to False, one by one.

And then I can change it to Disallow DBNull.

As I get more and more users this could be a very time consuming process.

So the name of the Table is classifeds_Ads and let's say the column I want to add is Bonus and it needs to be filled with False.

How do I do this?

Thank you in advance

Daniel Meis

You can create a new query and then just run this:

UPDATE classifieds_Ads Set Bonus = 0

Or an easier approach is to use the Column Properties pane to set the initial properties for the column - Allow Nulls: No, Default Value or Binding: 0

|||

Works great, Thank you.

Queueing log messages

I have a large web application with several web servers and several sql
servers. For debugging and usage tracking, the web applications write log
messages to a common log database on one of the sql servers. This traffic can
get quite extensive and, although desirable, it should not interfere with the
mainline user processing if possible.
In a previous project, I used an MSMQ to decouple the logging activity from
the mainline processing. The web applications wrote their messages to the
queue, which was a very fast operation, and the queue buffered the entry of
the log data into the database queue. In this case, we wrote a custom service
that read the queue and inserted the results into the database table.
With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
isn't a better way to do this. For example, would using Service Broker queues
be an option? Although most of the examples I've seen talk about sending
messages from one database to another, it looks like you can write C# code to
send SSB messages from an external application - ie. the web app. Then we'd
write an activation method on the queue that would simply write the log
messages into the database.
How would this work? Or is inserting someting into an SSB queue about the
same as just inserting the log record into the table itself, from a
performance point of view?
Are SSB queues implemented with MSMQ? or something else?
Or should I stick with MSMQ itself? Writing the application end (ie. the
sending end) of the queue is easy; but is there a better way to handle the
receiving end than building a whole service? For example, is there some way
to use CLR integration - or something else - to directly read from the queue
and insert rows into the log table?
All suggestions gratefully accepted
...Mike
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
I would expect that inserting directly into the log table would generally be
more efficient than a Service Broker queue or a transactional MSMQ for such
a simple task. The issue is that a synchronous physical write is required
to guarantee message delivery regardless of the technology. Service Broker,
MSMQ or a regular table insert must all wait for a write to complete before
returning to back to the client. Service Broker's sweet spot is more for
asynchronous processing and scale-out.
If you don't need guaranteed delivery, I think a non-transactional MSMQ
would probably provide the best response time. However, if your log table
is optimized for writes, I think it would be a close call between the
inserts and MSMQ. I suggest you run performance tests to determine the best
approach for your environment.

> Are SSB queues implemented with MSMQ? or something else?
Queues are schema-owned objects much like regular tables.
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike
|||Thanks to both Dan and Charles for your replies. A few followups:
The issue I'm really worried about here is the backup of inserting many
items into a potentially large table, ie. the log table. I don't want this
overhead to be in series with the "real" transactions.
1. What do you think about using a direct insert into the log table, but
using an async "fire and forget" call?
2. If I do go with the MSMQ solution, is there a better way than an external
service? Is there some way to use SQL Server's CLR to read from the queue? I
suspect this is possible, but I haven't found a good example of this
anywhere. Any suggestions?
3. Dan, you mentioned "if your log table is optimized for writes" - what did
you have in mind here?
thanks...Mike
|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.

> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
================================================== ===
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====
|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.

> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
================================================== ===
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
================================================== ====
This posting is provided "AS IS" with no warranties, and confers no rights.
================================================== ====
|||> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
I think an asynch call is a good idea. I don't know about the "forget" part
though; I think you'd want to know if it succeeded ;-)

> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
A quick Google search turned up a MSMQ example at
(http://www.codeproject.com/useritems/SqlMSMQ.asp) but I haven't looked at
it. Although it may be possible for the SQL CLR to host the app, I don't
see much value in doing so. The service method you mentioned is the most
elegant but you could easily invoke a command-line utility using a
continuously running SQL Agent job that launches at startup.

> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
Specifically, I meant a table that has only a clustered index with a
increasing key (e.g. IDENTITY column or log datetime). This will perform
very well regardless of table size. I've successfully used that approach
with tables containing billions of rows.
You might be able to get away with additional non-clustered indexes too
depending on the table size and your i/o subsystem. Once you reach a
certain threshold, consider partitioning the table by date. This will keep
the current day data working set small and mitigate the overhead of
maintaining those non-clustered indexes while improve manageability.
Without table partitioning, you still have the option of moving data via a
daily process into an appropriately indexed reporting table. This works
best when current data are infrequently accessed and most queries are done
against historical data. You can use a view (perhaps partitioned) to make
the implementation abstract.
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:48AC1D2D-73DE-4934-AE4A-57EFAB36333F@.microsoft.com...
> Thanks to both Dan and Charles for your replies. A few followups:
> The issue I'm really worried about here is the backup of inserting many
> items into a potentially large table, ie. the log table. I don't want this
> overhead to be in series with the "real" transactions.
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
> thanks...Mike
|||Thanks again, both of you.
- why not use a service? Just because it is yet another "thing" that has to
be built, tested, deployed, maintained, operated, etc. If it can all be
magically included in the database, that seems a bit easier. Of course the
service is a workable solution if the DB can't do the job.
The SQL Agent task is another good idea.
The tips on table organization are also very helpful
...Mike
|||MSMQ will give you decoupling and not much more.
Service Broker will give you integrated storage of messages and data (i.e.
you have only one product/database to backup/restore), integration with
database clustering and database mirroring. It also gives you activation of
T-SQL or CLR procedures to process the messages. It provides guaranteed EOIO
(Exactly Once In Order) semantics for your messages and reliable
communication shutdown and error (think TCP (broker) vs. UDP (msmq)),
decouples physical location from logical destination (databases containing
Service Broker queues can be moved to new hosts and continue the existing
messaging sessions)
For what you describe there are two usual patterns:
- one SQL Server all applications connect to and send a message (using T-SQL
SEND verb), which is usefull when the goal is to return quickly control to
the application and let the processing happen asynchronously. This gives
decoupling from processing, but not from availability, i.e. if the SQL
server is down the application cannot log
- Each application (Web server) has a local SQL Express instance to which it
connects and issues the SEND verb and lest the Express isntance handle the
delivery of the message to the central log. This gives decoupling both from
processing and availability. The SQL Express availability is usually same as
the Web server availability. The trouble of deploying a SQL Express instance
on each web host is about the same as deploying a msmq queue, since this is
not a full blown SQL Server instance that needs maintenance and
administration, once deployed it can pretty much go on auto-pilot mode.
Have a look at the slides at
http://blogs.msdn.com/remusrusanu/archive/2007/04/03/orlando-slides-and-code.aspx
HTH,
~ Remus
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike

Queueing log messages

I have a large web application with several web servers and several sql
servers. For debugging and usage tracking, the web applications write log
messages to a common log database on one of the sql servers. This traffic ca
n
get quite extensive and, although desirable, it should not interfere with th
e
mainline user processing if possible.
In a previous project, I used an MSMQ to decouple the logging activity from
the mainline processing. The web applications wrote their messages to the
queue, which was a very fast operation, and the queue buffered the entry of
the log data into the database queue. In this case, we wrote a custom servic
e
that read the queue and inserted the results into the database table.
With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
isn't a better way to do this. For example, would using Service Broker queue
s
be an option? Although most of the examples I've seen talk about sending
messages from one database to another, it looks like you can write C# code t
o
send SSB messages from an external application - ie. the web app. Then we'd
write an activation method on the queue that would simply write the log
messages into the database.
How would this work? Or is inserting someting into an SSB queue about the
same as just inserting the log record into the table itself, from a
performance point of view?
Are SSB queues implemented with MSMQ? or something else?
Or should I stick with MSMQ itself? Writing the application end (ie. the
sending end) of the queue is easy; but is there a better way to handle the
receiving end than building a whole service? For example, is there some way
to use CLR integration - or something else - to directly read from the queue
and insert rows into the log table?
All suggestions gratefully accepted
...Mike> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
I would expect that inserting directly into the log table would generally be
more efficient than a Service Broker queue or a transactional MSMQ for such
a simple task. The issue is that a synchronous physical write is required
to guarantee message delivery regardless of the technology. Service Broker,
MSMQ or a regular table insert must all wait for a write to complete before
returning to back to the client. Service Broker's sweet spot is more for
asynchronous processing and scale-out.
If you don't need guaranteed delivery, I think a non-transactional MSMQ
would probably provide the best response time. However, if your log table
is optimized for writes, I think it would be a close call between the
inserts and MSMQ. I suggest you run performance tests to determine the best
approach for your environment.

> Are SSB queues implemented with MSMQ? or something else?
Queues are schema-owned objects much like regular tables.
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike|||Hi Mike,
Did you encounter any issue of using MSMQ? If MSMQ worked fine, I think
that it is no need to change your current implementation. SQL Server
Service Broker also provides a queue for messages; however it is not MSMQ.
The explicit difference between Service Broker and MSMQ is that for Service
Broker you can use T-SQL to send messages while for MSMQ you need to run
MSMQ API to send messages.
For more detailed information, y ou may refer to:
Service Broker
http://msdn2.microsoft.com/en-us/library/ms166043.aspx
From your description, I saw that your MSMQ resolution seemed efficient and
worked fine now, so my recommendation here is just keeping it there until
it does not satisfy your requirements.
If you have any other questions or concerns, please feel free to let me
know. Have a good day!
Best regards,
Charles Wang
Microsoft Online Community Support
========================================
=============
Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...t/default.aspx.
========================================
==============
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
========================================
==============
This posting is provided "AS IS" with no warranties, and confers no rights.
========================================
==============|||Thanks to both Dan and Charles for your replies. A few followups:
The issue I'm really worried about here is the backup of inserting many
items into a potentially large table, ie. the log table. I don't want this
overhead to be in series with the "real" transactions.
1. What do you think about using a direct insert into the log table, but
using an async "fire and forget" call?
2. If I do go with the MSMQ solution, is there a better way than an external
service? Is there some way to use SQL Server's CLR to read from the queue? I
suspect this is possible, but I haven't found a good example of this
anywhere. Any suggestions?
3. Dan, you mentioned "if your log table is optimized for writes" - what did
you have in mind here?
thanks...Mike|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.

> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
========================================
=============
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
========================================
==============
This posting is provided "AS IS" with no warranties, and confers no rights.
========================================
==============|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.

> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
========================================
=============
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
========================================
==============
This posting is provided "AS IS" with no warranties, and confers no rights.
========================================
==============|||> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
I think an asynch call is a good idea. I don't know about the "forget" part
though; I think you'd want to know if it succeeded ;-)

> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
A quick Google search turned up a MSMQ example at
(http://www.codeproject.com/useritems/SqlMSMQ.asp) but I haven't looked at
it. Although it may be possible for the SQL CLR to host the app, I don't
see much value in doing so. The service method you mentioned is the most
elegant but you could easily invoke a command-line utility using a
continuously running SQL Agent job that launches at startup.

> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
Specifically, I meant a table that has only a clustered index with a
increasing key (e.g. IDENTITY column or log datetime). This will perform
very well regardless of table size. I've successfully used that approach
with tables containing billions of rows.
You might be able to get away with additional non-clustered indexes too
depending on the table size and your i/o subsystem. Once you reach a
certain threshold, consider partitioning the table by date. This will keep
the current day data working set small and mitigate the overhead of
maintaining those non-clustered indexes while improve manageability.
Without table partitioning, you still have the option of moving data via a
daily process into an appropriately indexed reporting table. This works
best when current data are infrequently accessed and most queries are done
against historical data. You can use a view (perhaps partitioned) to make
the implementation abstract.
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:48AC1D2D-73DE-4934-AE4A-57EFAB36333F@.microsoft.com...
> Thanks to both Dan and Charles for your replies. A few followups:
> The issue I'm really worried about here is the backup of inserting many
> items into a potentially large table, ie. the log table. I don't want this
> overhead to be in series with the "real" transactions.
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
> thanks...Mike|||Thanks again, both of you.
- why not use a service? Just because it is yet another "thing" that has to
be built, tested, deployed, maintained, operated, etc. If it can all be
magically included in the database, that seems a bit easier. Of course the
service is a workable solution if the DB can't do the job.
The SQL Agent task is another good idea.
The tips on table organization are also very helpful
...Mike|||MSMQ will give you decoupling and not much more.
Service Broker will give you integrated storage of messages and data (i.e.
you have only one product/database to backup/restore), integration with
database clustering and database mirroring. It also gives you activation of
T-SQL or CLR procedures to process the messages. It provides guaranteed EOIO
(Exactly Once In Order) semantics for your messages and reliable
communication shutdown and error (think TCP (broker) vs. UDP (msmq)),
decouples physical location from logical destination (databases containing
Service Broker queues can be moved to new hosts and continue the existing
messaging sessions)
For what you describe there are two usual patterns:
- one SQL Server all applications connect to and send a message (using T-SQL
SEND verb), which is usefull when the goal is to return quickly control to
the application and let the processing happen asynchronously. This gives
decoupling from processing, but not from availability, i.e. if the SQL
server is down the application cannot log
- Each application (Web server) has a local SQL Express instance to which it
connects and issues the SEND verb and lest the Express isntance handle the
delivery of the message to the central log. This gives decoupling both from
processing and availability. The SQL Express availability is usually same as
the Web server availability. The trouble of deploying a SQL Express instance
on each web host is about the same as deploying a msmq queue, since this is
not a full blown SQL Server instance that needs maintenance and
administration, once deployed it can pretty much go on auto-pilot mode.
Have a look at the slides at
[url]http://blogs.msdn.com/remusrusanu/archive/2007/04/03/orlando-slides-and-code.aspx[
/url]
HTH,
~ Remus
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike

Queueing log messages

I have a large web application with several web servers and several sql
servers. For debugging and usage tracking, the web applications write log
messages to a common log database on one of the sql servers. This traffic can
get quite extensive and, although desirable, it should not interfere with the
mainline user processing if possible.
In a previous project, I used an MSMQ to decouple the logging activity from
the mainline processing. The web applications wrote their messages to the
queue, which was a very fast operation, and the queue buffered the entry of
the log data into the database queue. In this case, we wrote a custom service
that read the queue and inserted the results into the database table.
With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
isn't a better way to do this. For example, would using Service Broker queues
be an option? Although most of the examples I've seen talk about sending
messages from one database to another, it looks like you can write C# code to
send SSB messages from an external application - ie. the web app. Then we'd
write an activation method on the queue that would simply write the log
messages into the database.
How would this work? Or is inserting someting into an SSB queue about the
same as just inserting the log record into the table itself, from a
performance point of view?
Are SSB queues implemented with MSMQ? or something else?
Or should I stick with MSMQ itself? Writing the application end (ie. the
sending end) of the queue is easy; but is there a better way to handle the
receiving end than building a whole service? For example, is there some way
to use CLR integration - or something else - to directly read from the queue
and insert rows into the log table?
All suggestions gratefully accepted
--
...Mike> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
I would expect that inserting directly into the log table would generally be
more efficient than a Service Broker queue or a transactional MSMQ for such
a simple task. The issue is that a synchronous physical write is required
to guarantee message delivery regardless of the technology. Service Broker,
MSMQ or a regular table insert must all wait for a write to complete before
returning to back to the client. Service Broker's sweet spot is more for
asynchronous processing and scale-out.
If you don't need guaranteed delivery, I think a non-transactional MSMQ
would probably provide the best response time. However, if your log table
is optimized for writes, I think it would be a close call between the
inserts and MSMQ. I suggest you run performance tests to determine the best
approach for your environment.
> Are SSB queues implemented with MSMQ? or something else?
Queues are schema-owned objects much like regular tables.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike|||Hi Mike,
Did you encounter any issue of using MSMQ? If MSMQ worked fine, I think
that it is no need to change your current implementation. SQL Server
Service Broker also provides a queue for messages; however it is not MSMQ.
The explicit difference between Service Broker and MSMQ is that for Service
Broker you can use T-SQL to send messages while for MSMQ you need to run
MSMQ API to send messages.
For more detailed information, y ou may refer to:
Service Broker
http://msdn2.microsoft.com/en-us/library/ms166043.aspx
From your description, I saw that your MSMQ resolution seemed efficient and
worked fine now, so my recommendation here is just keeping it there until
it does not satisfy your requirements.
If you have any other questions or concerns, please feel free to let me
know. Have a good day!
Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================Get notification to my posts through email? Please refer to:
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
If you are using Outlook Express, please make sure you clear the check box
"Tools/Options/Read: Get 300 headers at a time" to see your reply promptly.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
======================================================When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================|||Thanks to both Dan and Charles for your replies. A few followups:
The issue I'm really worried about here is the backup of inserting many
items into a potentially large table, ie. the log table. I don't want this
overhead to be in series with the "real" transactions.
1. What do you think about using a direct insert into the log table, but
using an async "fire and forget" call?
2. If I do go with the MSMQ solution, is there a better way than an external
service? Is there some way to use SQL Server's CLR to read from the queue? I
suspect this is possible, but I haven't found a good example of this
anywhere. Any suggestions?
3. Dan, you mentioned "if your log table is optimized for writes" - what did
you have in mind here?
thanks...Mike|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.
> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================|||Hi Mike,
For your questions:
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
If there is no explicit performance issues, it is no problem to use a
direct insert into a log table. Actually this is the most common way in
normal systems. For your system, I think that async "fire and forget" may
work more efficient since your system has intensive logging activities.
> 2. If I do go with the MSMQ solution, is there a better way than an
external
> service? Is there some way to use SQL Server's CLR to read from the
queue? I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
As far as I know, it is not possible to use SQL Server CLR to read from the
queue since SQL Server CLR cannot reference non-SQL Server projects. Could
you please let me know why you want to use SQL Server CLR to read from the
queue?
Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================|||> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
I think an asynch call is a good idea. I don't know about the "forget" part
though; I think you'd want to know if it succeeded ;-)
> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
A quick Google search turned up a MSMQ example at
(http://www.codeproject.com/useritems/SqlMSMQ.asp) but I haven't looked at
it. Although it may be possible for the SQL CLR to host the app, I don't
see much value in doing so. The service method you mentioned is the most
elegant but you could easily invoke a command-line utility using a
continuously running SQL Agent job that launches at startup.
> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
Specifically, I meant a table that has only a clustered index with a
increasing key (e.g. IDENTITY column or log datetime). This will perform
very well regardless of table size. I've successfully used that approach
with tables containing billions of rows.
You might be able to get away with additional non-clustered indexes too
depending on the table size and your i/o subsystem. Once you reach a
certain threshold, consider partitioning the table by date. This will keep
the current day data working set small and mitigate the overhead of
maintaining those non-clustered indexes while improve manageability.
Without table partitioning, you still have the option of moving data via a
daily process into an appropriately indexed reporting table. This works
best when current data are infrequently accessed and most queries are done
against historical data. You can use a view (perhaps partitioned) to make
the implementation abstract.
Hope this helps.
Dan Guzman
SQL Server MVP
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:48AC1D2D-73DE-4934-AE4A-57EFAB36333F@.microsoft.com...
> Thanks to both Dan and Charles for your replies. A few followups:
> The issue I'm really worried about here is the backup of inserting many
> items into a potentially large table, ie. the log table. I don't want this
> overhead to be in series with the "real" transactions.
> 1. What do you think about using a direct insert into the log table, but
> using an async "fire and forget" call?
> 2. If I do go with the MSMQ solution, is there a better way than an
> external
> service? Is there some way to use SQL Server's CLR to read from the queue?
> I
> suspect this is possible, but I haven't found a good example of this
> anywhere. Any suggestions?
> 3. Dan, you mentioned "if your log table is optimized for writes" - what
> did
> you have in mind here?
> thanks...Mike|||Thanks again, both of you.
- why not use a service? Just because it is yet another "thing" that has to
be built, tested, deployed, maintained, operated, etc. If it can all be
magically included in the database, that seems a bit easier. Of course the
service is a workable solution if the DB can't do the job.
The SQL Agent task is another good idea.
The tips on table organization are also very helpful
...Mike|||MSMQ will give you decoupling and not much more.
Service Broker will give you integrated storage of messages and data (i.e.
you have only one product/database to backup/restore), integration with
database clustering and database mirroring. It also gives you activation of
T-SQL or CLR procedures to process the messages. It provides guaranteed EOIO
(Exactly Once In Order) semantics for your messages and reliable
communication shutdown and error (think TCP (broker) vs. UDP (msmq)),
decouples physical location from logical destination (databases containing
Service Broker queues can be moved to new hosts and continue the existing
messaging sessions)
For what you describe there are two usual patterns:
- one SQL Server all applications connect to and send a message (using T-SQL
SEND verb), which is usefull when the goal is to return quickly control to
the application and let the processing happen asynchronously. This gives
decoupling from processing, but not from availability, i.e. if the SQL
server is down the application cannot log
- Each application (Web server) has a local SQL Express instance to which it
connects and issues the SEND verb and lest the Express isntance handle the
delivery of the message to the central log. This gives decoupling both from
processing and availability. The SQL Express availability is usually same as
the Web server availability. The trouble of deploying a SQL Express instance
on each web host is about the same as deploying a msmq queue, since this is
not a full blown SQL Server instance that needs maintenance and
administration, once deployed it can pretty much go on auto-pilot mode.
Have a look at the slides at
http://blogs.msdn.com/remusrusanu/archive/2007/04/03/orlando-slides-and-code.aspx
HTH,
~ Remus
"Mike Kraley" <mkraley@.community.nospam> wrote in message
news:24174429-AD79-47DF-AE08-E2C2D62ACFF6@.microsoft.com...
>I have a large web application with several web servers and several sql
> servers. For debugging and usage tracking, the web applications write log
> messages to a common log database on one of the sql servers. This traffic
> can
> get quite extensive and, although desirable, it should not interfere with
> the
> mainline user processing if possible.
> In a previous project, I used an MSMQ to decouple the logging activity
> from
> the mainline processing. The web applications wrote their messages to the
> queue, which was a very fast operation, and the queue buffered the entry
> of
> the log data into the database queue. In this case, we wrote a custom
> service
> that read the queue and inserted the results into the database table.
> With all the new fancy features of SS 2005 and .NET 2.0, I wonder if there
> isn't a better way to do this. For example, would using Service Broker
> queues
> be an option? Although most of the examples I've seen talk about sending
> messages from one database to another, it looks like you can write C# code
> to
> send SSB messages from an external application - ie. the web app. Then
> we'd
> write an activation method on the queue that would simply write the log
> messages into the database.
> How would this work? Or is inserting someting into an SSB queue about the
> same as just inserting the log record into the table itself, from a
> performance point of view?
> Are SSB queues implemented with MSMQ? or something else?
> Or should I stick with MSMQ itself? Writing the application end (ie. the
> sending end) of the queue is easy; but is there a better way to handle the
> receiving end than building a whole service? For example, is there some
> way
> to use CLR integration - or something else - to directly read from the
> queue
> and insert rows into the log table?
> All suggestions gratefully accepted
> --
> ...Mike

Monday, February 20, 2012

Queue Functionality

I am working on Queue functionality for my application. Queue is nothing but table where same record should not be processed by 2 different people/machines. To simplify

consider table

CREATE TABLE [dbo].[Table_1](

[Col1] [int] NULL,

[Enabled] [bit] NULL

) ON [PRIMARY]

I have procedure that picks up records and stores in table passed as input.

Different apps running on different machines specify their local tables

Create Procedure [dbo].[spTestQueue]

@.Tbl as varchar(100)

AS

Declare @.No varchar(10)

Select Top 1 @.No= Cast(Col1 as varchar(10)) from Table_1(nolock) Where Enabled = 0

Update Table_1 Set Enabled =1 Where Col1 = Cast(@.No as int)

EXEC ('Insert ' + @.Tbl + ' values(' + @.No + ')')

It worked fine during testing but there is nothing to prevent 2 machines to pick up same records.This is highly transactional table

How to efficiently implent locking or transcation to ensure that same record dose not get processed by 2 machines

You need to use transactions.

Code Snippet

BEGIN TRANSACTION

SELECT ... FROM Table_1 WITH (UPDLOCK) WHERE ...

UPDATE ...

COMMIT TRANSACTION

EXEC ...

I'd suggest your queue should be a separate table which should only contain unprocessed items, so deleting a row would remove it from the queue table. This would scale much better.

Code Snippet

BEGIN TRANSACTION

SELECT ... FROM Table_1 WITH (UPDLOCK) WHERE ...

DELETE FROM Table_1 WHERE ...

COMMIT TRANSACTION

INSERT INTO Store VALUES (...)

EXEC ...

You'll have to be very careful if you encounter any errors, you cannot roll the transaction back.

Hope that helps.

Jamie

|||

To ensure that when the first machine runs the procedure the second one can't read the row that is being updated by the first one, add 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE' at the beginning of the stored procedure. Also, add BEGIN TRAN and COMMIT TRAN to the beginning and end of the procedure. Here is the updates:

Code Snippet

Create Procedure [dbo].[spTestQueue]

@.Tbl as varchar(100)

AS

Begin

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

BEGIN TRANSACTION

Declare @.No varchar(10)

Select Top 1 @.No= Cast(Col1 as varchar(10)) from Table_1(nolock) Where Enabled = 0

Update Table_1 Set Enabled =1 Where Col1 = Cast(@.No as int)

EXEC ('Insert ' + @.Tbl + ' values(' + @.No + ')')

COMMIT TRANSACTION

End

I hope this answers your question.

Best regards,

Sami Samir

|||

I can think of a couple of solutions depending upon just how active this table is.

If you can afford to serialise the access to this table for this routine then you could use an application lock. This allows you to place a lock (like a critical section) over the pair of operation SELECT and UPDATE. This will prevent 1 app running the SELECT before another has run the update. As long as these run quickly then you will not get excessive contention.

You use the sp_getapplock and sp_releaseapplock procedures.

If you set a reasonable timeout on the the sp_getapplock call then this will cleanly serialise the operations.

If you cannot afford to serialise then I would suggest using a GUID to mark your record and retrieve it. You add a column of type uniqueidentifier to your table which starts off as null. And then you select it in this way. There is a sample of doing that below.

Code Snippet

DECLARE @.Tag_ID as uniqueidentifier

SET @.Tag_ID = NEWID()

UPDATE TOP (1) Table_1

SET Select_Key = @.Tag_ID

WHERE (Select_Key IS NULL)

SELECT @.No = CAST(Col1 as varchar(10))

FROM Table_1 (nolock)

WHERE (Select_Key = @.Tag_ID)

UPDATE Table_1 SET Enabled=1

WHERE (Col1 = CAST(@.No as int))

As the guid will be unique you will always get the record and noone else will get it. I have left it using enabled to mark when a record has been taken up.

|||

Went off and found my old applock code. This is a sample of how to use applocks to serialise the multiple runs. It will wait 5 seconds before failing. Any application lock (with owner of transaction) is automatically released if the transaction is committed or rolled back.


Code Snippet

-- Start a transaction and lock the App object
BEGIN TRANSACTION
-- Attempt to acquire the Table_1 queue processing Application Lock
EXEC @.lResVal = sp_getapplock
@.Resource = 'Table_1-QueueProcess',
@.LockMode = 'Exclusive',
@.LockOwner = 'Transaction',
@.LockTimeout = 5000

-- Check for failure
IF (@.lResVal >= 0)
BEGIN
-- The lock was acquired
-- Perform the acquisition of a record
SELECT TOP (1) ....
.
.
UPDATE Table_1 ....


-- Commit the transaction (will release lock) as record has been acquired
COMMIT TRANSACTION


-- Run the rest of the code
EXEC (' ....


END

ELSE
BEGIN
-- Failure to lock the sequence resource - so no action
ROLLBACK TRANSACTION
END


|||I thought about using Isolation level SERIALIZABL. But I don't think that will scale since this is highly transactional table|||

First, if you are using SQL Server 2005 then please don't spend time reinventing the wheel but instead use the Service Broker functionality that is built into the database engine. This gives a scalable queueing infrastructure among other things. See Books Online for more details.

If you are on older version of SQL Server then you can do below instead without need for doing the SELECT:


Code Snippet

DECLARE @.Col1 int;

SET ROWCOUNT 1;

UPDATE Table_1

SET @.Col1 = Col1

, Enabled = 1

WHERE Enabled = 0;

SET ROWCOUNT 0;

The UPDATE statement takes exclusive lock on the row so there will be no conflict. You can simplify it in SQL Server 2005 using the TOP clause like:

Code Snippet

UPDATE TOP(1) Table_1

SET @.Col1 = Col1

, Enabled = 1

WHERE Enabled = 0;