Stocks ended barely changed thanks to a late comebackTuesday as investors weighed a strong surge in construction and manufacturing activities against a spike in crude oil prices to a record high.

The Dow Jones industrial average (search) closed up 14.2 points, or 0.14 percent, at 10,202.65. The Standard & Poor's 500 Index (search) gained 0.56 point, or 0.05 percent, to 1,121.24. . The technology-focused Nasdaq Composite Index (search) rose 4.03 points, or 0.2 percent, to 1,990.77.

ExxonMobil Corp. (XOM) and other oil producers gained, but Southwest Airlines (LUV) and United Parcel Service Inc. (UPS), which are major fuel consumers, fell.

Pharmaceutical companies also jumped, with Merck & Co. (MRK) leading the blue-chip Dow higher and Baxter International Inc. providing a boost to the Standard & Poor's.

Viacom Inc. (VIA.B) shares dipped as the media conglomerate said No. 2 executive Mel Karmazin (search) will step down, ending several years of tension with Chief Executive Sumner Redstone (search).

The indexes spent most of the day lower as investors worried that soaring oil prices would raise costs for companies and consumers. Strong factory and construction data early in the day briefly lifted stocks before rising oil prices sent them lower.

But optimism came back late in the day, said Peter Cardillo, chief market analyst at SW Bach and Co.

"The market wants to move higher based on good fundamentals, but obviously it is subject from time to time to the fear factor, which is terrorism and oil prices."

An attack on foreign oil workers in Saudi Arabia, which left 22 people dead, sent benchmark oil prices settling up $2.45, or 6 percent, at $42.33 a barrel, setting a 21-year record on the New York Mercantile Exchange (search). The jump in turn raised stock investors' worries about the impact of rising energy prices on inflation and the Federal Reserve's interest rate policy.

"It's all about oil right now because of the terror attack in Saudi Arabia," said Todd Leone, managing director of equity trading at SG Cowen Securities. "Actually, though, the market's hung in there pretty well. I think that the attack happened there instead of here helped ease some fears."

However, a higher-than-expected rise in construction spending and strong growth in the manufacturing sector showed that the potential for higher rates would not harm two of the major engines of the economy — a major concern of investors. Those bullish figures kept stocks from plunging and let buyers re-enter the market late in the session.

An Institute for Supply Management (search) survey showed U.S. manufacturing grew faster than expected in May, while Commerce Department data showed the monthly increase in construction spending beat Wall Street's estimates.

Some investors also were waiting on the sidelines until Friday's payroll report for May from the Labor Department, which will show the number of new jobs added by the economy for that month.

"The jobs report that's due out this week is going to be pretty big for the market, I'm sure," said Joseph Kalinowski, director of research at Puglisi and Co. "We're just sort of waiting for the end of the week here."

ExxonMobil shares rose 45 cents, or about 1 percent, to $43.70.

Merck shares rose $1.06, or 2.2 percent, to $48.36, making it the biggest percentage gainer on the Dow. Baxter added 28 cents to $31.72.

Viacom fell 46 cents to $36.81 after president and chief operating officer Mel Karmazin unexpectedly resigned. MTV chief Tom Freston and CBS executive Leslie Moonves will share the company's No. 2 post.

Altria Group (MO)  buoyed the Dow after a Barron's report suggested tobacco companies could be a good investments after being beaten down recently following recent adverse court rulings. Altria shares rose 58 cents, or 1.2 percent, to $48.55.

Medtronic Inc. (MDT) and Genzyme Corp. (GENZ) have formed a joint venture to develop new drugs for heart disease, including treatments to repair damaged heart tissue. Medtronic climbed 95 cents to $48.85, while Genzyme was down 11 cents at $43.74.

Boston Scientific Corp. (BSX) gained $1.30 to $45.60 after its announced the acquisition of privately held Advanced Bionics Corp. for $740 million, giving the company a stronger presence in implanted microelectronic devices.

Labor Ready Inc. (LRW) surged $1.68 to $13.85 after raising its second-quarter earnings estimates. The temporary labor agency said it had better-than-expected sales in April and May.

Oracle Corp. (ORCL) fell 28 cents to $11.12 after a downgrade from Prudential, from overweight to neutral.

The Russell 2000 index of smaller companies was up 4.23, or 0.7 percent, at 572.51.

Overseas, Japan's Nikkei stock average rose 0.5 percent. In afternoon trading, Britain's FTSE 100 closed down 0.2 percent, France's CAC-40 was off 1.2 percent for the session and Germany's DAX index dropped 1.5 percent.

Reuters and the Associated Press contributed to this report.