Dan Bishop, Republican candidate for North Carolina's 9th District, is interviewed by CQ Roll Call at Robin's On Main diner in Hope Mills, N.C., on Saturday, August 10, 2019.

Republican Dan Bishop defeated Democrat Dan McCready in North Carolina's 9th District in a race that drew national attention and dragged on for months, according to an NBC News projection.

Bishop's victory Tuesday is a bright spot for a Republican House delegation that lost 40 seats in the 2018 midterm elections and is suffering from a recent wave of retirements before the 2020 elections. Still, the fact that the race was competitive may still be troubling for the GOP. The Republican led by about 2 percentage points with nearly all votes counted.

Bishop, a North Carolina state senator, will keep red a seat Republicans have held since 1963. President Donald Trump won the district by 11 points in 2016.

"Under this president, America is great again. We stand for our flag and we kneel for our maker," Bishop said in his victory speech, according to NBC News.

In a tweet Tuesday night, Trump claimed Bishop was "down 17 points 3 weeks ago" before he "asked [the president] for help." Trump said they "changed [Bishop's] strategy together." He did not specify which polls he was citing in the tweet. Both publicly released internal campaign polls and nonpartisan surveys showed a close race leading up to Tuesday.

Trump tweet: Dan Bishop was down 17 points 3 weeks ago. He then asked me for help, we changed his strategy together, and he ran a great race. Big Rally last night. Now it looks like he is going to win. @CNN & @MSNBC are moving their big studio equipment and talent out. Stay tuned!

Republicans threw every weapon they had into the fight. Both Trump and Vice President Mike Pence held rallies in North Carolina Monday night as they hoped to push Bishop over the finish line. Pence spoke at Wingate University, while Trump appeared in Fayetteville, N.C., where he told the crowd, "[Tuesday] is your chance to send a clear message to the America-hating left."

The 9th District garnered national attention after McCready, a Marine Corps Iraq War veteran, lost by a point to GOP pastor Mark Harris in 2018, coming just 905 votes short of victory. However, the state's Board of Elections refused to certify the election results after allegations surfaced that an operative working for Harris' campaign conducted an illegal ballot harvesting campaign.

The controversy led the Board of Elections to schedule a new race, leaving the seat vacant since January. Harris announced he would not run in the new contest, paving the way for Bishop to win the nomination.

McCready ran as a moderate Democrat, not afraid to buck the party when he disagreed on policy, a familiar playbook for Democratic candidates in 2018 running in historically Republican strongholds. He emphasized his commitment to protecting health care, the most important issue for voters in 2018.

Bishop, known for his sponsorship of the H.B. 2 "bathroom bill" that set off a firestorm for what critics saw as an attempt to discriminate against transgender people, nationalized the race. He showed his support for Trump and linked McCready with the more controversial members of the Democratic Party, namely House Speaker Nancy Pelosi and members of the liberal firebrand "Squad." His victory resolves the last contested House race of 2018.

The competitiveness of the race was evident in the nearly $14 million poured into it, as both parties fought to win a seat that could serve as a bellwether for the battleground state heading into the 2020 elections.

McCready's campaign dramatically outspent Bishop's in the race, $4.7 million to $1.9 million. But Republicans' outside spending dwarfed Democrats', $5.8 million to $1.4 million.

Overall outside spending from both parties and PACs exceeded $10.7 million, making this the second-most expensive House special election in U.S. history in terms of independent and expenditures. Only last year's special election race between Jon Ossoff and Karen Handel in Georgia's 6th District ranks higher.

Bishop's win is a calming sign for House Republicans, possibly signalling that they could win suburban districts Trump locked up in 2016, even if by a smaller margin. While special elections do not always reflect the national pulse, Bishop's victory could be an early indicator for the 2020 elections.