• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Help with SQL Query

I have a table EXAMINATION_QUESTIONS and a table EXAMINATION_SCHEDULER.

I wish to populate table SCHEDULER with records from a table EXAMINATION_QUESTIONS Using a conditional query.

Table EXAMINATION_QUESTIONS  needs to be populated with 2 extra column which are not present in table which is giving me the problem.

I have enclosed a query which i build and i am sure that there is some syntax in the query i am using.

ID,

CREATE PROCEDURE [dbo].[ExamMode_SetupExamQuestionforTestTaker]
(
	@REF_CODE nvarchar(50),
	@PROCESS_FLAG_ID int,
	@REQUESTOR_ID int,
	@TEST_TAKER_ID int
)
AS
SET NOCOUNT ON;
 
 
INSERT INTO EXAMINATION_SCHEDULER
(
	REQUESTOR_ID,
	EXAM_REFERENCE_CODE,
	TEST_TAKER_ID,
	QUESTION_ID,
	CORRECT_ANSWER_ID,
	MARK_ALLOCATED,
	CREATE_DATE
)
 
VALUES
(
	@REQUESTOR_ID,
	@TEST_TAKER_ID,
	SELECT
		EXAM_REFERENCE_CODE,
		QUESTION_ID,
		CORRECT_ANSWER_ID,
		MARK_ALLOCATED,
		CREATE_DATE
	FROM
		EXAMINATION_QUESTIONS
	WHERE 
		EXAM_REFERENCE_CODE=@REF_CODE  
)

Open in new window

0
TECH_NET
Asked:
TECH_NET
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
Hello TECH_NET,

CREATE PROCEDURE [dbo].[ExamMode_SetupExamQuestionforTestTaker]
(
      @REF_CODE nvarchar(50),
      @PROCESS_FLAG_ID int,
      @REQUESTOR_ID int,
      @TEST_TAKER_ID int
)
AS
SET NOCOUNT ON;
 
 
INSERT INTO EXAMINATION_SCHEDULER
(
      REQUESTOR_ID,
      EXAM_REFERENCE_CODE,
      TEST_TAKER_ID,
      QUESTION_ID,
      CORRECT_ANSWER_ID,
      MARK_ALLOCATED,
      CREATE_DATE
)
 
      SELECT    @REQUESTOR_ID,
                  @TEST_TAKER_ID,
                  EXAM_REFERENCE_CODE,
                  QUESTION_ID,
                  CORRECT_ANSWER_ID,
                  MARK_ALLOCATED,
                  CREATE_DATE
      FROM
            EXAMINATION_QUESTIONS
      WHERE
            EXAM_REFERENCE_CODE=@REF_CODE  

GO


Regards,

Aneesh
0
 
BrandonGalderisiCommented:
Here you go.
CREATE PROCEDURE [dbo].[ExamMode_SetupExamQuestionforTestTaker]
(
        @REF_CODE nvarchar(50),
        @PROCESS_FLAG_ID int,
        @REQUESTOR_ID int,
        @TEST_TAKER_ID int
)
AS
SET NOCOUNT ON;
 
 
INSERT INTO EXAMINATION_SCHEDULER
(
        REQUESTOR_ID,
        EXAM_REFERENCE_CODE,
        TEST_TAKER_ID,
        QUESTION_ID,
        CORRECT_ANSWER_ID,
        MARK_ALLOCATED,
        CREATE_DATE
)
 
        SELECT
        @REQUESTOR_ID,
        @TEST_TAKER_ID,
                EXAM_REFERENCE_CODE,
                QUESTION_ID,
                CORRECT_ANSWER_ID,
                MARK_ALLOCATED,
                CREATE_DATE
        FROM
                EXAMINATION_QUESTIONS
        WHERE 
                EXAM_REFERENCE_CODE=@REF_CODE  

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
close:
CREATE PROCEDURE [dbo].[ExamMode_SetupExamQuestionforTestTaker]
(
      @REF_CODE nvarchar(50),
      @PROCESS_FLAG_ID int,
      @REQUESTOR_ID int,
      @TEST_TAKER_ID int
)
AS
SET NOCOUNT ON;
 
 
INSERT INTO EXAMINATION_SCHEDULER
(
      REQUESTOR_ID,
      EXAM_REFERENCE_CODE,
      TEST_TAKER_ID,
      QUESTION_ID,
      CORRECT_ANSWER_ID,
      MARK_ALLOCATED,
      CREATE_DATE
)
 
SELECT
      @REQUESTOR_ID,
      @TEST_TAKER_ID,
            EXAM_REFERENCE_CODE,
            QUESTION_ID,
            CORRECT_ANSWER_ID,
            MARK_ALLOCATED,
            CREATE_DATE
      FROM
            EXAMINATION_QUESTIONS
      WHERE 
            EXAM_REFERENCE_CODE=@REF_CODE  
)

Open in new window

0
 
TECH_NETAuthor Commented:
I have a follow up question.

I get the following error when i execute the stored procedure from my .net program.

The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

I had to insert a COMMIT statement at the end of the sql query in the stored procedure to ensure the records are commited.

It is strange that the without the commit statement i do not see the inserted records although the identity column in being incremented.

Any thoughts why the above error is occuring.
0
 
TECH_NETAuthor Commented:
I had it fixed. I forgot a Begin transaction statement
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now