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 7188923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:11:31+00:00 2026-05-28T19:11:31+00:00

I am writing some sql, using sql server 2008, to loop around some records.

  • 0

I am writing some sql, using sql server 2008, to loop around some records. Within this loop I need to insert a row and the ID of that row will be put into a field in an existing record in a different table. I thought I would be able to achieve this with OUTPUT but I couldn’t find an example of exactly what I wanted to do.

Here’s what I have so far:

DECLARE @courseTempID INT
DECLARE @courseID INT
DECLARE @academicYearID INT
DECLARE @courseCode VARCHAR(10)
DECLARE @uio_id INT
DECLARE @creatorIntranetUserID INT

WHILE (SELECT count(*) FROM CoursesTemp WHERE dmprocessed = 0) > 0
BEGIN
    SELECT TOP 1 @id = courseTempID FROM CoursesTemp WHERE dmprocessed = 0
    SELECT TOP 1 @academicYearID = academicYearID FROM CoursesTemp WHERE dmprocessed = 0
    SELECT TOP 1 @courseCode = courseCode FROM CoursesTemp WHERE dmprocessed = 0
    SELECT TOP 1 @uio_id = uio_id FROM CoursesTemp WHERE dmprocessed = 0
    SELECT TOP 1 @creatorIntranetUserID = creatorIntranetUserID FROM CoursesTemp WHERE dmprocessed = 0

    INSERT INTO dbo.Courses VALUES(3,@academicYearID,1,@courseCode,@uio_id,GETDATE(),@creatorIntranetUserID)

    OUTPUT INSERTED.courseID INTO @courseID 

    UPDATE CoursesTemp SET dmprocessed = 1, courseID = @courseID WHERE courseTempID = @courseTempID

END

The error I am getting is “Incorrect syntax near ‘INSERTED'”.

Can anyone help me work out how to use OUTPUT in this way please?

  • 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-05-28T19:11:32+00:00Added an answer on May 28, 2026 at 7:11 pm

    A few performance improvements applied. It is safer to use SCOPE_IDENTITY()

    DECLARE @courseTempID INT
    DECLARE @courseID INT
    DECLARE @academicYearID INT
    DECLARE @courseCode VARCHAR(10)
    DECLARE @uio_id INT
    DECLARE @creatorIntranetUserID INT
    DECLARE @id INT
    
    WHILE EXISTS(SELECT 1 FROM CoursesTemp WHERE dmprocessed = 0)
    BEGIN
        SELECT TOP 1 @id = courseTempID 
            ,@academicYearID = academicYearID
            ,@courseCode = courseCode
            ,@uio_id = uio_id
            ,@creatorIntranetUserID = creatorIntranetUserID 
        FROM CoursesTemp WHERE dmprocessed = 0
    
        INSERT INTO dbo.Courses VALUES(3,@academicYearID,1,@courseCode,@uio_id,GETDATE(),@creatorIntranetUserID)
        UPDATE CoursesTemp SET dmprocessed = 1, courseID = SCOPE_IDENTITY() WHERE courseTempID = @courseTempID
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing a query within ms sql server 2005 and having some trouble getting
I'm writing some custom .Net extensions for SQL Server 2008. One of them is
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
I'm writing some data acquisition software and need a gui plotting library that is
So I was writing some code today that basically looks like this: string returnString
I'm using SqlBulkCopy to insert/update from a .net DataTable object to a SQL Server
I am writing an addin into Sql Server Management Studio, using the Visual Studio
I'm using Microsoft SQL Server 2008. I'm not an expert with it but I
I am writing this code to protect my server from SQL injection. The goal
I'm about to have to rewrite some rather old code using SQL Server's BULK

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.