Further to my comments above...
In one sense you have already created a temporary table - a table variable scope, like a temporary table, is within the stored procedure (or batch). You cannot use it outside of that scope - just like a temporary table cannot be used outside of its scope.
To create a temporary table instead of a table variable use
CREATE TABLE #temp_departments
instead of
DECLARE @temp_departments TABLE
and use
#
(or
##
) instead of
@
in the table name in the rest of your code.
However, the content of your temporary table is just a list of the column names
Developing Team Testing UI
Developing Team Testing UI
So your question is more likely to be ... how do I do a pivot?
Try changing your query to something like
INSERT INTO @temp_departments
SELECT *
FROM (
SELECT Department_Name, aValue
FROM Department
) AS SourceTable
PIVOT (
SUM(aValue)
FOR Department_Name IN ([Developing Team], [Testing], [UI])
) AS PivotTable;
aValue
is some other value in your
Department
table.
If this, or the comments I've already posted, do not help you solve your problem, post another question that includes sample data and your desired outcome (as well as the code). See
CodeProject Quick Answers FAQ[
^] for further information.