According to a 2012 Supreme Court decision, Mr. Markowitz said, Congress is not permitted to set conditions on spending to coerce states or localities to participate in a federal program against their will. Any conditions, at a minimum, must be directly related to the punitive action.

In other words, Mr. Markowitz said: “You can’t say ‘if you don’t use your police officers to go after unauthorized immigrants, you don’t get any money for your hospitals.’ They can’t impose conditions that are totally unrelated.”

According to the Immigrant Legal Resource Center, a national group, at least 39 cities and 364 counties nationwide count themselves as sanctuary jurisdictions, but they vary in how limited they are in restricting cooperation with immigration enforcement.

California has been one of the leaders in offering sanctuary, and reaction there was strong.

An hour after the president signed the order, a group of California state senators said they planned to fight it in court, with help from Eric H. Holder Jr., former attorney general, whom the Legislature has retained to take on what the senators said they expected would be continued fights with the Trump administration. They said Mr. Trump’s order violated the 10th Amendment by forcing local governments to enforce federal statutes.

Mayor Sam Liccardo of San Jose, who counts his city among the ranks of sanctuaries without using the term, said that, on matters of public safety, ”our police chief is the best person to decide how to use the scarce resources we have.”

“It’s not simply an ideological decision,” he said. “Our Police Department, like most, doesn’t engage in federal tax laws, federal environmental laws or federal immigration laws.”