Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
need to create temporary table from department table using stored procedure



What I have tried:

USE [EmployeeManagementSystem]
GO
/****** Object:  StoredProcedure [dbo].[TempDepartments]    Script Date: 21-02-2024 10:22:36 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TempDepartments]
AS
BEGIN
    -- Declare a table variable
    DECLARE @temp_departments TABLE (
        [Developing Team] VARCHAR(50),
        [Testing] VARCHAR(50),
        [UI] VARCHAR(50)
    );

    -- Insert data into the table variable
    INSERT INTO @temp_departments
    SELECT *
    FROM (
        SELECT Department_Name
        FROM Department
    ) AS SourceTable
    PIVOT (
        MAX(Department_Name)
        FOR Department_Name IN ([Developing Team], [Testing], [UI])
    ) AS PivotTable;

    -- Select the data from the table variable (for viewing purposes)
   SELECT * FROM @temp_departments;
END;
Posted
Comments
CHill60 21-Feb-24 3:14am    
So what is the problem?
OriginalGriff 21-Feb-24 3:19am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
CHill60 21-Feb-24 3:49am    
See the MS documentation CREATE TABLE (Transact-SQL) - SQL Server | Microsoft Learn[^], particularly the section on Temporary Tables[^].Quote:
A local temporary table created in a stored procedure is dropped automatically when the stored procedure is finished. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table can't be referenced by the process that called the stored procedure that created the table.
0x01AA 21-Feb-24 12:16pm    
And how one can upvote that comment? ;)
CHill60 22-Feb-24 4:24am    
Thanks for the thought :-)

1 solution

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
SQL
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900