Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:40:48+00:00 2026-06-12T18:40:48+00:00

ALTER PROCEDURE [dbo].[p_ReturnEmployeeOrg] ( @BusinessEntityID int = 21 ) **– NOT WORKING** AS SELECT

  • 0
ALTER PROCEDURE [dbo].[p_ReturnEmployeeOrg]
( @BusinessEntityID int = 21 ) **-- NOT WORKING**
AS
SELECT * FROM HumanResources.Employee
;WITH cte_Emp (EmployeeID,ManagerID,Title,EmployeeName,ManagerName,EmployeeLevel)
AS
-- Anchor member definition
(SELECT e.BusinessEntityID,e.MaritalStatus,e.JobTitle,
        CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
        CAST ('N/A' AS VARCHAR(200)),
        CAST (0 AS INT)
    FROM HumanResources.Employee e 
    INNER JOIN Person.Person c
        ON e.BusinessEntityID = c.BusinessEntityID
        WHERE e.BusinessEntityID = @BusinessEntityID

 UNION ALL
-- Recursive member definition 
SELECT  e.BusinessEntityID,e.MaritalStatus,e.JobTitle, 
                CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
                CAST ('N/A' AS VARCHAR(200)),
                CAST (0 AS INT)
        FROM HumanResources.Employee e
        INNER JOIN Person.Person c
        ON e.BusinessEntityID = c.BusinessEntityID)
        ORDER BY EmployeeLevel - **NOT WORKING**

       EXEC p_ReturnEmployeeOrg 21) 

— exec is NOT WORKING, the results are raw data instead of BusinessEntityID = 21

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:40:49+00:00Added an answer on June 12, 2026 at 6:40 pm

    Did you mean to place the first select statement at the end of the stored procedure? There are three pertinent issues to your stored procedure. Firstly, the CTE’s defined but not used in the stored procedure (this is the reason why your sp is returning everything from the employee table). Secondly your exec statement actually cuts through the middle of the CTE definition (I think this might have been formatting error during your posting). The order by statement in the CTE is probably going to cause an error. It’s best to move it out of the CTE.

    ALTER PROCEDURE [dbo].[p_ReturnEmployeeOrg]
    ( @BusinessEntityID int = 21 ) 
    AS
    
    ;WITH cte_Emp (EmployeeID,ManagerID,Title,EmployeeName,ManagerName,EmployeeLevel)
    AS
    -- Anchor member definition
    (SELECT e.BusinessEntityID,e.MaritalStatus,e.JobTitle,
            CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
            CAST ('N/A' AS VARCHAR(200)),
            CAST (0 AS INT)
        FROM HumanResources.Employee e 
        INNER JOIN Person.Person c
            ON e.BusinessEntityID = c.BusinessEntityID
            WHERE e.BusinessEntityID = @BusinessEntityID
    
     UNION ALL
    -- Recursive member definition 
    SELECT  e.BusinessEntityID,e.MaritalStatus,e.JobTitle, 
                    CAST(c.LastName + ',' + c.FirstName AS VARCHAR(200)),
                    CAST ('N/A' AS VARCHAR(200)),
                    CAST (0 AS INT)
            FROM HumanResources.Employee e
            INNER JOIN Person.Person c
            ON e.BusinessEntityID = c.BusinessEntityID)
    ) 
    SELECT * FROM cte_emp ORDER BY EmployeeLevel 
    --....................  
    
    EXEC p_ReturnEmployeeOrg 21
    

    I moved the select statement down just after the end of the CTE definition and changed the table name from employee to cte_emp. I alos moved your exec statement out of the CTE defintion.

    However there are other non-pertinent issues with this. As mentioned by RichardTheKiwi, why is it that you have e.maritalstatus in the CTE definition, but ManagerID in the CTE header? Is businessentityid in the CTE definition meant to be the employeeid in the CTE header?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ALTER PROCEDURE [dbo].[GetValues] @FieldName NVARCHAR(50), @FormName NVARCHAR(50), @PoolName NVARCHAR(50) AS SELECT FieldValue FROM [dbo].[Values]
ALTER PROCEDURE [dbo].[HO_GetListOfLeaguesPerLocation] AS BEGIN SELECT DISTINCT LeagueID, (EXEC dbo.HO_GetLeagueNumOfMatches LeagueID) FROM Games END
ALTER PROCEDURE [dbo].[GetTimeSheetsAttendance] @WhereClause varchar AS EXEC ('Select vwEmployeeList.ID,vwEmployeeList.Code, tbGNClient.FName + '' '' +
I've created the following stored procedure: ALTER PROCEDURE [dbo].[ExampleSP] ( @SearchText NVARCHAR(4000), @ID INT
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
ALTER PROCEDURE dbo.StoredProcedure8 @emp_code bigint, @co_id bigint, @p decimal(8,2) output AS SELECT @p =
ALTER PROCEDURE [dbo].[NST_InsertTblGenLedDet] @GHDHeader int, @Gldtype text, @GldAccount text, @GldDate DateTime, @GldVoucherType int, @GldDebit
I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE
From answers : ALTER PROCEDURE [dbo].[HardQ3] (@ID_User nvarchar(15), @ID_ListGroupParIzm Integer,@CIzmer Integer, @CPoint Integer, @UserExt
--Stored procedure ALTER PROCEDURE [dbo].[Test] @USERID varchar(25) AS BEGIN SET NOCOUNT ON IF NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.