SANTA CLARA — Trey Lance is off the 49ers, and onto the rival Dallas Cowboys.

Lance, drafted No. 3 overall in 2021 after a massive trade up to that perch, got dealt Friday for a fourth-round draft pick, a team source confirmed, less than three hours before kickoff of the 49ers’ preseason finale.

Lance, another source said, took the news better than Wednesday’s, when he was devasted to learn from coach Kyle Shanahan that Sam Darnold would be the top backup this season to Brock Purdy.

The 49ers’ trading partner comes as a surprise on multiple levels, including that the Cowboys outbid the Buffalo Bills for Lance, who was at Levi’s Stadium on Friday and met with Shanahan, general manager John Lynch and CEO Jed York after the trade, a source exclusively told this news organization.

The 49ers can only hope this trade does not backfire the way an August 1992 deal did with the Cowboys, in which Charles Haley was sent to Dallas (for 1993 second- and 1994 third-round picks) to help buoy the Cowboys’ Super Bowl runs. (Disclaimer: Wheras Lance was beloved by coaches other than from a depth-point standpoint, Haley’s volatility toward then-coach George Seifert necessitated his exit.)

Lance leaves to back up Dak Prescott, the entrenched starter of a Cowboys franchise that got eliminated from the playoffs the past two seasons by the 49ers. Cooper Rush was poised to serve as Prescott’s backup.

Lance’s trade, as first reported by ESPN’s Adam Schefter, is the first of a 49ers’ quarterback since Alex Smith went to the Kansas City Chiefs in 2013 for a pair of second-round picks.

Of the 13 players traded away by this 49ers’ regime since 2017, the only one that fetched more in overall return was a 2020 deal of DeForest Buckner to the Indianapolis Colts for a first-round pick.

The 49ers selected Lance a month after a whopping trade with the Miami Dolphins to move up from the No. 12 spot. The 49ers surrendered first-round draft picks in 2021, ’22 and ’23, along with a 2022 third-rounder to climb up to the No. 3 spot.

Lance made just four starts with the 49ers, two in each of his two seasons — losing his October 2021 debut in Arizona, winning at home in December 2021 against Tennessee, then losing last season’s water-logged opener in Chicago before a fractured ankle forced him out of a home-opening win over Seattle.

Although Lance made a full recovery from that ankle fracture after two surgeries, his 49ers tenure was also hindered by a fractured finger in the 2021 preseason finale, and a sprained knee in that 14-carry, 15-completion starting debut in Arizona. Lance’s guaranteed rookie contract is now property of the Cowboys, including $940,000 salary this season and $5.3 million next year, with the ability to exercise a fifth-year option in 2025.

Lance was excused from Wednesday’s practice, as general manager John Lynch described Lance as “devastated” upon hearing the news he didn’t win the No. 2 job. Lance, however, did attend Thursday’s walk-through practice.

“When you put your heart and soul into something, and it doesn’t to fruition you’re going to be devastated. That’s the kind of competitor he is,” Lynch said on KNBR 680-AM on Thursday. “… I talked with Kyle every single day, multiple times a day. I know how excited he was about Trey’s development. I think the good news is he’s excited at what Sam can be.”

Lance will count $8.4 million on the 49ers’ salary cap this season (rather than $10.8 million), as well as $5.5 million next season, according to a front-office source. Lance earned $28.8 million from his 49ers’ tenure, according to The Associated Press.

Statistically, in his four starts and four other games with cameos, Lance completed 56-of-102 passes for 797 yards with five touchdowns and two interceptions, and he ran 54 times for 235 yards and a touchdown, with his final carry resulting in his fractured right ankle in last September’s home opener at Levi’s Stadium.

Eight games marked the fewest played by a top-five pick with his original team since the common-draft era began in 1967, according to the AP.

Lance played just one full season at North Dakota State (and the Bison won the 2019  Football Championship Subdivision), then after just one game in a COVID-impacted 2021 season, he jumped to the pros.

This preseason, Lance struggled as the 49ers’ starter in their opener at Las Vegas (10-of-15, 112 yards, one touchdown), and he ralled the 49ers as the third quarterback into last Saturday’s game against the Denver Broncos, against whom he engineered a fourth-quarter comeback win (12-of-18, 173 yards, one touchdown, one interception).

Darnold was the 2018 draft’s No. 3 overall pick, by the New York Jets. After three seasons there, Darnold spent the past two with the Carolina Panthers before signing a one-year, $4.5 million deal with the 49ers when free agency opened in March.

While Lance and Darnold jockeyed for the No. 2 job through training camp, Purdy proved healthy after a March 10 surgery to repair a torn elbow ligament that occurred in the NFC Championship Game loss at Philadelphia.

Brandon Allen figures now to stick as the 49ers’ No. 3 quarterback, rather than risk being short-handed once again. Allen was a 2016 fourth-round draft pick of the Jacksonville Jaguars, and he’s since served as a backup with the Denver Broncos (2019) and the Cincinnati Bengals (2020-22), appearing in 15 games. Allen was the No. 4 quarterback all camp with the 49ers.

Lance’s time with the 49ers matched the length of Jim Plunkett’s tenure, which is notable in that Plunkett also cost a fortune in a trade as the New England Patriots acquired two 1976 first-round picks and 1977 first- and second-round picks.

The only other 49ers’ trade that paid off as poorly was for a 1978 swap with the Buffalo Bills for O.J. Simpson (1978 second- and third-round picks; 1979 first-, second- and fourth-round picks). Simpson’s 49ers career also spanned two, unfulfilling seasons, and that capped his Pro Football Hall of Fame career.

* * *

Quarterbacks traded by 49ers

2013 Alex Smith

To Kansas City, for 2013 second-round and 2014 second-round picks

2010 Shaun Hill

To Detroit, for 2011 seventh-round pick

2006 Cody Pickett

To Houston, for 2007 conditional draft pick

2006 Ken Dorsey

To Cleveland, with 2007 seventh-round pick for Trent Dilfer

2005 Tim Rattay

To Tampa Bay, for 2006 sixth-round pick

1999 Jim Druckenmiller

To Miami, for 2000 seventh-round and 2001 seventh-round picks

1999 Ty Detmer

To Cleveland, with fourth-round pick for fourth- and fifth-round picks

1994 Steve Bono

To Kansas City, for fifth-round draft pick

1993 Joe Montana

To Kansas City, with safety David Whitmore for first-round pick

1981 Steve DeBerg

To Denver, for 1983 fourth-round draft pick

1976 Tom Owens

To New England, with two 1976 first-round picks and 1977 first- and second-round picks for Jim Plunkett.

1961 Y.A. Tittle

To New York Giants, for Lou Cordileone

* * *

Players the 49ers have exported in a trade:

RB Jeff Wilson

2021, to Miami

For 2023 fifth-round pick (CB Darrell Luter Jr.)

LB Jonas Griffith

2021, to Denver

With seventh-round pick, for 2022 sixth-round pick (Nick Zakelj) and 2023 seventh-round pick (dealt to Minnesota to help move up for Ji’Ayir Brown)

LB Kwon Alexander

2020, to New Orleans

For 2021 fifth-round pick (Deommodore Lenoir) and Kiko Alonso

WR Marquise Goodwin

2020, to Philadelphia

With 2020 sixth-rounder and 2021 seventh-rounder for 2020 sixth-rounder (Charlie Woerner)

RB Matt Breida

2020, to Miami

For 2020 fifth-rounder (Colton McKivitz)

DT DeForest Buckner

2020, to Indianapolis

For 2020 first-rounder at No. 13, that was traded with seventh-round pick to Tampa Bay for No. 14 pick (Javon Kinlaw) and fourth-round pick that was dealt to Minnesota in package for No. 25 pick (Brandon Aiyuk)

LB Dekoda Watson

2019, to Denver

With sixth-round pick for fifth-round pick (Dre Greenlaw)

LB Eli Harold

2018, to Detroit

For 2020 seventh-round pick (Jauan Jennings)

OT Trent Brown

2018, to New England

With fifth-round pick for third-round pick (Tarvarius Moore)

C Daniel Kilgore

2018, to Miami

With seventh-round pick for seventh-round pick (Jullian Taylor)

CB Rashard Robinson

2017, to New York Jets

For 2018 fifth-round pick that was traded to New England in package with Trent Brown for third-round pick

TE Vance McDonald

2017, to Pittsburgh

With 2018 fifth-round pick for 2018 fourth-round pick (Kentavius Street)

Source: www.mercurynews.com