Stocks rose for a second day on Tuesday as optimism grew that a solution to the Greek debt problem was near, boosting investor appetite for risky assets as equities recouped some of their recent losses.

Buyers snapped up shares after the S&P 500's 7 percent swoon since April, mostly in commodities and technology shares, as investors sought to raise their exposure going into the end of the quarter and before earnings season next month.

The greater risk to the market is that the news is not negative and we rally on good earnings, said Marc Pado, U.S. market strategist at Cantor Fitzgerald & Co in San Francisco.

The more the market pushes higher, the more it makes the people who are under-exposed nervous, he said.

There was a strong move into riskier cyclical areas of the market such as energy, retail and materials. Those sectors, which are more sensitive to shifts in the economy, have underperformed the market so far this year.

The S&P energy index <.GSPE> surged nearly 2.5 percent, the biggest gainer among S&P sectors. Halliburton Co gained 5.9 percent to $48.94, while Chevron Corp was up 1.4 percent at $100.30.

In an advance indication of earnings season, Nike Inc surged 10.1 percent to $89.80 a day after reporting fourth-quarter earnings that beat expectations, while orders suggested robust strength for the future.

The Dow Jones industrial average <.DJI> gained 139.49 points, or 1.16 percent, to 12,183.05. The Standard & Poor's 500 Index <.SPX> rose 15.20 points, or 1.19 percent, to 1,295.30. The Nasdaq Composite Index <.IXIC> added 37.82 points, or 1.41 percent, to 2,726.10.

Greek lawmakers will vote Wednesday and Thursday on the measures, which must be passed to receive the next payment of

12 billion euros. If Greece doesn't get the funds, investors fear a Europe-wide crisis and potential credit market freeze could follow.

Also helping sentiment, progress was reported in talks to persuade European banks and insurers to voluntarily roll over maturing Greek debt.

While the market was generally optimistic about the vote, the CBOE Volatility Index <.VIX>, Wall Street's fear gauge, suggested some caution. The index stood at 19.46, a number considered relatively high.

(Reporting by Edward Krudy; Editing by Kenneth Barry)