Msg 8164, Level 16, State 1, Procedure getSalaryMonth, Line 31 [Batch Start Line 13] An INSERT EXEC statement cannot be nested.

-- Option #3 : Use OPENROWSET
DECLARE @StudentGrades TABLE (
    [StudentID]        INT,
    [StudentName]      VARCHAR(100),
    [Grade]            DECIMAL(4, 1)
)

INSERT INTO @StudentGrades ( [StudentID], [StudentName], [Grade] )
SELECT A.*
FROM OPENROWSET('SQLNCLI', 'Server=.;Database=SQL2008;Uid=sshelper;Pwd=sshelper',
     'EXECUTE [dbo].[usp_GetStudentGrades] 1 ') AS a

SELECT * FROM @StudentGrades

4.67
3
Maxfield 75 points

                                    DECLARE @StudentGrades TABLE (
    [StudentID]          INT,
    [StudentName]        VARCHAR(100),
    [Grade]              DECIMAL(4, 1)
)

INSERT INTO @StudentGrades ( [StudentID], [StudentName], [Grade] )
EXECUTE [dbo].[usp_GetStudentGrades] 1
GO

4.67 (3 Votes)
0
3.88
8

                                    -- Option #2 : Convert Stored Procedure to a Table-Valued Function
CREATE FUNCTION [dbo].[ufn_SearchStudents] (
    @SubjectID			INT
)
RETURNS TABLE 
AS
RETURN (
    SELECT B.[StudentID], B.[StudentName]
    FROM [dbo].[StudentSubject] A INNER JOIN [dbo].[Student] B
                                          ON A.[StudentID] = B.[StudentID]
    WHERE A.[SubjectID] = @SubjectID
)
GO

3.88 (8 Votes)
0
3.67
6
Cassandra 145 points

                                    DECLARE @StudentGrades TABLE (
    [StudentID]        INT,
    [StudentName]      VARCHAR(100),
    [Grade]            DECIMAL(4, 1)
)

INSERT INTO @StudentGrades ( [StudentID], [StudentName], [Grade] )
EXECUTE [dbo].[usp_GetStudentGrades] 1
GO

3.67 (6 Votes)
0
3.5
4
Thraupidae 115 points

                                    -- Option #1 : Integrate First Stored Procedure with the Second Stored Procedure
CREATE PROCEDURE [dbo].[usp_GetStudentGrades]
    @SubjectID			INT
AS

DECLARE @Students TABLE (
    [StudentID]        INT,
    [StudentName]      VARCHAR(50)
)

INSERT INTO @Students ( [StudentID], [StudentName] )
SELECT B.[StudentID], B.[StudentName]
FROM [dbo].[StudentSubject] A INNER JOIN [dbo].[Student] B
                                      ON A.[StudentID] = B.[StudentID]
WHERE A.[SubjectID] = @SubjectID

SELECT A.[StudentID], A.[StudentName], C.[Grade]
FROM @Students A INNER JOIN [dbo].[StudentSubject] B
                         ON A.[StudentID] = B.[StudentID]
                 INNER JOIN [dbo].[StudentGrade] C
                         ON B.[StudentSubjectID] = C.[StudentSubjectID]
GO

3.5 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source