Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2010-12-10 : 07:43:27
|
HI, I want to do this on SQL 2000 (don't ask):CREATE PROCEDURE [dbo].[Category_Add]@CategoryName nvarchar(255),@CategoryStatus bitASDECLARE @CategoryID INTINSERT INTOCategory([CategoryName],[CategoryOrder],[CategoryStatus])VALUES(@CategoryName,IsNull((select max(CategoryOrder) From Category), 0) + 1,@CategoryStatus)SET @CategoryID = SCOPE_IDENTITY()RETURN @CategoryIDGOIt says:Subqueries are not allowed in this context. Only scalar expressions are allowed.So what would be a good solution for thisThe secret to creativity is knowing how to hide your sources. (Einstein) |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-12-10 : 08:06:40
|
Try:CREATE PROCEDURE [dbo].[Category_Add]@CategoryName nvarchar(255),@CategoryStatus bit,@CategoryID INT OUTPUTASBEGINDECLARE @MaxCatOrder INTSelect @MaxCatOrder =Max(CategoryOrder) From CategoryINSERT INTOCategory([CategoryName],[CategoryOrder],[CategoryStatus])VALUES(@CategoryName,isnull(@MaxCatOrder,0) + 1,@CategoryStatus)SET @CategoryID = SCOPE_IDENTITY()ENDGO |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-12-10 : 08:14:59
|
You should only return errors.Use an OUTOUT parameter instead:SET ANSI_NULLS, QUOTED_IDENTIFIER ONGOCREATE PROCEDURE dbo.Category_Add @CategoryName nvarchar(255) ,@CategoryStatus bit ,@CategoryID int OUTPUTASSET NOCOUNT ONINSERT INTO dbo.Category(CategoryName, CategoryOrder, CategoryStatus)SELECT @CategoryName ,COALESCE(MAX(CategoryOrder), 0) + 1 ,@CategoryStatusFROM dbo.Category WITH (UPDLOCK, HOLDLOCK)SET @CategoryID = SCOPE_IDENTITY()GO-- to useDECLARE @CategoryID intEXEC dbo.Category_Add 'NewName', 1, @CategoryID OUTPUTSELECT @CategoryID Edit: Sorry - answer already given. |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-12-10 : 08:21:55
|
quote: INSERT INTO dbo.Category(CategoryName, CategoryOrder, CategoryStatus)SELECT @CategoryName ,COALESCE(MAX(CategoryOrder), 0) + 1 ,@CategoryStatusFROM dbo.Category WITH (UPDLOCK, HOLDLOCK)
When multiple inserts are happening on different systems simultaneosly, I feel this approach is correct. |
|
|
|
|
|
|
|