Showing posts with label stored. Show all posts
Showing posts with label stored. Show all posts

Friday, March 30, 2012

Populate a Table with Stored Proc.

I am looking to populate a Schedule table with information from two
other tables. I am able to populate it row by row, but I have created
tables that should provide all necessary information for me to be
able
to automatically populate a "generic" schedule for a few weeks or
more
at a time.

The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
shift_position

A DaysOff table contains:
(pk) emp_id, dayoff_1, dayoff_2 <-- the days off are entered in day
of
week (1-7) form

A CalendarDays table contains:
(pk) date, calendar_dow <-- dow contains the day of week number (as
above) for each day until 2010.

My main question is how to put all of this information together and
have SQL populate the rows with data based on days off. Any
suggestions?Nate (nate.borland@.westecnow.com) writes:

Quote:

Originally Posted by

I am looking to populate a Schedule table with information from two
other tables. I am able to populate it row by row, but I have created
tables that should provide all necessary information for me to be able
to automatically populate a "generic" schedule for a few weeks or more
at a time.
>
The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
shift_position
>
>
A DaysOff table contains:
(pk) emp_id, dayoff_1, dayoff_2 <-- the days off are entered in day
of
week (1-7) form
>
>
A CalendarDays table contains:
(pk) date, calendar_dow <-- dow contains the day of week number (as
above) for each day until 2010.
>
>
My main question is how to put all of this information together and
have SQL populate the rows with data based on days off. Any
suggestions?


Just as a reminder, in case you are getting old and don't remember
what you did yesterday, you posted this question yesterday as well,
and I replied by asking some questions, and Plamen Ratchev suggested
some queries. I suggest that you go Google news and find the old
thread and review our replies.

--
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|||Erland Sommarskog wrote:

Quote:

Originally Posted by

Nate (nate.borland@.westecnow.com) writes:

Quote:

Originally Posted by

>I am looking to populate a Schedule table with information from two
>other tables. I am able to populate it row by row, but I have created
>tables that should provide all necessary information for me to be able
>to automatically populate a "generic" schedule for a few weeks or more
>at a time.
>>
>The schedule table contains:
>(pk) schedule_id, start_datetime, end_datetime, shift_employee,
>shift_position
>>
>>
>A DaysOff table contains:
>(pk) emp_id, dayoff_1, dayoff_2 <-- the days off are entered in day
>of
>week (1-7) form
>>
>>
>A CalendarDays table contains:
>(pk) date, calendar_dow <-- dow contains the day of week number (as
>above) for each day until 2010.
>>
>>
>My main question is how to put all of this information together and
>have SQL populate the rows with data based on days off. Any
>suggestions?


>
Just as a reminder, in case you are getting old and don't remember
what you did yesterday, you posted this question yesterday as well,
and I replied by asking some questions, and Plamen Ratchev suggested
some queries. I suggest that you go Google news and find the old
thread and review our replies.
>
>


Hmm. I'm getting old and I don't do daft things like that!
Now, what was I doing before I read this?...

Wednesday, March 28, 2012

Poor Performance from Web Server

Hi,

I am having a problem with one of my stored procedures in SQL Server 2005. Basically the proc brings back a data set for the ASP.NET front end, but it is running very slowly from .NET.

I have run SQL profiler on the procedure and its taking around 20 seconds to bring back the data for the .NET, where as if I copy and paste the executed SP from profiler into the management studio and run it in a query window, it runs in around 1 second, even if I run DBCC DROPCLEANBUFFERS before I run it. More worryingly, the CPU usage is 40 times higher and the number of reads is 50% higher from .NET.

We have the .NET front end spread over 3 clustered web servers with load balancers and the SQL db is on a dedicated rig. I am having the same problem on my locally published version of the site as well, so I don't think it's an issue with the web site.

If anyone has got any ideas on this then please let me know as I am completely stuck. I should mention that the issue has only recently started occuring and it used to be fine and the rest of the site is fine...

Thanks in advance

Tom



maybe put some trace statements to echo out the time it execute a line of code. This way you can maybe find the bottleneck in your DAL. Are you using the Data Access Application Blocks. I have found those to be very valuable to manage my connections. I never see issues like you are describing anymore. Back in the day, like 4 years ago maybe when I was doing things on my own. Are you trying to fill a custom list or collection with a large set of records? I found that takes way too long and just op for datasets, readers or change my procedure to return a fixed set of rows.

But try the tracing thing to see what line(s) take the longest to execute and I think you will find your issue.

Monday, March 26, 2012

Poll - Stored Procedure vs. Command Text?

Has anyone found a clear benefit to one over the other for complex SQL
in Reporting Services reports?
I've stayed away from stored procedures so that my report development
lifecycle can remain mostly independent from my team's database
migrations, but if there is a clear benefit, I will strongly consider
it.
Thanks!
Mike"Bassist695" <Michael.EJ.Reynolds@.gmail.com> wrote in
news:1123614436.830410.247520@.g14g2000cwa.googlegroups.com:
> Has anyone found a clear benefit to one over the other for complex SQL
> in Reporting Services reports?
> I've stayed away from stored procedures so that my report development
> lifecycle can remain mostly independent from my team's database
> migrations, but if there is a clear benefit, I will strongly consider
> it.
> Thanks!
> Mike
>
I've used both. I prefer to use queries. I'll only used stored procs if 1)
I'll be reusing the same query in a number of reports, and I want to
simplify my life for maintenance, or 2) I need to do some processing on
values prior to returning data.
I query data from 3rd party software and I hate to place stored procs in
those databases. It's too easy to forget them, have have the software
company blow them away during an update.

Tuesday, March 20, 2012

Pls. help me optimize this stored procedure

Hi,

I'm kind of rusty when it comes stored procedures and I was wondering if someone could help me with the following SP. What I need to do is to query the database with a certain parameter (ex. Culture='fr-FR' and MsgKey='NoUpdateFound') and when that returns no rows I want it to default back to Culture 'en-US' with the same MsgKey='NoUpdateFound'). This way our users will always get an error message back in English if their own language is not defined. Currently I'm doing the following,

- Check row count on the parameter passed to sql and if found again do a select returning the values
- else do a select on the SQL with the default parameters.

I'm guessing there is a way to run a SQL and return it if it contains rows otherwise issue another query to return the default.

How can I improve this query. Your help is greatly appreciated. Also, if I do any assignment since 'Message' is defined as text I get the following error message

Server: Msg 279, Level 16, State 3, Line 1
The text, ntext, and image data types are invalid in this subquery or
aggregate expression.


CREATE PROCEDURE GetMessageByCulture
(
@.Culture varchar(25),
@.MsgKey varchar(50)

)
AS
SET NOCOUNT ON;

IF ( (SELECT COUNT(1) FROM ProductUpdateMsg WHERECulture=@.Culture AND MsgKey='NoUpdateFound') > 0)
SELECT Message FROM ProductUpdateMsg WHERECulture=@.Culture ANDMsgKey=@.MsgKey
ELSE
SELECT Message FROM ProductUpdateMsg WHERE Culture='en-US' ANDMsgKey=@.MsgKey
GO

Thank you
M.

Why don't you use resource files with globalization? This functionality is built in; all you have to do is edit an XML file to update the messages, and the message will always be displayed in the default language if the user's culture is not supported.

|||

IF EXISTS (SELECT * FROM ProductUpdateMsg WHERECulture=@.Culture AND MsgKey='NoUpdateFound')

SELECT Message FROM ProductUpdateMsg WHERECulture=@.Culture ANDMsgKey=@.MsgKey
ELSE
SELECT Message FROM ProductUpdateMsg WHERE Culture='en-US' ANDMsgKey=@.MsgKey

I am using * for the first statement because I don't know your columns. Ideally, you just only select one column instead of *.

Monday, March 12, 2012

Pls Help With JOIN query...

I'm trying to write a stored proc...

Basically, I have a tblItems table which contains a list of every item
available. One of the columns in this table is the brand... for test
purposes, I hardcoded the BrandID=1...

tblItems also contains a category column (int) which contains a categoryID
of 0..3...

I then have a category table which has CategoryID, Name, and DisplayOrder...

So basically what I'm trying to do is return a list of Category NAMES that
have items in them for a specifc brand... but I want to sort the returned
categories by the DisplayOrder column...

this is what I have now:

select DISTINCT tblCategories.Name, tblCategories.DisplayOrder from
tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by tblCategories.DisplayOrder

this does what I want it to do, but its returning TWO columns... Name AND
DisplayOrder... I only want to return Name, but if I take the DisplayOrder
out of the select portion, it errors out because it can't order by that...

Any ideas? Obviously I need the DISTINCT keyword so I dont get 10 copies of
the same category name.Just remove the order by ie:

select DISTINCT tblCategories.Name, tblCategories.DisplayOrder
from tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1
/* order by tblCategories.DisplayOrder */

Nobody wrote:

Quote:

Originally Posted by

I'm trying to write a stored proc...
>
Basically, I have a tblItems table which contains a list of every item
available. One of the columns in this table is the brand... for test
purposes, I hardcoded the BrandID=1...
>
tblItems also contains a category column (int) which contains a categoryID
of 0..3...
>
I then have a category table which has CategoryID, Name, and DisplayOrder...
>
So basically what I'm trying to do is return a list of Category NAMES that
have items in them for a specifc brand... but I want to sort the returned
categories by the DisplayOrder column...
>
this is what I have now:
>
>
select DISTINCT tblCategories.Name, tblCategories.DisplayOrder from
tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by tblCategories.DisplayOrder
>
this does what I want it to do, but its returning TWO columns... Name AND
DisplayOrder... I only want to return Name, but if I take the DisplayOrder
out of the select portion, it errors out because it can't order by that...
>
Any ideas? Obviously I need the DISTINCT keyword so I dont get 10 copies of
the same category name.

|||Okay my mistake. This will do the job:

select a.tblCategories.Name
from (select DISTINCT top 100 percent tblCategories.Name,
tblCategories.DisplayOrder
from tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by
tblCategories.Name,tblCategories.DisplayOrder) a

Unlike other databases, SQL Server does not allow 'order by' within
derived tables, so had to use top etc...

othellomy@.yahoo.com wrote:

Quote:

Originally Posted by

Just remove the order by ie:
>
select DISTINCT tblCategories.Name, tblCategories.DisplayOrder
from tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1
/* order by tblCategories.DisplayOrder */
>
Nobody wrote:

Quote:

Originally Posted by

I'm trying to write a stored proc...

Basically, I have a tblItems table which contains a list of every item
available. One of the columns in this table is the brand... for test
purposes, I hardcoded the BrandID=1...

tblItems also contains a category column (int) which contains a categoryID
of 0..3...

I then have a category table which has CategoryID, Name, and DisplayOrder...

So basically what I'm trying to do is return a list of Category NAMES that
have items in them for a specifc brand... but I want to sort the returned
categories by the DisplayOrder column...

this is what I have now:

select DISTINCT tblCategories.Name, tblCategories.DisplayOrder from
tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by tblCategories.DisplayOrder

this does what I want it to do, but its returning TWO columns... Name AND
DisplayOrder... I only want to return Name, but if I take the DisplayOrder
out of the select portion, it errors out because it can't order by that...

Any ideas? Obviously I need the DISTINCT keyword so I dont get 10 copies of
the same category name.

|||Nobody (nobody@.cox.net) writes:

Quote:

Originally Posted by

I'm trying to write a stored proc...
>
Basically, I have a tblItems table which contains a list of every item
available. One of the columns in this table is the brand... for test
purposes, I hardcoded the BrandID=1...
>
tblItems also contains a category column (int) which contains a categoryID
of 0..3...
>
I then have a category table which has CategoryID, Name, and
DisplayOrder...
>
So basically what I'm trying to do is return a list of Category NAMES that
have items in them for a specifc brand... but I want to sort the returned
categories by the DisplayOrder column...
>
this is what I have now:
>
>
select DISTINCT tblCategories.Name, tblCategories.DisplayOrder from
tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by tblCategories.DisplayOrder
>
this does what I want it to do, but its returning TWO columns... Name AND
DisplayOrder... I only want to return Name, but if I take the DisplayOrder
out of the select portion, it errors out because it can't order by that...
>
Any ideas? Obviously I need the DISTINCT keyword so I dont get 10 copies
of the same category name.


No, you don't need DISTINCT. You need to learn to use EXISTS:

SELECT C.Name
FROM tblCategories C
WHERE EXISTS (SELECT *
FROM tblItems I
WHERE I.CategoryID = C.CategoryID
AND I.BrandID = @.brandid)
ORDER BY C.DisplayOrder

--
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|||On Wed, 22 Nov 2006 16:59:17 -0800, Nobody wrote:

Quote:

Originally Posted by

>I'm trying to write a stored proc...
>
>Basically, I have a tblItems table which contains a list of every item
>available. One of the columns in this table is the brand... for test
>purposes, I hardcoded the BrandID=1...
>
>tblItems also contains a category column (int) which contains a categoryID
>of 0..3...
>
>I then have a category table which has CategoryID, Name, and DisplayOrder...
>
>So basically what I'm trying to do is return a list of Category NAMES that
>have items in them for a specifc brand... but I want to sort the returned
>categories by the DisplayOrder column...
>
>this is what I have now:
>
>
>select DISTINCT tblCategories.Name, tblCategories.DisplayOrder from
>tblCategories
>INNER JOIN tblItems
>on tblCategories.CategoryID = tblItems.CategoryID
>where BrandID=1 order by tblCategories.DisplayOrder
>
>this does what I want it to do, but its returning TWO columns... Name AND
>DisplayOrder... I only want to return Name, but if I take the DisplayOrder
>out of the select portion, it errors out because it can't order by that...
>
>Any ideas? Obviously I need the DISTINCT keyword so I dont get 10 copies of
>the same category name.
>


Hi Nobody,

Since you don't display any columns from tblItems, the only reason to
use it in this query is obviously to check for existance of at least one
row with BrandID equal to 1. That means that you can rewrite your query
as

SELECT c.Name --, c.DisplayOrder
FROM Categories AS c
WHERE EXISTS
(SELECT *
FROM Items AS i
WHERE i.CategoryID = c.CategoryID
AND i.BrandID = 1)
ORDER BY c.DisplayOrder;

You'll probably see a performance increase as well.

--
Hugo Kornelis, SQL Server MVP|||On 23 Nov 2006 01:48:32 -0800, othellomy@.yahoo.com wrote:

Quote:

Originally Posted by

>Okay my mistake. This will do the job:
>
>select a.tblCategories.Name
>from (select DISTINCT top 100 percent tblCategories.Name,
>tblCategories.DisplayOrder
from tblCategories
INNER JOIN tblItems
on tblCategories.CategoryID = tblItems.CategoryID
where BrandID=1 order by
>tblCategories.Name,tblCategories.DisplayOrder) a
>
>Unlike other databases, SQL Server does not allow 'order by' within
>derived tables, so had to use top etc...


Hi othellomy,

Though you can use ORDER BY in a subquery if you also use TOP, the ORDER
BY will only be used to determins which rows meat the TOP criterium;
there is no guarantee that the actual order of the query will be the
same. In fact, SQL Server 2005 will ignore both TOP 100 PERCENT and the
accomanying ORDER BY, since it is essentially a no-op to restrict the
output to 100 percent of the regular output.

If you really want to move the DISTINCT to a subquery (which in this
case is NOT needed - see my reply to Nobody), you could use

SELECT a.Name
FROM (SELECT DISTINCT c.Name, c.DisplayOrder
FROM Categories AS c
INNER JOIN Items AS i
ON i.CategoriID = c.CategoryID
WHERE i.BrandID = 1) AS a
ORDER BY a.DisplayOrder;

(untested)

--
Hugo Kornelis, SQL Server MVP

Pls Help - no clues as to what field

I am running a report that is using a stored proc that has been changed. I
made the changes that I am aware of however there seems to be a field
somewhere in an expression that causes this error ... I cannot figure out how
to locate the field!
It doesnt tell me what expression or what field. The error occurs in report
preview and the text is this:
An error occurred during local report processing.
an unexpected error occurred in the report processing.
the expression referenced a non-existing field in the fields collection.Double check your SQL. If you have something that you have wrapped with a
function (ie. LTRIM(tbl.Field), this will not have a fields name. You
would need to do: LTRIM(tbl.Field) as FieldName
Hope that helps
"MJT" <MJT@.discussions.microsoft.com> wrote in message
news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
>I am running a report that is using a stored proc that has been changed. I
> made the changes that I am aware of however there seems to be a field
> somewhere in an expression that causes this error ... I cannot figure out
> how
> to locate the field!
> It doesnt tell me what expression or what field. The error occurs in
> report
> preview and the text is this:
> An error occurred during local report processing.
> an unexpected error occurred in the report processing.
> the expression referenced a non-existing field in the fields collection.
>|||are you saying that I should check the stored procedure? I am not using sql.
I have verified that all the fields are coming back from the stored
procedure and exist in the fields collection so this is very strange.
"Chris" wrote:
> Double check your SQL. If you have something that you have wrapped with a
> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
> would need to do: LTRIM(tbl.Field) as FieldName
> Hope that helps
> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
> >I am running a report that is using a stored proc that has been changed. I
> > made the changes that I am aware of however there seems to be a field
> > somewhere in an expression that causes this error ... I cannot figure out
> > how
> > to locate the field!
> > It doesnt tell me what expression or what field. The error occurs in
> > report
> > preview and the text is this:
> >
> > An error occurred during local report processing.
> > an unexpected error occurred in the report processing.
> > the expression referenced a non-existing field in the fields collection.
> >
> >
>
>|||Yes, Check the syntax in the stored proc, if you want to post it, I'll look
at it as well.
I ran in to this same problem last week. I altered a stored proc and all of
a sudden I was getting this error, found that I had put an
ISNULL(tbl.FieldName, 0) and it came back to SQL Reports as an unknown
field. As soon as I made it ISNULL(tb.FIeldName,0) as 'CurrentValue' the
field was no longer a problem in SQL Reports.
"MJT" <MJT@.discussions.microsoft.com> wrote in message
news:7249105D-B46E-40BB-B468-DCC97C47A767@.microsoft.com...
> are you saying that I should check the stored procedure? I am not using
> sql.
> I have verified that all the fields are coming back from the stored
> procedure and exist in the fields collection so this is very strange.
> "Chris" wrote:
>> Double check your SQL. If you have something that you have wrapped with
>> a
>> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
>> would need to do: LTRIM(tbl.Field) as FieldName
>> Hope that helps
>> "MJT" <MJT@.discussions.microsoft.com> wrote in message
>> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
>> >I am running a report that is using a stored proc that has been changed.
>> >I
>> > made the changes that I am aware of however there seems to be a field
>> > somewhere in an expression that causes this error ... I cannot figure
>> > out
>> > how
>> > to locate the field!
>> > It doesnt tell me what expression or what field. The error occurs in
>> > report
>> > preview and the text is this:
>> >
>> > An error occurred during local report processing.
>> > an unexpected error occurred in the report processing.
>> > the expression referenced a non-existing field in the fields
>> > collection.
>> >
>> >
>>|||this may be a bit of an interesting dilemma. I will let you know the
outcome. I am combining 2 reports into one report. Each report seems to run
successfully one its own. When I put them together and run them ... I get
that error. The interesting part is that the 2 reports use the same stored
procedure however they return different result sets depending on the
parameter values sent. I have 2 separate datasets set up and 2 separate
tables set up to render the data so in theory this should work from what I
have read. I am wondering if I am running into an issue where it somehow
*thinks* a field is missing because it exists in one result set and not the
other? I am reworking this solution again ... step by step to see at what
point it fails. As of now I have each separate report working and I am about
to combiine them again. I will post again. Thanks for your help ... that is
still a possibility (the stored procs) and I will keep that in mind ... I
have to involve another group for that so I need to verifiy the point of
failure first.
"Chris" wrote:
> Yes, Check the syntax in the stored proc, if you want to post it, I'll look
> at it as well.
> I ran in to this same problem last week. I altered a stored proc and all of
> a sudden I was getting this error, found that I had put an
> ISNULL(tbl.FieldName, 0) and it came back to SQL Reports as an unknown
> field. As soon as I made it ISNULL(tb.FIeldName,0) as 'CurrentValue' the
> field was no longer a problem in SQL Reports.
>
>
> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> news:7249105D-B46E-40BB-B468-DCC97C47A767@.microsoft.com...
> > are you saying that I should check the stored procedure? I am not using
> > sql.
> > I have verified that all the fields are coming back from the stored
> > procedure and exist in the fields collection so this is very strange.
> >
> > "Chris" wrote:
> >
> >> Double check your SQL. If you have something that you have wrapped with
> >> a
> >> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
> >> would need to do: LTRIM(tbl.Field) as FieldName
> >>
> >> Hope that helps
> >>
> >> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> >> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
> >> >I am running a report that is using a stored proc that has been changed.
> >> >I
> >> > made the changes that I am aware of however there seems to be a field
> >> > somewhere in an expression that causes this error ... I cannot figure
> >> > out
> >> > how
> >> > to locate the field!
> >> > It doesnt tell me what expression or what field. The error occurs in
> >> > report
> >> > preview and the text is this:
> >> >
> >> > An error occurred during local report processing.
> >> > an unexpected error occurred in the report processing.
> >> > the expression referenced a non-existing field in the fields
> >> > collection.
> >> >
> >> >
> >>
> >>
> >>
>
>|||what i will do is to re create the report layout from scratch since there is
no problem in stored procedure. it may be a reason that you wrongly copied
some expression from another report and it is difficult to point to the
expression where it occurs. There is no debugger for the expression code. if
you assemblie you can debug easily to find the syntax error in the code.
~Bava
"MJT" wrote:
> this may be a bit of an interesting dilemma. I will let you know the
> outcome. I am combining 2 reports into one report. Each report seems to run
> successfully one its own. When I put them together and run them ... I get
> that error. The interesting part is that the 2 reports use the same stored
> procedure however they return different result sets depending on the
> parameter values sent. I have 2 separate datasets set up and 2 separate
> tables set up to render the data so in theory this should work from what I
> have read. I am wondering if I am running into an issue where it somehow
> *thinks* a field is missing because it exists in one result set and not the
> other? I am reworking this solution again ... step by step to see at what
> point it fails. As of now I have each separate report working and I am about
> to combiine them again. I will post again. Thanks for your help ... that is
> still a possibility (the stored procs) and I will keep that in mind ... I
> have to involve another group for that so I need to verifiy the point of
> failure first.
> "Chris" wrote:
> > Yes, Check the syntax in the stored proc, if you want to post it, I'll look
> > at it as well.
> > I ran in to this same problem last week. I altered a stored proc and all of
> > a sudden I was getting this error, found that I had put an
> > ISNULL(tbl.FieldName, 0) and it came back to SQL Reports as an unknown
> > field. As soon as I made it ISNULL(tb.FIeldName,0) as 'CurrentValue' the
> > field was no longer a problem in SQL Reports.
> >
> >
> >
> >
> > "MJT" <MJT@.discussions.microsoft.com> wrote in message
> > news:7249105D-B46E-40BB-B468-DCC97C47A767@.microsoft.com...
> > > are you saying that I should check the stored procedure? I am not using
> > > sql.
> > > I have verified that all the fields are coming back from the stored
> > > procedure and exist in the fields collection so this is very strange.
> > >
> > > "Chris" wrote:
> > >
> > >> Double check your SQL. If you have something that you have wrapped with
> > >> a
> > >> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
> > >> would need to do: LTRIM(tbl.Field) as FieldName
> > >>
> > >> Hope that helps
> > >>
> > >> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> > >> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
> > >> >I am running a report that is using a stored proc that has been changed.
> > >> >I
> > >> > made the changes that I am aware of however there seems to be a field
> > >> > somewhere in an expression that causes this error ... I cannot figure
> > >> > out
> > >> > how
> > >> > to locate the field!
> > >> > It doesnt tell me what expression or what field. The error occurs in
> > >> > report
> > >> > preview and the text is this:
> > >> >
> > >> > An error occurred during local report processing.
> > >> > an unexpected error occurred in the report processing.
> > >> > the expression referenced a non-existing field in the fields
> > >> > collection.
> > >> >
> > >> >
> > >>
> > >>
> > >>
> >
> >
> >|||thanks for your suggestion ... I would re-create from scratch but the report
is rather complicated so it would be best to try it this way first before
trying to reinvent the wheel. I still suspect it has something to do with
the fact that I am calling the same stored proc twice somehow although like I
said ... the result sets are feeding two different data regions.
"Bava Mani" wrote:
> what i will do is to re create the report layout from scratch since there is
> no problem in stored procedure. it may be a reason that you wrongly copied
> some expression from another report and it is difficult to point to the
> expression where it occurs. There is no debugger for the expression code. if
> you assemblie you can debug easily to find the syntax error in the code.
> ~Bava
> "MJT" wrote:
> > this may be a bit of an interesting dilemma. I will let you know the
> > outcome. I am combining 2 reports into one report. Each report seems to run
> > successfully one its own. When I put them together and run them ... I get
> > that error. The interesting part is that the 2 reports use the same stored
> > procedure however they return different result sets depending on the
> > parameter values sent. I have 2 separate datasets set up and 2 separate
> > tables set up to render the data so in theory this should work from what I
> > have read. I am wondering if I am running into an issue where it somehow
> > *thinks* a field is missing because it exists in one result set and not the
> > other? I am reworking this solution again ... step by step to see at what
> > point it fails. As of now I have each separate report working and I am about
> > to combiine them again. I will post again. Thanks for your help ... that is
> > still a possibility (the stored procs) and I will keep that in mind ... I
> > have to involve another group for that so I need to verifiy the point of
> > failure first.
> >
> > "Chris" wrote:
> >
> > > Yes, Check the syntax in the stored proc, if you want to post it, I'll look
> > > at it as well.
> > > I ran in to this same problem last week. I altered a stored proc and all of
> > > a sudden I was getting this error, found that I had put an
> > > ISNULL(tbl.FieldName, 0) and it came back to SQL Reports as an unknown
> > > field. As soon as I made it ISNULL(tb.FIeldName,0) as 'CurrentValue' the
> > > field was no longer a problem in SQL Reports.
> > >
> > >
> > >
> > >
> > > "MJT" <MJT@.discussions.microsoft.com> wrote in message
> > > news:7249105D-B46E-40BB-B468-DCC97C47A767@.microsoft.com...
> > > > are you saying that I should check the stored procedure? I am not using
> > > > sql.
> > > > I have verified that all the fields are coming back from the stored
> > > > procedure and exist in the fields collection so this is very strange.
> > > >
> > > > "Chris" wrote:
> > > >
> > > >> Double check your SQL. If you have something that you have wrapped with
> > > >> a
> > > >> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
> > > >> would need to do: LTRIM(tbl.Field) as FieldName
> > > >>
> > > >> Hope that helps
> > > >>
> > > >> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> > > >> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
> > > >> >I am running a report that is using a stored proc that has been changed.
> > > >> >I
> > > >> > made the changes that I am aware of however there seems to be a field
> > > >> > somewhere in an expression that causes this error ... I cannot figure
> > > >> > out
> > > >> > how
> > > >> > to locate the field!
> > > >> > It doesnt tell me what expression or what field. The error occurs in
> > > >> > report
> > > >> > preview and the text is this:
> > > >> >
> > > >> > An error occurred during local report processing.
> > > >> > an unexpected error occurred in the report processing.
> > > >> > the expression referenced a non-existing field in the fields
> > > >> > collection.
> > > >> >
> > > >> >
> > > >>
> > > >>
> > > >>
> > >
> > >
> > >|||I have tried running the parts of the report separately and they work ... I
put them together and they dont ... I get the "expression referenced a
non-existing field in the fields collection" error message and report wont
run. Since both run separately I am not inclined to think it is stored proc.
Any more ideas?
"Chris" wrote:
> Yes, Check the syntax in the stored proc, if you want to post it, I'll look
> at it as well.
> I ran in to this same problem last week. I altered a stored proc and all of
> a sudden I was getting this error, found that I had put an
> ISNULL(tbl.FieldName, 0) and it came back to SQL Reports as an unknown
> field. As soon as I made it ISNULL(tb.FIeldName,0) as 'CurrentValue' the
> field was no longer a problem in SQL Reports.
>
>
> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> news:7249105D-B46E-40BB-B468-DCC97C47A767@.microsoft.com...
> > are you saying that I should check the stored procedure? I am not using
> > sql.
> > I have verified that all the fields are coming back from the stored
> > procedure and exist in the fields collection so this is very strange.
> >
> > "Chris" wrote:
> >
> >> Double check your SQL. If you have something that you have wrapped with
> >> a
> >> function (ie. LTRIM(tbl.Field), this will not have a fields name. You
> >> would need to do: LTRIM(tbl.Field) as FieldName
> >>
> >> Hope that helps
> >>
> >> "MJT" <MJT@.discussions.microsoft.com> wrote in message
> >> news:E98971F5-7FB1-47E9-BDCA-8EE206328488@.microsoft.com...
> >> >I am running a report that is using a stored proc that has been changed.
> >> >I
> >> > made the changes that I am aware of however there seems to be a field
> >> > somewhere in an expression that causes this error ... I cannot figure
> >> > out
> >> > how
> >> > to locate the field!
> >> > It doesnt tell me what expression or what field. The error occurs in
> >> > report
> >> > preview and the text is this:
> >> >
> >> > An error occurred during local report processing.
> >> > an unexpected error occurred in the report processing.
> >> > the expression referenced a non-existing field in the fields
> >> > collection.
> >> >
> >> >
> >>
> >>
> >>
>
>

Wednesday, March 7, 2012

please solve this deadlock problem

i have 2 stored procedures
1st sp
CREATE procedure dbo.sfd_sp_Assign_PR
@.VOICE_FILE_LOCATION VARCHAR(255),
@.VOICE_FILE_NAME VARCHAR(255),
@.PR VARCHAR(10),
@.TYPE CHAR(1),
@.ACCOUNTID varchar(10) OUTPUT,
@.DICTATOR VARCHAR(50) OUTPUT
AS
DECLARE @.Count as int
DECLARE @.LAST_PR as VARCHAR(10)
DECLARE @.TR as VARCHAR(10)
DECLARE @.PROVIDER_ID as smallint
DECLARE @.DURATION as int
DECLARE @.SHIFT_ID as varchar(10)
DECLARE @.TR_SHIFT_ID as varchar(10)
DECLARE @.FILE_ASSIGNED_DATE as datetime
DECLARE @.TR_ASSIGNED_TIME as datetime
DECLARE @.undertranscription as int
DECLARE @.readyforproofreading as int
DECLARE @.MaxSequence as int
DECLARE @.WEIGHTED_PR_DURATION as int
DECLARE @.PR_VOICE_WEIGHT as int
DECLARE @.TR_STATUS AS CHAR(1)
DECLARE @.PR_DOC_FILE_NAME AS VARCHAR(255)
DECLARE @.PR_DOC_FILE_LOCATION AS VARCHAR(255)
DECLARE @.PATIENT_NAME AS VARCHAR(50)
DECLARE @.MRN AS VARCHAR(20)
DECLARE @.VISIT_DATE AS SMALLDATETIME
DECLARE @.TR_CHAR_COUNT AS INT
DECLARE @.VF_START_TIME AS SMALLINT
DECLARE @.VF_END_TIME AS SMALLINT
DECLARE @.TR_NOTES AS VARCHAR(1000)
DECLARE @.TR_CHECKIN_DATETIME AS DATETIME
DECLARE @.REPORT_TYPE AS VARCHAR(50)
DECLARE @.TR_PL_STATUS AS SMALLINT
DECLARE @.LOCATIONID AS SMALLINT
DECLARE @.TR_TRANSCRIPTS CURSOR
SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
@.WEIGHTED_PR_DURATION=WEIGHTED_PR_DURATI
ON FROM VOICE_FILES with (nolock)
WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
IF @.ACCOUNTID is NULL
return -1 --No record in voice files
SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS with (nolock) WHERE
ACCOUNTID=@.ACCOUNTID AND PROVIDER_ID=@.PROVIDER_ID
If @.Count = 0
return -2 --No record in account weights
SELECT @.Count=count(*) from DISTRIBUTION with (nolock) where
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.Count = 0
return -3 --File not assigned to TR for transcription
SELECT @.Count=count(*) FROM VOICE_FILE_PRIORITY with (nolock) WHERE
VOICE_FILE_NAME=@.VOICE_FILE_NAME AND
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
If @.Count = 0
return -4 --No record in voice file priority
SELECT assigned_to_pr FROM DISTRIBUTION with (nolock) WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and ASSIGNED_TO_PR=@.PR
if @.@.rowcount=1
return -5 --file already assigned to selected PR
SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.PR AND
getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = DATEPART(dw,getdate())
If @.SHIFT_ID IS NULL
return -6 --Shift ID not found
SET @.undertranscription = 0
SET @.readyforproofreading = 0
SELECT @.MaxSequence=max(sequence) from distribution with (nolock) WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
SELECT @.LAST_PR=ASSIGNED_TO_PR FROM DISTRIBUTION with (nolock) WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and sequence=@.MaxSequence
IF @.MaxSequence=1 AND @.LAST_PR IS NULL
BEGIN
SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS in
('A','O') AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
IF @.Count > 0
SET @.undertranscription = @.Duration
SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS='C' AND
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
IF @.Count > 0
SET @.readyforproofreading = @.Duration
END
SELECT @.PR_VOICE_WEIGHT=PR_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP with
(nolock) WHERE ACCOUNTID=@.ACCOUNTID
AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.PR
if @.PR_VOICE_WEIGHT IS NOT NULL
SET @.WEIGHTED_PR_DURATION = @.PR_VOICE_WEIGHT * @.DURATION
--SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
PROVIDER_ID=@.PROVIDER_ID
SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER WHERE PROVIDER_ID=@.PROVIDER_ID
SELECT @.FILE_ASSIGNED_DATE=dbo. sfd_fn_File_Assigned_Date(@.PR,getdate())
SET @.FILE_ASSIGNED_DATE=CONVERT(VARCHAR(12),
@.FILE_ASSIGNED_DATE,101)
BEGIN TRANSACTION
IF @.LAST_PR IS NULL AND @.MaxSequence=1
BEGIN
UPDATE TR_TRANSCRIPTS with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.@.ERROR !=0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
UPDATE DISTRIBUTION with (updlock) SET
ASSIGNED_TO_PR=@.PR,PR_SHIFT_ID=@.SHIFT_ID
,
PR_ASSIGNED_TIME=GETDATE(),DISTRIBUTION_
TYPE=@.TYPE WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND SEQUENCE=1
If @.@.ERROR !=0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.@.ERROR != 0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
END
ELSE
BEGIN
SELECT @.TR_STATUS=TR_STATUS, @.TR=ASSIGNED_TO_TR, @.TR_SHIFT_ID=TR_SHIFT_ID,
@.TR_ASSIGNED_TIME=TR_ASSIGNED_TIME
FROM DISTRIBUTION with (nolock) WHERE sequence=1 AND
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
INSERT INTO DISTRIBUTION (VOICE_FILE_LOCATION, VOICE_FILE_NAME,
TR_STATUS,SEQUENCE,NOTES,PR_STATUS,ASSIG
NED_TO_TR,ASSIGNED_TO_PR,TR_SHIFT_ID
,PR_SHIFT_ID,TR_ASSIGNED_TIME,PR_ASSIGNE
D_TIME,DISTRIBUTION_TYPE) values
(@.VOICE_FILE_LOCATION, @.VOICE_FILE_NAME, @.TR_STATUS, @.MaxSequence+1,
'Distributed to Tr', 'N', @.TR, @.PR, @.TR_SHIFT_ID, @.SHIFT_ID,
@.TR_ASSIGNED_TIME, GETDATE(),@.TYPE)
-- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE PR_STATUS IN ('N','P')
AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
-- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
-- IF @.COUNT = 0
-- BEGIN
-- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
-- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
-- IF @.COUNT > 0
-- BEGIN
UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND ASSIGNED_TO_PR IS NULL
If @.@.ERROR != 0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
-- END
-- END
SET @.TR_TRANSCRIPTS = CURSOR FOR
SELECT
PATIENT_NAME,MRN,VISIT_DATE,TR_CHAR_COUN
T,PR_DOC_FILE_NAME,PR_DOC_FILE_LOCAT
ION,VF_START_TIME,VF_END_TIME,TR_NOTES,R
EPORT_TYPE,TR_CHECKIN_DATETIME,TR_PL
_STATUS,ASSIGNED_TO_TR,LOCATIONID
FROM TR_TRANSCRIPTS with (nolock) where ASSIGNED_TO_PR=@.LAST_PR AND
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND PR_DOC_FILE_NAME IS NOT NULL
OPEN @.TR_TRANSCRIPTS
FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
@.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FILE
_LOCATION,@.VF_START_TIME,@.VF_END_TIME,@.T
R_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_DAT
ETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
WHILE (@.@.FETCH_STATUS = 0)
BEGIN
INSERT INTO
TR_TRANSCRIPTS(DOC_FILE_NAME,DOC_FILE_LO
CATION,PATIENT_NAME,MRN,VISIT_DATE,T
RANSCRIPTION_STATUS,TR_CHAR_COUNT,VOICE_
FILE_NAME,VOICE_FILE_LOCATION,ASSIGN
ED_TO_TR,TR_CHECKIN_DATETIME,PR_STATUS,V
F_START_TIME,VF_END_TIME,TR_NOTES,RE
PORT_TYPE,TR_PL_STATUS,ASSI
GNED_TO_PR,LOCATIONID) VALUES
(@.PR_DOC_FILE_NAME,@.PR_DOC_FILE_LOCATION
,@.PATIENT_NAME,@.MRN,@.VISIT_DATE,'A',
@.TR_CHAR_COUNT,@.VOICE_FILE_NAME,@.VOICE_F
ILE_LOCATION,@.TR,@.TR_CHECKIN_DATETIM
E,'N',@.VF_START_TIME,@.VF_END_TIME,@.TR_NO
TES,@.REPORT_TYPE,@.TR_PL_STATUS,@.PR,@.
LOCATIONID)
SET @.undertranscription = 0
SET @.readyforproofreading = @.Duration
FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
@.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FILE
_LOCATION,@.VF_START_TIME,@.VF_END_TIME,@.T
R_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_DAT
ETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
END
CLOSE @.TR_TRANSCRIPTS
DEALLOCATE @.TR_TRANSCRIPTS
--UPDATE EMP_ASSIGNED_WORKLOAD
END
--common
UPDATE VOICE_FILES with (updlock) SET STATUS='A',TYPE= CASE WHEN TYPE='B'
OR TYPE='R' THEN 'N' ELSE TYPE END WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.@.ERROR !=0
BEGIN
ROLLBACK TRANSACTION
RETURN 0
END
IF EXISTS(SELECT empid from emp_assigned_workload with (nolock) WHERE
EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
AND SHIFT_ID=@.SHIFT_ID)
UPDATE EMP_ASSIGNED_WORKLOAD with (updlock) SET
UNDER_TRANSCRIPTION=UNDER_TRANSCRIPTION+
@.undertranscription,READY_FOR_PROOFR
EADING=READY_FOR_PROOFREADING+@.readyforp
roofreading,
WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_PR
_DURATION,
PR_WORK_ASSIGNED=PR_WORK_ASSIGNED+@.DURAT
ION
WHERE EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE AND
SHIFT_ID=@.SHIFT_ID
ELSE
INSERT INTO
EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_COM
PLETED,
UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGNED
,SHIFT_ID,
WORK_ASSIGNED) VALUES (@.FILE_ASSIGNED_DATE,@.PR,0,0,@.undertrans
cription,
@.readyforproofreading,@.DURATION,0,@.SHIFT
_ID,@.WEIGHTED_PR_DURATION)
If @.@.ERROR !=0 OR @.@.ROWCOUNT=0
BEGIN
ROLLBACK TRANSACTION
RETURN -10
END
COMMIT TRANSACTION
RETURN 1
GO
--end 1st sp
2nd sp
CREATE procedure dbo.sfd_sp_Assign_TR
@.VOICE_FILE_LOCATION VARCHAR(255),
@.VOICE_FILE_NAME VARCHAR(255),
@.TR VARCHAR(10),
@.ACCOUNTID varchar(10) output,
@.DICTATOR VARCHAR(50) output
AS
DECLARE @.Count as int
DECLARE @.PROVIDER_ID as smallint
DECLARE @.WEIGHTED_TR_DURATION as int
DECLARE @.DURATION as int
DECLARE @.SHIFT_ID as varchar(10)
DECLARE @.STAT_FLAG as char(1)
DECLARE @.MAX_Priority as int
DECLARE @.PRIORITY as bit
DECLARE @.FILE_ASSIGNED_DATE as datetime
DECLARE @.TRANS_VOICE_WEIGHT AS int
DECLARE @.MASTER_BLOCKED AS int
SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
@.WEIGHTED_TR_DURATION=WEIGHTED_TR_DURATI
ON,@.STAT_FLAG=STAT_FLAG FROM
VOICE_FILES with (nolock) WHERE
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
IF @.ACCOUNTID is NULL
return -1 --No record in voice files
SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS WHERE ACCOUNTID=@.ACCOUNTID AND
PROVIDER_ID=@.PROVIDER_ID
If @.Count = 0
return -2 --No record in account weights
SELECT @.Count=count(*) from vf_tr_assign where
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.Count > 0
return -3 --TR has already started working on the file
SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.TR AND
getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = datepart(dw,getdate())
If @.SHIFT_ID is null
return -4 --Shift ID not found
SELECT @.Count=count(*) from DISTRIBUTION where
VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.Count > 0
return -5 --File already assigned for transcription
if @.STAT_FLAG='S' or @.STAT_FLAG='P'
SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
WHERE IMPORTANCE=@.STAT_FLAG
ELSE
BEGIN
SELECT @.PRIORITY=PRIORITY FROM ACCOUNT with (nolock) WHERE
ACCOUNTID=@.ACCOUNTID
IF @.PRIORITY is null
set @.PRIORITY=0
IF @.PRIORITY = 1
BEGIN
SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
WHERE IMPORTANCE='N'
set @.STAT_FLAG='N'
END
ELSE
BEGIN
SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
WHERE IMPORTANCE='L'
set @.STAT_FLAG='L'
END
END
if @.MAX_Priority is null
set @.MAX_Priority = 0
set @.MAX_Priority = @.MAX_Priority + 1
SELECT @.TRANS_VOICE_WEIGHT=TRANS_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP with
(nolock) WHERE ACCOUNTID=@.ACCOUNTID
AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.TR
if @.TRANS_VOICE_WEIGHT is not null
SET @.WEIGHTED_TR_DURATION = @.TRANS_VOICE_WEIGHT * @.DURATION
--SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
PROVIDER_ID=@.PROVIDER_ID
SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER with (nolock) WHERE
PROVIDER_ID=@.PROVIDER_ID
SELECT @.FILE_ASSIGNED_DATE = dbo. sfd_fn_File_Assigned_Date(@.TR,getdate())
SET @.FILE_ASSIGNED_DATE = CONVERT(VARCHAR(12),@.FILE_ASSIGNED_DATE,
101)
BEGIN transaction
Insert into
dbo. Distribution(VOICE_FILE_LOCATION,VOICE_F
ILE_NAME,ASSIGNED_TO_TR,TR_SHIFT
_ID,NOTES,
TR_ASSIGNED_TIME) values (@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
TR,@.SHIFT_I
D
,'Distributed to Tr',GETDATE())
If @.@.error != 0
BEGIN
rollback transaction
RETURN 0
END
IF EXISTS(SELECT EMPID from emp_assigned_workload WHERE EMPID=@.TR AND
FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
AND SHIFT_ID=@.SHIFT_ID)
Update EMP_ASSIGNED_WORKLOAD with (updlock) SET
WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_TR
_DURATION,
TR_WORK_ASSIGNED=TR_WORK_ASSIGNED+@.DURAT
ION WHERE EMPID=@.TR AND
FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
AND SHIFT_ID=@.SHIFT_ID
ELSE
INSERT INTO
EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_COM
PLETED,
UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGNED
,SHIFT_ID,
WORK_ASSIGNED) VALUES (@.FILE_ASSIGNED_DATE,@.TR,0,0,0,0,0,@.DURA
TION,@.SHIFT_ID
,
@.WEIGHTED_TR_DURATION)
If @.@.error != 0 OR @.@.ROWCOUNT=0
BEGIN
rollback transaction
RETURN -10
END
INSERT INTO
VOICE_FILE_PRIORITY(VOICE_FILE_LOCATION,
VOICE_FILE_NAME,ACCOUNT_ID,PROVIDER_
ID,
PRIORITY,ASSIGNED_TO_TR,ASSIGNED_TO_PR,I
MPORTANCE,STATUS,PENDINGSTATUS)
VALUES
(@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
ACCOUNTID,@.PROVIDER_ID,
@.MAX_Priority,@.TR ,NULL,@.STAT_FLAG,'N',NULL)
If @.@.error != 0
BEGIN
rollback transaction
RETURN 0
END
-- written by raghu for deadlock
select @.MASTER_BLOCKED = blocked from dbo.master.sysprocesses with (nolock)
where blocked<>0
if @.MASTER_BLOCKED=0
begin
UPDATE VOICE_FILES with (updlock) SET STATUS='T',TYPE= CASE WHEN TYPE='B'
OR TYPE='R' THEN 'N' ELSE TYPE END,UPDATE_TIME=getdate()
WHERE VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
VOICE_FILE_NAME=@.VOICE_FILE_NAME
If @.@.error <> 0 or @.@.rowcount=0
BEGIN
rollback transaction
RETURN 0
END
end
else
begin
SET LOCK_TIMEOUT 30
if @.@.LOCK_TIMEOUT= 30
rollback transaction
RETURN 0
end
-- end by raghu
COMMIT TRANSACTION
RETURN 1
GO
--end spHi
Please read this article
http://www.sql-server-performance.com/deadlocks.asp
"raghu veer" <raghu veer@.discussions.microsoft.com> wrote in message
news:E05516F6-C1D4-418F-9111-C7EFD74B5BF3@.microsoft.com...
>i have 2 stored procedures
> 1st sp
> CREATE procedure dbo.sfd_sp_Assign_PR
> @.VOICE_FILE_LOCATION VARCHAR(255),
> @.VOICE_FILE_NAME VARCHAR(255),
> @.PR VARCHAR(10),
> @.TYPE CHAR(1),
> @.ACCOUNTID varchar(10) OUTPUT,
> @.DICTATOR VARCHAR(50) OUTPUT
> AS
> DECLARE @.Count as int
> DECLARE @.LAST_PR as VARCHAR(10)
> DECLARE @.TR as VARCHAR(10)
> DECLARE @.PROVIDER_ID as smallint
> DECLARE @.DURATION as int
> DECLARE @.SHIFT_ID as varchar(10)
> DECLARE @.TR_SHIFT_ID as varchar(10)
> DECLARE @.FILE_ASSIGNED_DATE as datetime
> DECLARE @.TR_ASSIGNED_TIME as datetime
> DECLARE @.undertranscription as int
> DECLARE @.readyforproofreading as int
> DECLARE @.MaxSequence as int
> DECLARE @.WEIGHTED_PR_DURATION as int
> DECLARE @.PR_VOICE_WEIGHT as int
> DECLARE @.TR_STATUS AS CHAR(1)
> DECLARE @.PR_DOC_FILE_NAME AS VARCHAR(255)
> DECLARE @.PR_DOC_FILE_LOCATION AS VARCHAR(255)
> DECLARE @.PATIENT_NAME AS VARCHAR(50)
> DECLARE @.MRN AS VARCHAR(20)
> DECLARE @.VISIT_DATE AS SMALLDATETIME
> DECLARE @.TR_CHAR_COUNT AS INT
> DECLARE @.VF_START_TIME AS SMALLINT
> DECLARE @.VF_END_TIME AS SMALLINT
> DECLARE @.TR_NOTES AS VARCHAR(1000)
> DECLARE @.TR_CHECKIN_DATETIME AS DATETIME
> DECLARE @.REPORT_TYPE AS VARCHAR(50)
> DECLARE @.TR_PL_STATUS AS SMALLINT
> DECLARE @.LOCATIONID AS SMALLINT
> DECLARE @.TR_TRANSCRIPTS CURSOR
> SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
> @.WEIGHTED_PR_DURATION=WEIGHTED_PR_DURATI
ON FROM VOICE_FILES with (nolock)
> WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.ACCOUNTID is NULL
> return -1 --No record in voice files
> SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS with (nolock) WHERE
> ACCOUNTID=@.ACCOUNTID AND PROVIDER_ID=@.PROVIDER_ID
> If @.Count = 0
> return -2 --No record in account weights
> SELECT @.Count=count(*) from DISTRIBUTION with (nolock) where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count = 0
> return -3 --File not assigned to TR for transcription
> SELECT @.Count=count(*) FROM VOICE_FILE_PRIORITY with (nolock) WHERE
> VOICE_FILE_NAME=@.VOICE_FILE_NAME AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> If @.Count = 0
> return -4 --No record in voice file priority
> SELECT assigned_to_pr FROM DISTRIBUTION with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and ASSIGNED_TO_PR=@.PR
> if @.@.rowcount=1
> return -5 --file already assigned to selected PR
> SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.PR AND
> getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = DATEPART(dw,getdate())
> If @.SHIFT_ID IS NULL
> return -6 --Shift ID not found
> SET @.undertranscription = 0
> SET @.readyforproofreading = 0
> SELECT @.MaxSequence=max(sequence) from distribution with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> SELECT @.LAST_PR=ASSIGNED_TO_PR FROM DISTRIBUTION with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and sequence=@.MaxSequence
> IF @.MaxSequence=1 AND @.LAST_PR IS NULL
> BEGIN
> SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS in
> ('A','O') AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.Count > 0
> SET @.undertranscription = @.Duration
> SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS='C'
> AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.Count > 0
> SET @.readyforproofreading = @.Duration
> END
> SELECT @.PR_VOICE_WEIGHT=PR_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP with
> (nolock) WHERE ACCOUNTID=@.ACCOUNTID
> AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.PR
> if @.PR_VOICE_WEIGHT IS NOT NULL
> SET @.WEIGHTED_PR_DURATION = @.PR_VOICE_WEIGHT * @.DURATION
> --SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER WHERE PROVIDER_ID=@.PROVIDER_ID
> SELECT @.FILE_ASSIGNED_DATE=dbo. sfd_fn_File_Assigned_Date(@.PR,getdate())
> SET @.FILE_ASSIGNED_DATE=CONVERT(VARCHAR(12),
@.FILE_ASSIGNED_DATE,101)
>
> BEGIN TRANSACTION
> IF @.LAST_PR IS NULL AND @.MaxSequence=1
> BEGIN
> UPDATE TR_TRANSCRIPTS with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> UPDATE DISTRIBUTION with (updlock) SET
> ASSIGNED_TO_PR=@.PR,PR_SHIFT_ID=@.SHIFT_ID
,
> PR_ASSIGNED_TIME=GETDATE(),DISTRIBUTION_
TYPE=@.TYPE WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND SEQUENCE=1
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR != 0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> END
> ELSE
> BEGIN
> SELECT @.TR_STATUS=TR_STATUS, @.TR=ASSIGNED_TO_TR, @.TR_SHIFT_ID=TR_SHIFT_ID,
> @.TR_ASSIGNED_TIME=TR_ASSIGNED_TIME
> FROM DISTRIBUTION with (nolock) WHERE sequence=1 AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> INSERT INTO DISTRIBUTION (VOICE_FILE_LOCATION, VOICE_FILE_NAME,
> TR_STATUS,SEQUENCE,NOTES,PR_STATUS,ASSIG
NED_TO_TR,ASSIGNED_TO_PR,TR_SHIFT_
ID,PR_SHIFT_ID,TR_ASSIGNED_TIME,PR_ASSIG
NED_TIME,DISTRIBUTION_TYPE)
> values
> (@.VOICE_FILE_LOCATION, @.VOICE_FILE_NAME, @.TR_STATUS, @.MaxSequence+1,
> 'Distributed to Tr', 'N', @.TR, @.PR, @.TR_SHIFT_ID, @.SHIFT_ID,
> @.TR_ASSIGNED_TIME, GETDATE(),@.TYPE)
> -- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE PR_STATUS IN ('N','P')
> AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> -- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> -- IF @.COUNT = 0
> -- BEGIN
> -- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> -- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> -- IF @.COUNT > 0
> -- BEGIN
> UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND ASSIGNED_TO_PR IS NULL
> If @.@.ERROR != 0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> -- END
> -- END
> SET @.TR_TRANSCRIPTS = CURSOR FOR
> SELECT
> PATIENT_NAME,MRN,VISIT_DATE,TR_CHAR_COUN
T,PR_DOC_FILE_NAME,PR_DOC_FILE_LOC
ATION,VF_START_TIME,VF_END_TIME,TR_NOTES
,REPORT_TYPE,TR_CHECKIN_DATETIME,TR_
PL_STATUS,ASSIGNED_TO_TR,LOCATIONID
> FROM TR_TRANSCRIPTS with (nolock) where ASSIGNED_TO_PR=@.LAST_PR AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND PR_DOC_FILE_NAME IS NOT NULL
> OPEN @.TR_TRANSCRIPTS
> FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
> @.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FI
LE_LOCATION,@.VF_START_TIME,@.VF_END_TIME,
@.TR_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_D
ATETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
> WHILE (@.@.FETCH_STATUS = 0)
> BEGIN
> INSERT INTO
> TR_TRANSCRIPTS(DOC_FILE_NAME,DOC_FILE_LO
CATION,PATIENT_NAME,MRN,VISIT_DATE,TRANS
CR
IPTION_STATUS,TR_CHAR_COUNT,VOICE_FILE_N
AME,VOICE_FILE_LOCATION,ASSIGNED_TO_TR,T
R_CH
ECKIN_DATETIME,PR_STATUS,VF_START_TIME,V
F_END_TIME,TR_NOTES,REPORT_TYPE,TR_PL_ST
ATUS
,AS
SIGNED_TO_PR,LOCATIONID)
> VALUES
> (@.PR_DOC_FILE_NAME,@.PR_DOC_FILE_LOCATION
,@.PATIENT_NAME,@.MRN,@.VISIT_DATE,'A
',@.TR_CHAR_COUNT,@.VOICE_FILE_NAME,@.VOICE
_FILE_LOCATION,@.TR,@.TR_CHECKIN_DATET
IME,'N',@.VF_START_TIME,@.VF_END_TIME,@.TR_
NOTES,@.REPORT_TYPE,@.TR_PL_STATUS,@.PR
,@.LOCATIONID)
> SET @.undertranscription = 0
> SET @.readyforproofreading = @.Duration
> FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
> @.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FI
LE_LOCATION,@.VF_START_TIME,@.VF_END_TIME,
@.TR_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_D
ATETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
> END
> CLOSE @.TR_TRANSCRIPTS
> DEALLOCATE @.TR_TRANSCRIPTS
>
> --UPDATE EMP_ASSIGNED_WORKLOAD
> END
> --common
> UPDATE VOICE_FILES with (updlock) SET STATUS='A',TYPE= CASE WHEN TYPE='B'
> OR TYPE='R' THEN 'N' ELSE TYPE END WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
> VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> IF EXISTS(SELECT empid from emp_assigned_workload with (nolock) WHERE
> EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID)
> UPDATE EMP_ASSIGNED_WORKLOAD with (updlock) SET
> UNDER_TRANSCRIPTION=UNDER_TRANSCRIPTION+
@.undertranscription,READY_FOR_PROO
FREADING=READY_FOR_PROOFREADING+@.readyfo
rproofreading,
> WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_PR
_DURATION,
> PR_WORK_ASSIGNED=PR_WORK_ASSIGNED+@.DURAT
ION
> WHERE EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE AND
> SHIFT_ID=@.SHIFT_ID
> ELSE
> INSERT INTO
> EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_C
OMPLETED,
> UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGN
ED,SHIFT_ID,
> WORK_ASSIGNED) VALUES (@.FILE_ASSIGNED_DATE,@.PR,0,0,@.undertrans
cription,
> @.readyforproofreading,@.DURATION,0,@.SHIFT
_ID,@.WEIGHTED_PR_DURATION)
> If @.@.ERROR !=0 OR @.@.ROWCOUNT=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN -10
> END
> COMMIT TRANSACTION
> RETURN 1
> GO
> --end 1st sp
> 2nd sp
> CREATE procedure dbo.sfd_sp_Assign_TR
> @.VOICE_FILE_LOCATION VARCHAR(255),
> @.VOICE_FILE_NAME VARCHAR(255),
> @.TR VARCHAR(10),
> @.ACCOUNTID varchar(10) output,
> @.DICTATOR VARCHAR(50) output
> AS
> DECLARE @.Count as int
> DECLARE @.PROVIDER_ID as smallint
> DECLARE @.WEIGHTED_TR_DURATION as int
> DECLARE @.DURATION as int
> DECLARE @.SHIFT_ID as varchar(10)
> DECLARE @.STAT_FLAG as char(1)
> DECLARE @.MAX_Priority as int
> DECLARE @.PRIORITY as bit
> DECLARE @.FILE_ASSIGNED_DATE as datetime
> DECLARE @.TRANS_VOICE_WEIGHT AS int
> DECLARE @.MASTER_BLOCKED AS int
> SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
> @.WEIGHTED_TR_DURATION=WEIGHTED_TR_DURATI
ON,@.STAT_FLAG=STAT_FLAG FROM
> VOICE_FILES with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.ACCOUNTID is NULL
> return -1 --No record in voice files
> SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS WHERE ACCOUNTID=@.ACCOUNTID AND
> PROVIDER_ID=@.PROVIDER_ID
> If @.Count = 0
> return -2 --No record in account weights
> SELECT @.Count=count(*) from vf_tr_assign where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count > 0
> return -3 --TR has already started working on the file
> SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.TR AND
> getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = datepart(dw,getdate())
> If @.SHIFT_ID is null
> return -4 --Shift ID not found
> SELECT @.Count=count(*) from DISTRIBUTION where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count > 0
> return -5 --File already assigned for transcription
> if @.STAT_FLAG='S' or @.STAT_FLAG='P'
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE=@.STAT_FLAG
> ELSE
> BEGIN
> SELECT @.PRIORITY=PRIORITY FROM ACCOUNT with (nolock) WHERE
> ACCOUNTID=@.ACCOUNTID
> IF @.PRIORITY is null
> set @.PRIORITY=0
> IF @.PRIORITY = 1
> BEGIN
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE='N'
> set @.STAT_FLAG='N'
> END
> ELSE
> BEGIN
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE='L'
> set @.STAT_FLAG='L'
> END
> END
> if @.MAX_Priority is null
> set @.MAX_Priority = 0
> set @.MAX_Priority = @.MAX_Priority + 1
> SELECT @.TRANS_VOICE_WEIGHT=TRANS_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP
> with
> (nolock) WHERE ACCOUNTID=@.ACCOUNTID
> AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.TR
> if @.TRANS_VOICE_WEIGHT is not null
> SET @.WEIGHTED_TR_DURATION = @.TRANS_VOICE_WEIGHT * @.DURATION
> --SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER with (nolock) WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.FILE_ASSIGNED_DATE = dbo. sfd_fn_File_Assigned_Date(@.TR,getdate())
> SET @.FILE_ASSIGNED_DATE = CONVERT(VARCHAR(12),@.FILE_ASSIGNED_DATE,
101)
> BEGIN transaction
> Insert into
> dbo. Distribution(VOICE_FILE_LOCATION,VOICE_F
ILE_NAME,ASSIGNED_TO_TR,TR_SHI
FT_ID,NOTES,
> TR_ASSIGNED_TIME) values
> (@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
TR,@.SHIFT_ID
> ,'Distributed to Tr',GETDATE())
> If @.@.error != 0
> BEGIN
> rollback transaction
> RETURN 0
> END
> IF EXISTS(SELECT EMPID from emp_assigned_workload WHERE EMPID=@.TR AND
> FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID)
> Update EMP_ASSIGNED_WORKLOAD with (updlock) SET
> WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_TR
_DURATION,
> TR_WORK_ASSIGNED=TR_WORK_ASSIGNED+@.DURAT
ION WHERE EMPID=@.TR AND
> FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID
> ELSE
> INSERT INTO
> EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_C
OMPLETED,
> UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGN
ED,SHIFT_ID,
> WORK_ASSIGNED) VALUES
> (@.FILE_ASSIGNED_DATE,@.TR,0,0,0,0,0,@.DURA
TION,@.SHIFT_ID,
> @.WEIGHTED_TR_DURATION)
> If @.@.error != 0 OR @.@.ROWCOUNT=0
> BEGIN
> rollback transaction
> RETURN -10
> END
> INSERT INTO
> VOICE_FILE_PRIORITY(VOICE_FILE_LOCATION,
VOICE_FILE_NAME,ACCOUNT_ID,PROVIDE
R_ID,
> PRIORITY,ASSIGNED_TO_TR,ASSIGNED_TO_PR,I
MPORTANCE,STATUS,PENDINGSTATUS)
> VALUES
> (@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
ACCOUNTID,@.PROVIDER_ID,
> @.MAX_Priority,@.TR ,NULL,@.STAT_FLAG,'N',NULL)
> If @.@.error != 0
> BEGIN
> rollback transaction
> RETURN 0
> END
> -- written by raghu for deadlock
> select @.MASTER_BLOCKED = blocked from dbo.master.sysprocesses with
> (nolock)
> where blocked<>0
> if @.MASTER_BLOCKED=0
> begin
> UPDATE VOICE_FILES with (updlock) SET STATUS='T',TYPE= CASE WHEN TYPE='B'
> OR TYPE='R' THEN 'N' ELSE TYPE END,UPDATE_TIME=getdate()
> WHERE VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
> VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.error <> 0 or @.@.rowcount=0
> BEGIN
> rollback transaction
> RETURN 0
> END
> end
> else
> begin
> SET LOCK_TIMEOUT 30
> if @.@.LOCK_TIMEOUT= 30
> rollback transaction
> RETURN 0
> end
> -- end by raghu
>
>
> COMMIT TRANSACTION
> RETURN 1
> GO
>
> --end sp|||You should really be careful about using WITH(NOLOCK) when SELECTing
information that will be used in an INSERT or UPDATE. It can allow garbage
to be stored in the database.
At first glance, I notice a pattern that I would do different. I would
apply the update lock when you read the row to be updated. For example:
IF EXISTS(SELECT...FROM...WITH(UPDLOCK) WHERE...)
UPDATE...
With your code (depending on the transaction isolation level), the SELECT
will obtain a shared lock and then the UPDATE will attempt to obtain an
exclusive lock. This can cause a deadlock because more than one transaction
can obtain a shared lock, and then neither can obtain an exclusive lock
because both transactions have a shared lock.
You should avoid using a cursor within a transaction.
WITH(UPDLOCK) on a single-row update doesn't accomplish anything, because an
UPDATE always applies an exclusive lock on any row that is updated.
Also, there's nothing evil about using GOTO for error handling.
After further examination, these procedures are a disaster waiting to
happen. You really need to learn how locking works in SQL Server. You need
to learn about transaction isolation levels and how each affects lock
duration. You need to learn about how concurrent transactions
interact--especially when using WITH(NOLOCK). You also need to learn how to
write set-based statements instead of using cursors. In addition, you
should learn about optimistic concurrency, how to use rowversion (timestamp)
columns, and how to recover from collisions.
"raghu veer" <raghu veer@.discussions.microsoft.com> wrote in message
news:E05516F6-C1D4-418F-9111-C7EFD74B5BF3@.microsoft.com...
>i have 2 stored procedures
> 1st sp
> CREATE procedure dbo.sfd_sp_Assign_PR
> @.VOICE_FILE_LOCATION VARCHAR(255),
> @.VOICE_FILE_NAME VARCHAR(255),
> @.PR VARCHAR(10),
> @.TYPE CHAR(1),
> @.ACCOUNTID varchar(10) OUTPUT,
> @.DICTATOR VARCHAR(50) OUTPUT
> AS
> DECLARE @.Count as int
> DECLARE @.LAST_PR as VARCHAR(10)
> DECLARE @.TR as VARCHAR(10)
> DECLARE @.PROVIDER_ID as smallint
> DECLARE @.DURATION as int
> DECLARE @.SHIFT_ID as varchar(10)
> DECLARE @.TR_SHIFT_ID as varchar(10)
> DECLARE @.FILE_ASSIGNED_DATE as datetime
> DECLARE @.TR_ASSIGNED_TIME as datetime
> DECLARE @.undertranscription as int
> DECLARE @.readyforproofreading as int
> DECLARE @.MaxSequence as int
> DECLARE @.WEIGHTED_PR_DURATION as int
> DECLARE @.PR_VOICE_WEIGHT as int
> DECLARE @.TR_STATUS AS CHAR(1)
> DECLARE @.PR_DOC_FILE_NAME AS VARCHAR(255)
> DECLARE @.PR_DOC_FILE_LOCATION AS VARCHAR(255)
> DECLARE @.PATIENT_NAME AS VARCHAR(50)
> DECLARE @.MRN AS VARCHAR(20)
> DECLARE @.VISIT_DATE AS SMALLDATETIME
> DECLARE @.TR_CHAR_COUNT AS INT
> DECLARE @.VF_START_TIME AS SMALLINT
> DECLARE @.VF_END_TIME AS SMALLINT
> DECLARE @.TR_NOTES AS VARCHAR(1000)
> DECLARE @.TR_CHECKIN_DATETIME AS DATETIME
> DECLARE @.REPORT_TYPE AS VARCHAR(50)
> DECLARE @.TR_PL_STATUS AS SMALLINT
> DECLARE @.LOCATIONID AS SMALLINT
> DECLARE @.TR_TRANSCRIPTS CURSOR
> SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
> @.WEIGHTED_PR_DURATION=WEIGHTED_PR_DURATI
ON FROM VOICE_FILES with (nolock)
> WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.ACCOUNTID is NULL
> return -1 --No record in voice files
> SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS with (nolock) WHERE
> ACCOUNTID=@.ACCOUNTID AND PROVIDER_ID=@.PROVIDER_ID
> If @.Count = 0
> return -2 --No record in account weights
> SELECT @.Count=count(*) from DISTRIBUTION with (nolock) where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count = 0
> return -3 --File not assigned to TR for transcription
> SELECT @.Count=count(*) FROM VOICE_FILE_PRIORITY with (nolock) WHERE
> VOICE_FILE_NAME=@.VOICE_FILE_NAME AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> If @.Count = 0
> return -4 --No record in voice file priority
> SELECT assigned_to_pr FROM DISTRIBUTION with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and ASSIGNED_TO_PR=@.PR
> if @.@.rowcount=1
> return -5 --file already assigned to selected PR
> SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.PR AND
> getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = DATEPART(dw,getdate())
> If @.SHIFT_ID IS NULL
> return -6 --Shift ID not found
> SET @.undertranscription = 0
> SET @.readyforproofreading = 0
> SELECT @.MaxSequence=max(sequence) from distribution with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> SELECT @.LAST_PR=ASSIGNED_TO_PR FROM DISTRIBUTION with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME and sequence=@.MaxSequence
> IF @.MaxSequence=1 AND @.LAST_PR IS NULL
> BEGIN
> SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS in
> ('A','O') AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.Count > 0
> SET @.undertranscription = @.Duration
> SELECT @.Count=COUNT(*) FROM VF_TR_ASSIGN with (nolock) WHERE STATUS='C'
> AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.Count > 0
> SET @.readyforproofreading = @.Duration
> END
> SELECT @.PR_VOICE_WEIGHT=PR_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP with
> (nolock) WHERE ACCOUNTID=@.ACCOUNTID
> AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.PR
> if @.PR_VOICE_WEIGHT IS NOT NULL
> SET @.WEIGHTED_PR_DURATION = @.PR_VOICE_WEIGHT * @.DURATION
> --SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER WHERE PROVIDER_ID=@.PROVIDER_ID
> SELECT @.FILE_ASSIGNED_DATE=dbo. sfd_fn_File_Assigned_Date(@.PR,getdate())
> SET @.FILE_ASSIGNED_DATE=CONVERT(VARCHAR(12),
@.FILE_ASSIGNED_DATE,101)
>
> BEGIN TRANSACTION
> IF @.LAST_PR IS NULL AND @.MaxSequence=1
> BEGIN
> UPDATE TR_TRANSCRIPTS with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> UPDATE DISTRIBUTION with (updlock) SET
> ASSIGNED_TO_PR=@.PR,PR_SHIFT_ID=@.SHIFT_ID
,
> PR_ASSIGNED_TIME=GETDATE(),DISTRIBUTION_
TYPE=@.TYPE WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND SEQUENCE=1
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR != 0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> END
> ELSE
> BEGIN
> SELECT @.TR_STATUS=TR_STATUS, @.TR=ASSIGNED_TO_TR, @.TR_SHIFT_ID=TR_SHIFT_ID,
> @.TR_ASSIGNED_TIME=TR_ASSIGNED_TIME
> FROM DISTRIBUTION with (nolock) WHERE sequence=1 AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> INSERT INTO DISTRIBUTION (VOICE_FILE_LOCATION, VOICE_FILE_NAME,
> TR_STATUS,SEQUENCE,NOTES,PR_STATUS,ASSIG
NED_TO_TR,ASSIGNED_TO_PR,TR_SHIFT_
ID,PR_SHIFT_ID,TR_ASSIGNED_TIME,PR_ASSIG
NED_TIME,DISTRIBUTION_TYPE)
> values
> (@.VOICE_FILE_LOCATION, @.VOICE_FILE_NAME, @.TR_STATUS, @.MaxSequence+1,
> 'Distributed to Tr', 'N', @.TR, @.PR, @.TR_SHIFT_ID, @.SHIFT_ID,
> @.TR_ASSIGNED_TIME, GETDATE(),@.TYPE)
> -- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE PR_STATUS IN ('N','P')
> AND VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> -- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> -- IF @.COUNT = 0
> -- BEGIN
> -- SELECT @.COUNT=COUNT(*) FROM TR_TRANSCRIPTS WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> -- AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> -- IF @.COUNT > 0
> -- BEGIN
> UPDATE VOICE_FILE_PRIORITY with (updlock) SET ASSIGNED_TO_PR=@.PR WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND ASSIGNED_TO_PR IS NULL
> If @.@.ERROR != 0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> -- END
> -- END
> SET @.TR_TRANSCRIPTS = CURSOR FOR
> SELECT
> PATIENT_NAME,MRN,VISIT_DATE,TR_CHAR_COUN
T,PR_DOC_FILE_NAME,PR_DOC_FILE_LOC
ATION,VF_START_TIME,VF_END_TIME,TR_NOTES
,REPORT_TYPE,TR_CHECKIN_DATETIME,TR_
PL_STATUS,ASSIGNED_TO_TR,LOCATIONID
> FROM TR_TRANSCRIPTS with (nolock) where ASSIGNED_TO_PR=@.LAST_PR AND
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME AND PR_DOC_FILE_NAME IS NOT NULL
> OPEN @.TR_TRANSCRIPTS
> FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
> @.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FI
LE_LOCATION,@.VF_START_TIME,@.VF_END_TIME,
@.TR_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_D
ATETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
> WHILE (@.@.FETCH_STATUS = 0)
> BEGIN
> INSERT INTO
> TR_TRANSCRIPTS(DOC_FILE_NAME,DOC_FILE_LO
CATION,PATIENT_NAME,MRN,VISIT_DATE,TRANS
CR
IPTION_STATUS,TR_CHAR_COUNT,VOICE_FILE_N
AME,VOICE_FILE_LOCATION,ASSIGNED_TO_TR,T
R_CH
ECKIN_DATETIME,PR_STATUS,VF_START_TIME,V
F_END_TIME,TR_NOTES,REPORT_TYPE,TR_PL_ST
ATUS
,AS
SIGNED_TO_PR,LOCATIONID)
> VALUES
> (@.PR_DOC_FILE_NAME,@.PR_DOC_FILE_LOCATION
,@.PATIENT_NAME,@.MRN,@.VISIT_DATE,'A
',@.TR_CHAR_COUNT,@.VOICE_FILE_NAME,@.VOICE
_FILE_LOCATION,@.TR,@.TR_CHECKIN_DATET
IME,'N',@.VF_START_TIME,@.VF_END_TIME,@.TR_
NOTES,@.REPORT_TYPE,@.TR_PL_STATUS,@.PR
,@.LOCATIONID)
> SET @.undertranscription = 0
> SET @.readyforproofreading = @.Duration
> FETCH NEXT FROM @.TR_TRANSCRIPTS INTO
> @.PATIENT_NAME,@.MRN,@.VISIT_DATE,@.TR_CHAR_
COUNT,@.PR_DOC_FILE_NAME,@.PR_DOC_FI
LE_LOCATION,@.VF_START_TIME,@.VF_END_TIME,
@.TR_NOTES,@.REPORT_TYPE,@.TR_CHECKIN_D
ATETIME,@.TR_PL_STATUS,@.TR,@.LOCATIONID
> END
> CLOSE @.TR_TRANSCRIPTS
> DEALLOCATE @.TR_TRANSCRIPTS
>
> --UPDATE EMP_ASSIGNED_WORKLOAD
> END
> --common
> UPDATE VOICE_FILES with (updlock) SET STATUS='A',TYPE= CASE WHEN TYPE='B'
> OR TYPE='R' THEN 'N' ELSE TYPE END WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
> VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.ERROR !=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN 0
> END
> IF EXISTS(SELECT empid from emp_assigned_workload with (nolock) WHERE
> EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID)
> UPDATE EMP_ASSIGNED_WORKLOAD with (updlock) SET
> UNDER_TRANSCRIPTION=UNDER_TRANSCRIPTION+
@.undertranscription,READY_FOR_PROO
FREADING=READY_FOR_PROOFREADING+@.readyfo
rproofreading,
> WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_PR
_DURATION,
> PR_WORK_ASSIGNED=PR_WORK_ASSIGNED+@.DURAT
ION
> WHERE EMPID=@.PR AND FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE AND
> SHIFT_ID=@.SHIFT_ID
> ELSE
> INSERT INTO
> EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_C
OMPLETED,
> UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGN
ED,SHIFT_ID,
> WORK_ASSIGNED) VALUES (@.FILE_ASSIGNED_DATE,@.PR,0,0,@.undertrans
cription,
> @.readyforproofreading,@.DURATION,0,@.SHIFT
_ID,@.WEIGHTED_PR_DURATION)
> If @.@.ERROR !=0 OR @.@.ROWCOUNT=0
> BEGIN
> ROLLBACK TRANSACTION
> RETURN -10
> END
> COMMIT TRANSACTION
> RETURN 1
> GO
> --end 1st sp
> 2nd sp
> CREATE procedure dbo.sfd_sp_Assign_TR
> @.VOICE_FILE_LOCATION VARCHAR(255),
> @.VOICE_FILE_NAME VARCHAR(255),
> @.TR VARCHAR(10),
> @.ACCOUNTID varchar(10) output,
> @.DICTATOR VARCHAR(50) output
> AS
> DECLARE @.Count as int
> DECLARE @.PROVIDER_ID as smallint
> DECLARE @.WEIGHTED_TR_DURATION as int
> DECLARE @.DURATION as int
> DECLARE @.SHIFT_ID as varchar(10)
> DECLARE @.STAT_FLAG as char(1)
> DECLARE @.MAX_Priority as int
> DECLARE @.PRIORITY as bit
> DECLARE @.FILE_ASSIGNED_DATE as datetime
> DECLARE @.TRANS_VOICE_WEIGHT AS int
> DECLARE @.MASTER_BLOCKED AS int
> SELECT @.ACCOUNTID=ACCOUNTID,@.PROVIDER_ID=PROVID
ER_ID,@.DURATION=DURATION,
> @.WEIGHTED_TR_DURATION=WEIGHTED_TR_DURATI
ON,@.STAT_FLAG=STAT_FLAG FROM
> VOICE_FILES with (nolock) WHERE
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> IF @.ACCOUNTID is NULL
> return -1 --No record in voice files
> SELECT @.Count=COUNT(*) FROM ACCOUNT_WEIGHTS WHERE ACCOUNTID=@.ACCOUNTID AND
> PROVIDER_ID=@.PROVIDER_ID
> If @.Count = 0
> return -2 --No record in account weights
> SELECT @.Count=count(*) from vf_tr_assign where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count > 0
> return -3 --TR has already started working on the file
> SELECT @.SHIFT_ID=SHIFT_ID FROM EMP_SHIFT with (nolock) WHERE EMPID=@.TR AND
> getdate() >= EFFECTIVE_DATE AND DAY_OF_WEEK = datepart(dw,getdate())
> If @.SHIFT_ID is null
> return -4 --Shift ID not found
> SELECT @.Count=count(*) from DISTRIBUTION where
> VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
> AND VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.Count > 0
> return -5 --File already assigned for transcription
> if @.STAT_FLAG='S' or @.STAT_FLAG='P'
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE=@.STAT_FLAG
> ELSE
> BEGIN
> SELECT @.PRIORITY=PRIORITY FROM ACCOUNT with (nolock) WHERE
> ACCOUNTID=@.ACCOUNTID
> IF @.PRIORITY is null
> set @.PRIORITY=0
> IF @.PRIORITY = 1
> BEGIN
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE='N'
> set @.STAT_FLAG='N'
> END
> ELSE
> BEGIN
> SELECT @.MAX_Priority=MAX(PRIORITY) FROM VOICE_FILE_PRIORITY with (nolock)
> WHERE IMPORTANCE='L'
> set @.STAT_FLAG='L'
> END
> END
> if @.MAX_Priority is null
> set @.MAX_Priority = 0
> set @.MAX_Priority = @.MAX_Priority + 1
> SELECT @.TRANS_VOICE_WEIGHT=TRANS_VOICE_WEIGHT FROM ACCOUNT_WEIGHTS_EXCP
> with
> (nolock) WHERE ACCOUNTID=@.ACCOUNTID
> AND PROVIDER_ID=@.PROVIDER_ID AND EMPID=@.TR
> if @.TRANS_VOICE_WEIGHT is not null
> SET @.WEIGHTED_TR_DURATION = @.TRANS_VOICE_WEIGHT * @.DURATION
> --SELECT @.DICTATOR=FIRST_NAME + ' ' + LAST_NAME FROM PROVIDER WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.DICTATOR=DICTATED_BY FROM PROVIDER with (nolock) WHERE
> PROVIDER_ID=@.PROVIDER_ID
> SELECT @.FILE_ASSIGNED_DATE = dbo. sfd_fn_File_Assigned_Date(@.TR,getdate())
> SET @.FILE_ASSIGNED_DATE = CONVERT(VARCHAR(12),@.FILE_ASSIGNED_DATE,
101)
> BEGIN transaction
> Insert into
> dbo. Distribution(VOICE_FILE_LOCATION,VOICE_F
ILE_NAME,ASSIGNED_TO_TR,TR_SHI
FT_ID,NOTES,
> TR_ASSIGNED_TIME) values
> (@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
TR,@.SHIFT_ID
> ,'Distributed to Tr',GETDATE())
> If @.@.error != 0
> BEGIN
> rollback transaction
> RETURN 0
> END
> IF EXISTS(SELECT EMPID from emp_assigned_workload WHERE EMPID=@.TR AND
> FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID)
> Update EMP_ASSIGNED_WORKLOAD with (updlock) SET
> WORK_ASSIGNED=WORK_ASSIGNED+@.WEIGHTED_TR
_DURATION,
> TR_WORK_ASSIGNED=TR_WORK_ASSIGNED+@.DURAT
ION WHERE EMPID=@.TR AND
> FILE_ASSIGNED_DATE=@.FILE_ASSIGNED_DATE
> AND SHIFT_ID=@.SHIFT_ID
> ELSE
> INSERT INTO
> EMP_ASSIGNED_WORKLOAD(FILE_ASSIGNED_DATE
,EMPID,PR_WORK_COMPLETED,TR_WORK_C
OMPLETED,
> UNDER_TRANSCRIPTION,READY_FOR_PROOFREADI
NG,PR_WORK_ASSIGNED,TR_WORK_ASSIGN
ED,SHIFT_ID,
> WORK_ASSIGNED) VALUES
> (@.FILE_ASSIGNED_DATE,@.TR,0,0,0,0,0,@.DURA
TION,@.SHIFT_ID,
> @.WEIGHTED_TR_DURATION)
> If @.@.error != 0 OR @.@.ROWCOUNT=0
> BEGIN
> rollback transaction
> RETURN -10
> END
> INSERT INTO
> VOICE_FILE_PRIORITY(VOICE_FILE_LOCATION,
VOICE_FILE_NAME,ACCOUNT_ID,PROVIDE
R_ID,
> PRIORITY,ASSIGNED_TO_TR,ASSIGNED_TO_PR,I
MPORTANCE,STATUS,PENDINGSTATUS)
> VALUES
> (@.VOICE_FILE_LOCATION,@.VOICE_FILE_NAME,@.
ACCOUNTID,@.PROVIDER_ID,
> @.MAX_Priority,@.TR ,NULL,@.STAT_FLAG,'N',NULL)
> If @.@.error != 0
> BEGIN
> rollback transaction
> RETURN 0
> END
> -- written by raghu for deadlock
> select @.MASTER_BLOCKED = blocked from dbo.master.sysprocesses with
> (nolock)
> where blocked<>0
> if @.MASTER_BLOCKED=0
> begin
> UPDATE VOICE_FILES with (updlock) SET STATUS='T',TYPE= CASE WHEN TYPE='B'
> OR TYPE='R' THEN 'N' ELSE TYPE END,UPDATE_TIME=getdate()
> WHERE VOICE_FILE_LOCATION=@.VOICE_FILE_LOCATION
AND
> VOICE_FILE_NAME=@.VOICE_FILE_NAME
> If @.@.error <> 0 or @.@.rowcount=0
> BEGIN
> rollback transaction
> RETURN 0
> END
> end
> else
> begin
> SET LOCK_TIMEOUT 30
> if @.@.LOCK_TIMEOUT= 30
> rollback transaction
> RETURN 0
> end
> -- end by raghu
>
>
> COMMIT TRANSACTION
> RETURN 1
> GO
>
> --end sp

PLEASE PLEASE HELP - How can I get a return value from a SQL Stored Proc is ASP.NET?

Hi. I'm sorry to bother all of you, but I have spent two days looking
at code samples all over the internet, and I can not get a single one
of them to work for me. I am simply trying to get a value returned to
the ASP from a stored procedure. The error I am getting is: Item can
not be found in the collection corresponding to the requested name or
ordinal.

Here is my Stored Procedure code.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
ALTER PROCEDURE [dbo].[sprocRetUPC]
@.sUPC varchar(50),
@.sRetUPC varchar(50) OUTPUT

AS

BEGIN
SET NOCOUNT ON;
SET @.sRetUPC = (SELECT bcdDVD_Title FROM tblBarcodes WHERE bcdUPC =
@.sUPC)
RETURN @.sRetUPC

END

Here is my ASP.NET code.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim oConnSQL As ADODB.Connection

oConnSQL = New ADODB.Connection
oConnSQL.ConnectionString = "DSN=BarcodeSQL"
oConnSQL.Open()

Dim oSproc As ADODB.Command
oSproc = New ADODB.Command
oSproc.ActiveConnection = oConnSQL
oSproc.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
oSproc.CommandText = "sprocRetUPC"

Dim oParam1
Dim oParam2
oParam1 = oSproc.CreateParameter("sRetUPC",
ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamOutput, 50)
oParam2 = oSproc.CreateParameter("sUPC", ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamInput, 50, "043396005396")

Dim res
res = oSproc("sRetUPC")

Response.Write(res.ToString())

End Sub

If I put the line -
oSproc.Execute()

above the "Dim res" line, I end up with the following error:
Procedure or function 'sprocRetUPC' expects parameter '@.sUPC', which
was not supplied. I thought that oParam2 was the parameter. I was also
under the assumption that the return parameter has to be declared
first. What am I doing wrong here?jbonifacejr wrote:

Quote:

Originally Posted by

>
Hi. I'm sorry to bother all of you, but I have spent two days looking
at code samples all over the internet, and I can not get a single one
of them to work for me. I am simply trying to get a value returned to
the ASP from a stored procedure. The error I am getting is: Item can
not be found in the collection corresponding to the requested name or
ordinal.
>
Here is my Stored Procedure code.
>
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
ALTER PROCEDURE [dbo].[sprocRetUPC]
@.sUPC varchar(50),
@.sRetUPC varchar(50) OUTPUT
>
AS
>
BEGIN
SET NOCOUNT ON;
SET @.sRetUPC = (SELECT bcdDVD_Title FROM tblBarcodes WHERE bcdUPC =
@.sUPC)
RETURN @.sRetUPC
>
END
>
Here is my ASP.NET code.
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
>
Dim oConnSQL As ADODB.Connection
>
oConnSQL = New ADODB.Connection
oConnSQL.ConnectionString = "DSN=BarcodeSQL"
oConnSQL.Open()
>
Dim oSproc As ADODB.Command
oSproc = New ADODB.Command
oSproc.ActiveConnection = oConnSQL
oSproc.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
oSproc.CommandText = "sprocRetUPC"
>
Dim oParam1
Dim oParam2
oParam1 = oSproc.CreateParameter("sRetUPC",
ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamOutput, 50)
oParam2 = oSproc.CreateParameter("sUPC", ADODB.DataTypeEnum.adVarChar,
ADODB.ParameterDirectionEnum.adParamInput, 50, "043396005396")
>
Dim res
res = oSproc("sRetUPC")
>
Response.Write(res.ToString())
>
End Sub
>
If I put the line -
oSproc.Execute()
>
above the "Dim res" line, I end up with the following error:
Procedure or function 'sprocRetUPC' expects parameter '@.sUPC', which
was not supplied. I thought that oParam2 was the parameter. I was also
under the assumption that the return parameter has to be declared
first. What am I doing wrong here?


Just a few pointers here:
- creating a parameter will just create a parameter. To use it, you need
to add it to the command object using oSProc.Parameters.Append
- in a stored procedure you can only use the RETURN keyword to return an
integer, so @.sRetUPC is out of the question
- if you want to use the value of the output parameter, then you should
access it through the Parameters collection of the Command object. The
syntax you are currently using refers to the resultset, but the stored
procedure does not have one

HTH,
Gert-Jan|||Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...

I removed the Return @.sRetUPC line. I am guessing that I can rely on
the set @.sRetUPC line to set the value of the output parameter

Quote:

Originally Posted by

>From there, I appended the parameters in the ASP code...like this


oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.

Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()

This is not working. Do you know how I can get access to the value of
the parameter that is returned?

Quote:

Originally Posted by

Just a few pointers here:
- creating a parameter will just create a parameter. To use it, you need
to add it to the command object using oSProc.Parameters.Append
- in a stored procedure you can only use the RETURN keyword to return an
integer, so @.sRetUPC is out of the question
- if you want to use the value of the output parameter, then you should
access it through the Parameters collection of the Command object. The
syntax you are currently using refers to the resultset, but the stored
procedure does not have one
>
HTH,
Gert-Jan

|||jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...
>
I removed the Return @.sRetUPC line. I am guessing that I can rely on
the set @.sRetUPC line to set the value of the output parameter
>

Quote:

Originally Posted by

>>From there, I appended the parameters in the ASP code...like this


oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.
>
Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()
>
This is not working. Do you know how I can get access to the value of
the parameter that is returned?


Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?

Since I don't even know how your code looks like right now, just two
notes:

1) Use parameter names with leading @.. The underlying provider may
prefer that.

2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)

--
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|||If you look at the top post you will see where I put the code I am
using. I also tried to let people know what happened when I tried their
suggestions. But, thanks for the advice...and I'll look at those SQL
Books online.

Jan

Erland Sommarskog wrote:

Quote:

Originally Posted by

jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...

I removed the Return @.sRetUPC line. I am guessing that I can rely on
the set @.sRetUPC line to set the value of the output parameter

Quote:

Originally Posted by

>From there, I appended the parameters in the ASP code...like this


oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.

Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()

This is not working. Do you know how I can get access to the value of
the parameter that is returned?


>
Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?
>
Since I don't even know how your code looks like right now, just two
notes:
>
1) Use parameter names with leading @.. The underlying provider may
prefer that.
>
2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)
>
>
--
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

|||Why are you using stored proc when you can use a function(s)? I always
thought the output parameter was a bit of a hack and clumsy to use.

jbonifacejr wrote:

Quote:

Originally Posted by

If you look at the top post you will see where I put the code I am
using. I also tried to let people know what happened when I tried their
suggestions. But, thanks for the advice...and I'll look at those SQL
Books online.
>
Jan
>
Erland Sommarskog wrote:

Quote:

Originally Posted by

jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

Thank you for your help. Any chance you have a moment to help just a
little more? Here is what I did, but I still can't access the value
output by the stored proc...
>
I removed the Return @.sRetUPC line. I am guessing that I can rely on
the set @.sRetUPC line to set the value of the output parameter
>
>>From there, I appended the parameters in the ASP code...like this
oSproc.Parameters.Append(oParam2)
oSproc.Parameters.Append(oParam1)
--originally I tried to do Param1 then Param2, but I got an error
about the parameter
--type being an output, so I just figured I had them in the wrong
order because the
--first parameter in the code was the output one.
>
Then, I added the line oSproc.Execute()
After that is:
Dim res
res = oSproc.Parameters.Item("sRetUPC").Value.toString()
>
This is not working. Do you know how I can get access to the value of
the parameter that is returned?


Never say "not working" in newsgroup post with explaining what it
means. Do you get an unexpected result? An error message? Something
else?

Since I don't even know how your code looks like right now, just two
notes:

1) Use parameter names with leading @.. The underlying provider may
prefer that.

2) Use adParamInputOutput for the output value. T-SQL does not have any
true output-only parameters. (Save the return value, but there is a
separate enum value for return values as I recall.)

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

|||jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

If you look at the top post you will see where I put the code I am
using.


Since then you changed the code according to Gert-Jan's advice, and we
don't know what it looked after that.

Basically, if you only say "not working" without specifying why, and
don't show us the code, don't expect that much help. But that's your call.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks Erland...I see where I screwed up. I thought I had explained
what was wrong, but I did that in a different thread in another forum.

Anyway, I got this working using classic ASP and ADODB. Now I am going
to try to get it working over ASP.NET and ADO.NET. Wish me luck.

So far, eveerything works except that I am constantly being told that
the stored procedure expects a parameter that was not supplied.
However, The same two parameters are created and added to the
Parameters of the command object.

I'll continue to work on it and see if I can get it to work. Looks like
I need a datareader or some other object. I found a great KB article
that basically shows me everythig I am doing (right and wrong)...

http://support.microsoft.com/kb/306574
Erland Sommarskog wrote:

Quote:

Originally Posted by

jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

If you look at the top post you will see where I put the code I am
using.


>
Since then you changed the code according to Gert-Jan's advice, and we
don't know what it looked after that.
>
Basically, if you only say "not working" without specifying why, and
don't show us the code, don't expect that much help. But that's your call.
>
>
>
--
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

|||jbonifacejr (jbonifacejr@.hotmail.com) writes:

Quote:

Originally Posted by

Thanks Erland...I see where I screwed up. I thought I had explained
what was wrong, but I did that in a different thread in another forum.


Posting the same question independently to two forums is not a nice
thing to. This means that people can waste time on answering your post,
when it has already been answered elsewhere.

Quote:

Originally Posted by

Anyway, I got this working using classic ASP and ADODB. Now I am going
to try to get it working over ASP.NET and ADO.NET. Wish me luck.
>
So far, eveerything works except that I am constantly being told that
the stored procedure expects a parameter that was not supplied.
However, The same two parameters are created and added to the
Parameters of the command object.


Again, without seeing your code it's hard to tell. There is a difference
between ADO and SqlClient though: with ADO, the parameter names are
just local to the application, so if you misspell a parameter name,
you may get away with it. Not so with SqlClient.

Quote:

Originally Posted by

I'll continue to work on it and see if I can get it to work. Looks like
I need a datareader or some other object.


Since your procedure has an output parameter, but no result set, the most
conventient method to use is ExecuteNonQuery, in which case you only need
the Command object.
--
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