I've built a SQL Server table from my C# code by taking the ColumnNames
from a DataTable
as it's derived from an Excel file I import.
This works fine; now I'd like to use SQL table I created in a separate stored procedure without using dynamic SQL because the query is fairly large and it's going to be a pain to maintain if I go that route.
I can't specify the columns in the stored procedure if I create a Table
parameter because it's not a static set of columns, which is why I had to build the table dynamically from my C# code.
So I'm essentially looking to do something like this:
CREATE PROCEDURE usp_SprocName
@TableName VARCHAR(50)
AS
SELECT Something FROM Somewhere R
LEFT JOIN
(
SELECT
*
FROM @TableName HR
WHERE Field = Whatever
) HR ON R.Field= HR.Field
However, I get the error to declare my @Table
variable in my subquery.
Is there some way to get around this without using dynamic SQL?