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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:59:39+00:00 2026-05-13T21:59:39+00:00

I have written some code to ensure that items on an order are all

  • 0

I have written some code to ensure that items on an order are all numbered (the “position number” or “item number” has been introduced only recently and we did not want to go and change all related code – as it is “asthetics only” and has no functional impact.)

So, the idea is to go and check for an records that jave an itemno of NULL or 0 – and then compute one and assign it. When executing this code in a query window, it works fine. When putting it into an AFTER INSERT-trigger, it loops forever.

So what is wrong here?

/****** Objekt:  Trigger [SetzePosNr]    Skriptdatum: 02/28/2010 20:06:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [SetzePosNr]
ON [dbo].[bestellpos]
   AFTER INSERT 
  AS
BEGIN
   DECLARE @idb int
   DECLARE @idp int
   DECLARE @pnr int

   SELECT @idp=id,@idb=id_bestellungen FROM bestellpos WHERE posnr IS NULL OR posnr=0

   WHILE @idp IS NOT NULL
   BEGIN   
      SELECT @pnr = 1+max(posnr) FROM bestellpos WHERE id_bestellungen = @idb
print( 'idp=' + str(@idp) + ', idb=' + str(@idb) + ', posnr=' + str(@pnr))
      UPDATE bestellpos SET posnr=@pnr WHERE id=@idp
      SELECT @idp=id,@idb=id_bestellungen FROM bestellpos WHERE posnr IS NULL OR posnr=0
   END
 END
  • 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-13T21:59:39+00:00Added an answer on May 13, 2026 at 9:59 pm

    Aaaargh – just found the problem: the 3d line (from the end, the SELECT-statement) does not assign the variables when there result of the WHERE is NULL. So “set @idp=null” before that statement has fixed the problem!
    Not sure whhy I assumed a problem between direct execution and triggering of these statements, seems I picked the wrong test-cases 🙁

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

Sidebar

Related Questions

I have a simple screensaver that I've written, which has been deployed to all
I have written some code in java that will guess a number based on
I have written some code that loads an XML document using an XmlDocument object
I have written some WebGL code, actually I am playing with the examples that
I have an application written in C# that invokes some C code as well.
I have written some JavaScript and jQuery code that accepts only numeric input in
I have written some code that provides statistical information about visitors to a website
I have written some code there are 27 different functions that execute like this..
I have written some code that works pretty well, but I have a strange
I have written some code that looks more or less like this: QVector<QRgb> colorTable(256);

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.