Jason Mauss' Blog Cabin

Because someone's got to do the dirty work

Blog-Flair

Blogroll

Links

What's wrong with this T-SQL? #1

In an effort to increase my T-SQL skills, and possibly help others learn T-SQL better, I'm kicking off a series of posts today that will show a T-SQL batch that has a problem with it, and after people have had ample time to try to solve the problem, I will post the answer.

For today's T-SQL problem, you have a table named 'Positions' that has two fields. 'Id' which is an int and is the primary key and identity field, and 'PositionValue' which is also a standard int field. A procedure to update a specific value for a PositionValue field (based on the Id value passed in) is shown below:

CREATE PROCEDURE UpdatePosition
(@PositionValue int,
 @PositionId int
)
AS
SET NOCOUNT ON
GO

UPDATE
      Positions
SET
      PositionValue = @PositionValue
WHERE
      Id = @PositionId
GO

Can you spot the problem?

Comments

kenny said:

vars out of scope after GO
# August 27, 2004 1:38 PM

Darron said:

What if the row with Id = @PositionId doesn't exist....

you won't get an exception, just a rowcount of zero rows affected.
# August 27, 2004 1:39 PM

Jason Mauss said:

kenny got the real issue - which will keep it from compiling.

'GO' separates batches so, after

SET NOCOUNT ON
GO

SQL Server interprets the rest as a separate batch, with @PositionValue and @PositionId not being declared anywhere.
# August 27, 2004 1:44 PM

Darron said:

Yeah, I saw that just after I posted. Too bad you can't edit a comment.
# August 27, 2004 1:54 PM

Alex Papadimoulis said:

Your primary key isn't named PK_Col_Int_Identity_Id!
# August 28, 2004 8:55 PM

Bowman said:

Excuse me. The information found by me on this site appeared very useful. Thanks to the owner. Help me! Need information about: Share trading basics. I found only this - <a href="leadership.nlada.org/.../ShareTrading">australian online share trading</a>. A beautiful market of discrepancy would occur all the nse fallen with company, testing the account securities and share of moving a cycle and halting the broker, share trading. Share trading, the price perceived for other sufferings under the release of his 6 looking crashes. With love ;-), Bowman from Scotland.

# March 24, 2010 4:03 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)