Showing posts with label group. Show all posts
Showing posts with label group. Show all posts

Friday, March 30, 2012

not a GROUP BY expression??

Why am I get this error message when I execute this query?

SELECT ODisciplina.BOUI FROM ODisciplina , Disciplina$student
WHERE ((Disciplina$student.PARENT$(+)=ODisciplina.BOUI))
GROUP BY (ODisciplina.BOUI )HAVING( Count(CHILD$)=0 )
ORDER BY (ODisciplina.NAME)

without the 3rd line it works and without the last line it works too.Tks, I've found the error. It should be:

SELECT ODisciplina.BOUI FROM ODisciplina , Disciplina$student
WHERE ((Disciplina$student.PARENT$(+)=ODisciplina.BOUI))
GROUP BY (ODisciplina.BOUI, ODisciplina.NAME )HAVING( Count(CHILD$)=0 )
ORDER BY (ODisciplina.NAME)

Monday, March 26, 2012

Noobie SQL question

I hope this is the right group to ask this question. If not please
direct me to a more appropriate group.
I'm really new at SQL so bear with me.
I'll try to simplify the situation as best as I can...
Three files, 2 columns in each file. File one columns are item number
and current cost. File 2 columns are item number and last cost. File 3
columns are item number and item type. Each file has the same number of
records. One field in file two is null because it is a kit which does
not have a last cost associated with it. The 'kit' item number is
associated with an item type of 'K' in file 3.
I'm trying to update the current cost in file one with the last cost in
file 2. The command I've tried errors out when it gets to the 'kit'
record in file 2 because the current cost in file one can not be null.
How do I go about selecting only the non kit records in file 2 based on
the selection criteria available in file 3?
Any help or guidance would be greatly appreciated.
JimWhen you say "file", are you talking about "tables"? Or actual "files" in a
filesystem?
If you're talking about actual "tables", posting your DDL and some sample
data will help people to help you with a specific answer to your question.
Thanks
"Brass Balls" <me@.hotmail.com> wrote in message
news:d69Xd.1241$jW6.1155@.fe2.columbus.rr.com...
>I hope this is the right group to ask this question. If not please
> direct me to a more appropriate group.
> I'm really new at SQL so bear with me.
> I'll try to simplify the situation as best as I can...
> Three files, 2 columns in each file. File one columns are item number
> and current cost. File 2 columns are item number and last cost. File 3
> columns are item number and item type. Each file has the same number of
> records. One field in file two is null because it is a kit which does
> not have a last cost associated with it. The 'kit' item number is
> associated with an item type of 'K' in file 3.
> I'm trying to update the current cost in file one with the last cost in
> file 2. The command I've tried errors out when it gets to the 'kit'
> record in file 2 because the current cost in file one can not be null.
> How do I go about selecting only the non kit records in file 2 based on
> the selection criteria available in file 3?
> Any help or guidance would be greatly appreciated.
> Jim
>|||Showing my unfamiliarity with the language I was using table and file
interchangeably. What's the difference?
Anyway here's what I'm starting with:
Table 1:
ITNBR CMAT
1000 1.00
1001 1.50
2000 3.00
9999 5.00
Table 2:
ITNBR LCOST HOUSE
1000 2.00 1
1001 2.75 2
2000 3.00 1
9999 1
Table 3:
ITNBR ITTYP
1000 2
1001 2
2000 7
9999 K
I'm trying to update the CMAT field in table 1 with the LCOST field in table
2
if the HOUSE field in table 2 equals 1. The CMAT field can not be a NULL
value. There is no LCOST associated with ITNBR 9999 in table 2 because it i
s a
kit and not a separately purchased item. Item types are designated in table
3:
2=purchased, 7=manufactured, K=kit.
The following command errors out when trying to update CMAT with the NULL va
lue
in table 2:
update amflibx/itemasb f1
set cmat = (select lcost from amflibx/itembl f2
where f1.itnbr = f2.itnbr and f2.house = '1')
where f1.itnbr in (select itnbr from amflibx/itemasa f3
where f3.ittyp <> 'K')
The final result should be:
Table 1:
ITNBR CMAT
1000 2.00
1001 1.50
2000 3.00
9999
Any suggestions?
Jim
In article <bL9Xd.10974$nL5.5877@.fe11.lga>, xyz@.abcdef.com says...
>
>When you say "file", are you talking about "tables"? Or actual "files" in
a
>filesystem?
>If you're talking about actual "tables", posting your DDL and some sample
>data will help people to help you with a specific answer to your question.
>Thanks
>"Brass Balls" <me@.hotmail.com> wrote in message
>news:d69Xd.1241$jW6.1155@.fe2.columbus.rr.com...
>|||"Jim" <you@.somehost.com> wrote in message
news:422df802$0$30430$bb4e3ad8@.newscene.com...
> Showing my unfamiliarity with the language I was using table and file
> interchangeably. What's the difference?
>
OK, I thought you might be referring to tables, just wanted to make sure. A
'file' is an actual physical file, like a comma-delimited or other file on
your hard drive. You might see references on here to people importing or
exporting flat files or XML files. Other than your SQL MDF database file
and LDF log file, "files" are generally external to your database. Tables
are internal to the database.

> Anyway here's what I'm starting with:
> Table 1:
<snip>
> 9999 K
>
Just so you know, it's generally considered a good idea to post your actual
DDL (Data Definition Language) code so that people trying to help can
cut-and-paste to create the tables locally on their servers. Helps
troubleshoot your problem more quickly. You can generate your DDL using SQL
Ent Manager's Generate SQL Script function.

> I'm trying to update the CMAT field in table 1 [itemsab] with the LCOST
> field in table 2 [itemtbl]
> if the HOUSE field in table 2 [itemtbl] equals 1.
> The CMAT field can not be a NULL
> value. [You have it set as a NULL value in your example for item 9999?]
> There is no LCOST associated with ITNBR 9999 in table 2 because it is a
> kit and not a separately purchased item. [You have to decide if your
> business rules
> allow NULL values in CMAT or not...] Item types are designated in table
> 3:
> 2=purchased, 7=manufactured, K=kit.
> The following command errors out when trying to update CMAT with the NULL
> value
> in table 2:
> update amflibx/itemasb f1
> set cmat = (select lcost from amflibx/itembl f2
> where f1.itnbr = f2.itnbr and f2.house = '1')
> where f1.itnbr in (select itnbr from amflibx/itemasa f3
> where f3.ittyp <> 'K')
>
Not sure where the forward slashes are coming from, but here's a version
that *almost* gives the results you want:
UPDATE itemsab
SET CMAT = b.tempLCost
FROM
(
SELECT f1.itnbr AS PartNum, COALESCE(f2.lcost, 0) AS tempLCost, f3.ittyp
FROM itemsab f1 INNER JOIN itemtbl f2
ON f1.itnbr = f2.itnbr
INNER JOIN itemasa f3
ON f1.itnbr = f3.itnbr
AND f2.house = 1 AND f3.ittyp <> 'K'
) b
WHERE itnbr = b.PartNum
The reason it's almost what you want is that your criteria are in conflict
with one another. You say that the CMAT cannot be NULL, yet you try to set
it to NULL. It is probably defined as NOT NULL in the database. If you
want to, you can assign a 0 to it instead of a NULL using the COALESCE()
function as above. This query, with your sample data updates only 2 rows -
it does not update the 9999 item since we specifically exclude 'K' item
types. If you want it to update all House Types of 1, including 'K' types,
like 9999, try:
UPDATE itemsab
SET CMAT = b.tempLCost
FROM
(
SELECT f1.itnbr AS PartNum, COALESCE(f2.lcost, 0) AS tempLCost, f3.ittyp
FROM itemsab f1 INNER JOIN itemtbl f2
ON f1.itnbr = f2.itnbr
INNER JOIN itemasa f3
ON f1.itnbr = f3.itnbr
AND f2.house = 1
) b
WHERE itnbr = b.PartNum
If you want to be able to assign NULL values to CMAT field, you need to make
sure it is set to Allow NULL values. (*Not* NOT NULL). Then you can drop
the COALESCE():
UPDATE itemsab
SET CMAT = b.tempLCost
FROM
(
SELECT f1.itnbr AS PartNum, f2.lcost AS tempLCost, f3.ittyp
FROM itemsab f1 INNER JOIN itemtbl f2
ON f1.itnbr = f2.itnbr
INNER JOIN itemasa f3
ON f1.itnbr = f3.itnbr
AND f2.house = 1
) b
WHERE itnbr = b.PartNum

> The final result should be:
> Table 1:
> ITNBR CMAT
> 1000 2.00
> 1001 1.50
> 2000 3.00
> 9999
>
Hope that helps.|||Mike,
Thanks for your efforts in helping me working this out.
Isn't there a way to select only records in table 2 with non null values in
'lcost' based on a linkage to 'itnbr's in table 3 where the 'ittyp' does not
equal 'K'?
And then using only those records in table 2 to update the records in table
1.
Thinking while I'm typing can't this check for not null values in 'lcost' be
made part of the first selection criteria and abandon the references to tabl
e 3
altogether?
Jim
In article <%suXd.10583$Sq7.6550@.fe09.lga>, xyz@.abcdef.com says...
>
>"Jim" <you@.somehost.com> wrote in message
>news:422df802$0$30430$bb4e3ad8@.newscene.com...
>OK, I thought you might be referring to tables, just wanted to make sure.
A
>'file' is an actual physical file, like a comma-delimited or other file on
>your hard drive. You might see references on here to people importing or
>exporting flat files or XML files. Other than your SQL MDF database file
>and LDF log file, "files" are generally external to your database. Tables
>are internal to the database.
>
><snip>
>Just so you know, it's generally considered a good idea to post your actual
>DDL (Data Definition Language) code so that people trying to help can
>cut-and-paste to create the tables locally on their servers. Helps
>troubleshoot your problem more quickly. You can generate your DDL using SQ
L
>Ent Manager's Generate SQL Script function.
>
>Not sure where the forward slashes are coming from, but here's a version
>that *almost* gives the results you want:
>UPDATE itemsab
>SET CMAT = b.tempLCost
>FROM
>(
> SELECT f1.itnbr AS PartNum, COALESCE(f2.lcost, 0) AS tempLCost, f3.ittyp
> FROM itemsab f1 INNER JOIN itemtbl f2
> ON f1.itnbr = f2.itnbr
> INNER JOIN itemasa f3
> ON f1.itnbr = f3.itnbr
> AND f2.house = 1 AND f3.ittyp <> 'K'
> ) b
>WHERE itnbr = b.PartNum
>The reason it's almost what you want is that your criteria are in conflict
>with one another. You say that the CMAT cannot be NULL, yet you try to set
>it to NULL. It is probably defined as NOT NULL in the database. If you
>want to, you can assign a 0 to it instead of a NULL using the COALESCE()
>function as above. This query, with your sample data updates only 2 rows -
>it does not update the 9999 item since we specifically exclude 'K' item
>types. If you want it to update all House Types of 1, including 'K' types,
>like 9999, try:
>UPDATE itemsab
>SET CMAT = b.tempLCost
>FROM
>(
> SELECT f1.itnbr AS PartNum, COALESCE(f2.lcost, 0) AS tempLCost, f3.ittyp
> FROM itemsab f1 INNER JOIN itemtbl f2
> ON f1.itnbr = f2.itnbr
> INNER JOIN itemasa f3
> ON f1.itnbr = f3.itnbr
> AND f2.house = 1
> ) b
>WHERE itnbr = b.PartNum
>If you want to be able to assign NULL values to CMAT field, you need to mak
e
>sure it is set to Allow NULL values. (*Not* NOT NULL). Then you can drop
>the COALESCE():
>UPDATE itemsab
>SET CMAT = b.tempLCost
>FROM
>(
> SELECT f1.itnbr AS PartNum, f2.lcost AS tempLCost, f3.ittyp
> FROM itemsab f1 INNER JOIN itemtbl f2
> ON f1.itnbr = f2.itnbr
> INNER JOIN itemasa f3
> ON f1.itnbr = f3.itnbr
> AND f2.house = 1
> ) b
>WHERE itnbr = b.PartNum
>
>Hope that helps.
>

Friday, March 23, 2012

Noob having troble with group by etc.

Well i guess it is a bit more than just group by but here we go...

General description :( background info on the problem; may or may not be needed)

I am trying to get a list of active card holders for a particular month. A card holder is considered active if their status was active any time during that month. I have a card holder status in my CardHolder table but that will only give me their current status. I also have a CardHolderHistory table that saves any changes to a card holder including their status. Using the CardHolderHistory table i can find out who was active for any month in question by using the following algorithm:

A card holder is active if they meet one or both of the following conditions;

1) The last entry before the month in question has them as active in CardHolderHistory or

2) They were set to active any time during the month in question

The problem:

I create a temporary table and copy the card holders into the table with separate select into statements for each of the above conditions. For the first condition, I first gather all the CardHolderHistory entries before the month in question with the following select

select EmployerName, cardholderhistory.FirstName, cardholderhistory.Initial, cardholderhistory.LastName, ChangeDate, CardHolderHistory.CardState, FKEmployerID

into #MonthlyCards from cardholderhistory left join Employer on PKEmployerID = cardholderhistory.FKEmployerID

where changedate<@.datespecified

This gives me ALL of the CardHolderHistory before the month in question (@.dateSpecified).

It would look something like this

EmployerName First Initial Last Status ChangeDate EmployerID

-

.

.

.

Some Fake Company John P Doe A 2006-05-02 11:34:26.360 109

Some Fake Company John P Doe A 2006-03-28 11:14:10.520 109

Some Fake Company Jane T Doe S 2006-05-30 15:34:14.900 109

Some Fake Company Jane T Doe A 2006-03-28 12:20:03.670 109

Some Fake Company Jane T Doe A 2006-03-01 10:12:45.320 109

.

. (S = suspended; A = Active)

.

Now I need to make sure people are only listed once and remove everyone whose status = suspended for their last change date. That is my problem; I can’t figure out how to remove these people. The next step i took was to do a group by with EmployerName, FirstName, Initial, LastName, Status, Max(ChangeDate), FKEmployerID. This removes everyone listed multiple times except if their status changes. After the group by the list would be

EmployerName First Initial Last Status ChangeDate EmployerID

-

Some Fake Company John P Doe A 2006-05-02 11:34:26.360 109

Some Fake Company Jane T Doe S 2006-05-30 15:34:14.900 109

Some Fake Company Jane T Doe A 2006-03-28 12:20:03.670 109

Now I need to delete Jane completely, since her latest status is S. I could delete where Status = ‘S’ but that leaves her as active from 3/28. I can’t figure out how to completely remove Jane. The rest I can figure out I think. Please help me get rid of Jane for good, I don’t like her.

Thanks for any help,

Rico

Instead of grabbing too many records, how about grabbing only the records you want. In this case, construct a query that uses a full outer join of the two conditions you have stated. Now you will have only one record for the active members and you can sort out the values of the columns.

Hopefully, this code gives you something to work with. I have assumed that there is a CardHolderID that is unique to each CardHolder record.

Select

From(

select c1.CardHolderID,

c1.FirstName,

c1.Initial,

c1.LastName,

c1.ChangeDate,

C1.CardState,

FKEmployerID

From cardholderhistory c1

Where ChangeDate = (

Select Top 1

changeDate

From cardholderhistory c2

Where c1.CardHolderID = c2.CardHolderID

And c2.ChangeDate < @.DateSpecified

Order by changeDate desc )

And CardState = 'A'

) t1

Full Outer Join

(

select c3.CardHolderID,

c3.FirstName,

c3.Initial,

c3.LastName,

c3.ChangeDate,

C3.CardState,

FKEmployerID

From cardholderhistory c3

Where ChangeDate between @.monthStart and @.monthEnd

) t2

On t1.CardHolderID = t2.CardHolderID

Join Employer

On t1.FKEmployerID = Employer.PKEmployerID

|||

Anomolus,

Thank you very much for your reply. I have wasted an embarrassing number of hours trying to figure this out but with your help I was able to get what I needed. The code i used was

declare @.dateSpecified datetime
--@.employerID int
declare @.endDate datetime

set @.dateSpecified = '8/1/2006'
set @.endDate = dateadd(m, 1, @.dateSpecified)


select EmployerName, chh.FirstName, chh.Initial, chh.LastName, FKEmployerID
into #MonthlyCards
From CardHolderHistory chh left join
Employer on PKEmployerID = FKemployerID
Where ChangeDate = ( Select Top 1 ChangeDate
From CardHolderHistory chh2
Where chh.PKCardHolderID = chh2.PKCardHolderID
And chh2.ChangeDate < @.DateSpecified
Order by changeDate desc )
And (CardState = 'A' or CardState = '')
Order By EmployerName, LastName, FirstName, Initial

insert into #MonthlyCards([EmployerName],[FirstName],[Initial],[LastName],[FKEmployerID])
(select EmployerName, chh.FirstName, chh.Initial, chh.LastName, FKEmployerID
From CardHolderHistory chh left join
Employer on PKEmployerID = FKemployerID
where (changedate between dateadd(ms, (-5),@.dateSpecified) and @.enddate)
And (CardState = 'A' or CardState = ''))

Select * from #monthlycards
group by EmployerName, FirstName, Initial, LastName, FKEmployerID
--order by pkcardholderid

drop table #MonthlyCards

I was having a problem getting that first condition in the form of a select. I have few follow-up questions, now that i got it to work.

1) Why use the Full outer join? It seems to get a bunch of info that i don't need and has a bunch of nulls. Wouldn't the full outer join join the two conditions into the same record?

2) What if there are too ChangeDates with the same value? Will this cause errors or is it generally acceptable to rely on the millisecond precision of the datetime data type?

3) Is there anything i should do to optimize this code? My CardHolderHistory table has thousands of entries already and it will continue to get bigger.

Again, thanks Anomolus, and thank you to anyone that can help with these questions,

Rico

|||

1) The Full outer join allows you to get the list of customers in a single select. The customers will match at least one of the two conditions and if it matches both, then they are combined into a single record. You will get nulls when the customer only satisfies one condition. It occurs to me that all you really need out of this is the CustomerID. Isn't there a risk of getting old information if you grab the name from history as it could have been what was changed last?

2) This is really a business question since your rule says the last change. If it is possible for two changes to happen at the same time, then you ought to have another field that designates the version order in the history table.

|||

"Isn't there a risk of getting old information if you grab the name from history as it could have been what was changed last?"

Yes there is, thanks for catching that! I thouught that i would save some resources by using one less join but you are right... i had thought of that in my earlier failed attemps but looked over it in this version.

"If it is possible for two changes to happen at the same time"

I dont think this is possible, maybe you could help me find out. The ChangeDate value is filled in automatically (the default value is getdate())when a new CardHolderHistory record is created. It is not changes beyond that. If 2 different administrators used the website and tried to update card holders at the same and my SaveCardHolder stored procedure is called at the same time im guessing this could generate the same ChangeDate. Besides the fact that it is unlikly, can more than one record be saved at the same time? I insert into the CardHolderHistory table from Save CardHolder using transactions (roll back on error otherwise commit) if this makes a difference.

Thank you for your help! You have really saved me!

sql

Tuesday, March 20, 2012

Nonemptycrossjoin alternative

Hi,

Trying to filter out measure group rows for various
conditions of different dimensions to do a sum of a measure

This is to be used in a cube calculation (mdx expression), not a mdx select statement.

The conditions for the date dimension is <>certain date and <=certain date, and hence the filter returns all member except the matching ones, which is a large set.

sum(
nonemptycrossjoin(

(filter(descendants([Purchase Date].[Fiscal Hierarchy].Members, , leaves),
([Purchase Date].[Fiscal Hierarchy].CurrentMember.Name <> "yyyy-dd-dd"
and [Purchase Date].[Fiscal Hierarchy].CurrentMember.Name <= "yyyy-dd-dd")))

,

(filter(descendants([Ship Date].[Fiscal Hierarchy].Members, , leaves),
([Ship Date].[Fiscal Hierarchy].CurrentMember.Name = "yyyy-dd-dd"
or [Ship Date].[Fiscal Hierarchy].CurrentMember.Name > "yyyy-dd-dd")))

))
,[Measures].[Test Measure]


The nonemptycrossjoin takes a long time to return results.
Is there a better way to do a not(conditions).
Or a better approach to filter measure group rows for
different conditions(and, or) of dimension memebers.

Using exists(filter1, filter2, 'Measuregroupname') does not return the expected results.
Also there are more than 2 dimensions to be filtered.

If the above statement is put in a subselect, it works faster,
but to use it in a cube calculation, can't use the 'create subcube' with the
subselect query inside a create member statement.

How can 'create subcube' be used in the Script in Cube calculations?

Regards

A couple of clarifications - is there a single "certain date" or multiple, and is the name pattern "yyyy-mm-dd", rather than "yyyy-dd-dd"?

Assuming that your Date [Fiscal Hierarchy] is chronologically ordered, it seems more efficient to directly compute the desired range of dates, rather than using Filter() - if the "certain date" is always going to be a member of the hierarchy.

|||

Thanks for the quick reply.
Sorry that was a typo.....it is in yyyy-mm-dd format.
'certain date' is like the fiscal year end date.

So the [Test Measure] must be added for all measure group rows
where the above conditions for Purchase date and Ship Date are met.

Regards

|||

Here's my interpretation of your requirement in the Adventure Works context, using [Date], [Ship Date] and [Sales Amount]. The date selected is "July 1, 2002", which is the fiscal year start:

>>

With

Member [Measures].[CertainDate] as "July 1, 2002"

Member [Measures].[SalesTest] as

Sum({NULL:StrToMember("[Date].[Fiscal].[Date].["

+ [Measures].[CertainDate] + "]").PrevMember} *

{StrToMember("[Ship Date].[Fiscal].[Date].["

+ [Measures].[CertainDate] + "]"):NULL},

[Measures].[Sales Amount])

select {[Measures].[CertainDate], [Measures].[SalesTest]} on 0

from [Adventure Works]

--

CertainDate SalesTest
July 1, 2002 $157,608.37

>>

Based on this entry in Mosha's blog, there's no need for NonEmptyCrossJoin():

http://sqljunkies.com/WebLog/mosha/archive/2006/10/09/nonempty_exists_necj.aspx

>>

...

We have put logic inside the MDX query optimizer to recognize the cases when the empty cells can be eliminated from the set automatically. So instead of using AS2000 style of formula

SUM(NonEmptyCrossJoin(Descendants(Product.CurrentMember, Product.[Product Name]), Descendants(Customer.CurrentMember, Customer.[Customer Name]))

in AS2005 it should be written as

SUM(Descendants(Product.CurrentMember, Product.[Product Name]) * Descendants(Customer.CurrentMember, Customer.[Customer Name])))

...

>>

|||

Hi:

I don't have an easy answer to your question for general performance improvement. However, I did not one thing about your filter statement. You specify the set to fileter on using the DESCENDANTS() function:

descendants([Purchase Date].[Fiscal Hierarchy].Members, , leaves)

I don't think you need to use the DESCENDANTS() function to specify your set. Perhaps using a set definition which doesn't use DESCENDANTS() will help perfromance. You might try a set definition which refers to all the members at the leaf level with the correct level reference such as:

[Purchase Date].[Fiscal Hierarchy].<leaf level name>.Members

Hope this helps - PaulG

|||

That worked great.
The combination of using * instead of nonemptycrossjoin
and avoiding descendants works well and query returns results very quick.

There is one problem though.....on running the query for
another measure (amounts), it gives the following error.

"The expression contains a function that cannot operate on a set with more than 4,294,967,296 tuples.
Is there a known limitation with sum()."

The first measure was the row count measure.

Regards

|||

Hmm - this error was mentioned before in the forum - don't know its current status. But you can try replacing * with nested Sum(), like:

>>

With

Member [Measures].[CertainDate] as "July 1, 2002"

Member [Measures].[SalesTest] as

Sum({NULL:StrToMember("[Date].[Fiscal].[Date].["

+ [Measures].[CertainDate] + "]").PrevMember},

Sum({StrToMember("[Ship Date].[Fiscal].[Date].["

+ [Measures].[CertainDate] + "]"):NULL},

[Measures].[Sales Amount])),

FORMAT_STRING = 'Currency'

select {[Measures].[CertainDate], [Measures].[SalesTest]} on 0

from [Adventure Works]

>>

Wednesday, March 7, 2012

NOLOCK or Not to NOLOCK

I need some help to under stand when the right time is for NOLOCK. I work in a small dev group and NOLOCK seams to be a buzz word and others are throwing it in all over for no apparent reason.

I read the thing from http://www.sql-server-performance.com/ and I am sure that our web and SQL servers are about 100x over sized for the application. While are ASP.Net (VB) app may demonstrate some hesitation from time to time I am more inclined to blame poor VB.Net coding techniques before slow SQL. The point being the NOLOCK is being added to SELECTS that are not part of a transaction and were using the SQL data adapter to return datasets or single column values.

Also I am not even sure it's being used correctly. The OLM has the example:
SELECT au_lname FROM authors WITH (NOLOCK)

However I am seeing it formatted like this:
SELECT au_lname FROM authors (NOLOCK)

I am by no mean an expert, I follow what I read in books or from examples from others. And I have never read in a book go crazy with NOLOCK because it's the bomb!

Any thoughts? I am trying to learn as much as I can before I raise my hand and say this might be a bad idea.

Thankshttp://www.sql-server-performance.com/lock_contention_tamed_article.asp|||This is the article that I read.
I guess I need to learn if I am having a lock contention problem? I have been using the Enterprise manager and watching for locks and there does not appear to be any issues. I am also trying a demo of "Spotlight on SQL" and it shows just a few database level shared locks and no blocking locks.

I am guessing I would see blocking locks if there was contention which would warrant the use of the NOLOCK correct?|||In general, I use NOLOCK when I have a problem, and generally, I see the problem when the front end is MS Access, because of the way folks tend to use Access (binding to the entire table).|||To be honest, NOLOCK used to be very helpful when you were stuck with COM+'s serialized isolation level. My biggest problem with table hints is that they are just that, hints, not instructions. You can write some lovely SQL statement that works for days, months, years. Then all of sudden it stops working, why? Because SQL has decided that it no longer wants to take notice of your hint.

So I'd say, like has already been said, use them when you *need* to use them. Normally sorting out your isolation levels will solve most the of the problems.