It is possible to put unexpandable code in an \noalign{..} expression. The closing } can be added dynamically using a special trick, allowing a macro to insert code after the \noalign if the test inside it is true.
\noalign is used for material which should not be aligned with the tabular format, i.e. put into cells. All horizontal lines like \hline or \toprule are done using \noalign. You can use these only between rows, i.e. at the beginning of a row, which is in fact the source of your original problem. However, it is possible to have multiple \noaligns after each other.
The following defines a \NoAlignInputIfFileExists{<filename>} which can be used as you intended, but will create an error if not used between tabular rows:
\documentclass{standalone}
\usepackage{booktabs}
\makeatletter
\newcommand*\NoAlignInputIfFileExists[1]{%
\noalign{\ifnum 0=`}\fi
\IfFileExists{#1}
{\ifnum 0=`{\fi }\@@input #1 }%
{\ifnum 0=`{\fi }}%
}
\makeatother
\begin{document}
\begin{tabular}{ll}
% must be used at the beginning of a tabular line, but
% can be mixed with other \noalign commands like \hline etc.
\NoAlignInputIfFileExists{inp}
\end{tabular}
\end{document}
To explain this advanced code a little:
The \noalign boxes/executes its content while reading it. It doesn't read the {..} as an argument, so it doesn't look directly for the closing }.
Here the \noalign{\ifnum 0=`}\fi trick is used. The } is actually removed because the \ifnum is false. However, the \newcommand sees both an opening { and closing } so is happy to have a matching number of them. The actual closing } for the \noalign exists twice: once for in each if/else-clause. An \ifnum 0=`{\fi } is used to remove the { during the execution of \noalign, which is again needed to be present to make the \newcommand definition happy.
\noalign\bgroup, and\egroup, rather than the more elaborate\ifnum0=\}\fi`. – Bruno Le Floch Apr 05 '12 at 10:18\ifnum0=`}\fithen used again in\hlineetc.? I think I knew it once, but it eludes me now. – Martin Scharrer Apr 05 '12 at 10:39\hline\hline. – egreg Apr 05 '12 at 10:53\hline\hlinerequires non-expandable lookahead, but this can be done with\noalign\bgroup...closed by\egroupin\@xhline. The only case where this lookahead is risky is\hline &: with the\bgroupversion, the\futureletpicks up\outer endtemplate:, which by chance doesn't break anything. When using the\ifnumtrick, TeX's master counter is non-zero when futureletting&, hence we get&, not the end-template command. This is safer (but that safety happens to be useless here). – Bruno Le Floch Apr 05 '12 at 12:02