Home All Groups Group Topic Archive Search About

SqlServer stored procedure newby question

Author
20 Nov 2005 6:01 AM
Lloyd Dupont
I'm trying to write a stored procedure which try to write some text and
return the new ID or -1 if the text was already there.

something along those lines:
Table: BugReport
------------------
ID: int, identity/auto-increment
Text: nvarchar(max)

create bool Procedure(nvarchar(max) bugText)
{
    if( count( select * from BugReport where Text = bugText) )
        return -1;

    int ret;
    insert into BugReport ( Text ) values ( BugReport ); select ret =
@@lastrowid;
    return ret;
}

How would you write this in real/valid TSQL ?
Thanks!

Author
20 Nov 2005 9:17 AM
luxspes
What about:

ALTER PROCEDURE [dbo].[AddBug]
      @bugtext nvarchar(max) ,
      @idkey int OUTPUT
      AS
BEGIN
         DECLARE @count int ;
         SELECT @count = count(*) FROM BugInfo WHERE BugInfo=  @bugtext;

          IF @count = 0
          BEGIN
             INSERT INTO BugInfo ( BugInfo ) VALUES ( @bugtext );
            SELECT @idkey =  @@IDENTITY;
              RETURN;
          END
     SET @idkey = -1;
END

I tested it with the following code...and it worked fine:

DECLARE    @return_value int,
         @idkey int

SELECT    @idkey = 0

EXEC    @return_value = [dbo].[AddBug]
         @bugtext = N'Some bug',
         @idkey = @idkey OUTPUT

SELECT    @idkey as N'@idkey'

SELECT    'Return Value' = @return_value

GO

The table was defined like this (using SQLServer 2005):

CREATE TABLE [dbo].[BugInfo](
     [Id] [int] IDENTITY(1,1) NOT NULL,
     [BugInfo] [varchar](50) COLLATE Modern_Spanish_CI_AS NULL,
  CONSTRAINT [PK_BugInfo] PRIMARY KEY CLUSTERED
(
     [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

I hope all this it works for you ;)...

Lloyd Dupont wrote:
Show quote
> I'm trying to write a stored procedure which try to write some text and
> return the new ID or -1 if the text was already there.
>
> something along those lines:
> Table: BugReport
> ------------------
> ID: int, identity/auto-increment
> Text: nvarchar(max)
>
> create bool Procedure(nvarchar(max) bugText)
> {
>     if( count( select * from BugReport where Text = bugText) )
>         return -1;
>
>     int ret;
>     insert into BugReport ( Text ) values ( BugReport ); select ret =
> @@lastrowid;
>     return ret;
> }
>
> How would you write this in real/valid TSQL ?
> Thanks!
>
>
Author
20 Nov 2005 9:17 AM
luxspes
What about:

ALTER PROCEDURE [dbo].[AddBug]
      @bugtext nvarchar(max) ,
      @idkey int OUTPUT
      AS
BEGIN
         DECLARE @count int ;
         SELECT @count = count(*) FROM BugInfo WHERE BugInfo=  @bugtext;

          IF @count = 0
          BEGIN
             INSERT INTO BugInfo ( BugInfo ) VALUES ( @bugtext );
            SELECT @idkey =  @@IDENTITY;
              RETURN;
          END
     SET @idkey = -1;
END

I tested it with the following code...and it worked fine:

DECLARE    @return_value int,
         @idkey int

SELECT    @idkey = 0

EXEC    @return_value = [dbo].[AddBug]
         @bugtext = N'Some bug',
         @idkey = @idkey OUTPUT

SELECT    @idkey as N'@idkey'

SELECT    'Return Value' = @return_value

GO

The table was defined like this (using SQLServer 2005):

CREATE TABLE [dbo].[BugInfo](
     [Id] [int] IDENTITY(1,1) NOT NULL,
     [BugInfo] [varchar](50) COLLATE Modern_Spanish_CI_AS NULL,
  CONSTRAINT [PK_BugInfo] PRIMARY KEY CLUSTERED
(
     [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

I hope all this it works for you ;)...

Lloyd Dupont wrote:
Show quote
> I'm trying to write a stored procedure which try to write some text and
> return the new ID or -1 if the text was already there.
>
> something along those lines:
> Table: BugReport
> ------------------
> ID: int, identity/auto-increment
> Text: nvarchar(max)
>
> create bool Procedure(nvarchar(max) bugText)
> {
>     if( count( select * from BugReport where Text = bugText) )
>         return -1;
>
>     int ret;
>     insert into BugReport ( Text ) values ( BugReport ); select ret =
> @@lastrowid;
>     return ret;
> }
>
> How would you write this in real/valid TSQL ?
> Thanks!
>
>
Author
20 Nov 2005 6:17 PM
Sericinus hunter
luxspes wrote:
Show quote
> What about:
>
> ALTER PROCEDURE [dbo].[AddBug]
>      @bugtext nvarchar(max) ,
>      @idkey int OUTPUT
>      AS
> BEGIN
>         DECLARE @count int ;
>         SELECT @count = count(*) FROM BugInfo WHERE BugInfo=  @bugtext;
>
>          IF @count = 0
>          BEGIN
>             INSERT INTO BugInfo ( BugInfo ) VALUES ( @bugtext );
>            SELECT @idkey =  @@IDENTITY;
>              RETURN;
>          END
>     SET @idkey = -1;
> END

Instead of SELECT COUNT(*) I would suggest

    IF EXISTS (SELECT 1 FROM BugInfo WHERE BugInfo = @bugtext)
    BEGIN
    ...further as written

This will likely be more efficient.
Author
21 Nov 2005 1:30 AM
Lloyd Dupont
Thanks Sericinus, interesting precision.

Show quote
"Sericinus hunter" <serh***@flash.net> wrote in message
news:p53gf.1757$nA2.1545@newssvr22.news.prodigy.net...
> luxspes wrote:
>> What about:
>>
>> ALTER PROCEDURE [dbo].[AddBug]
>>      @bugtext nvarchar(max) ,
>>      @idkey int OUTPUT
>>      AS
>> BEGIN
>>         DECLARE @count int ;
>>         SELECT @count = count(*) FROM BugInfo WHERE BugInfo=  @bugtext;
>>
>>          IF @count = 0
>>          BEGIN
>>             INSERT INTO BugInfo ( BugInfo ) VALUES ( @bugtext );
>>            SELECT @idkey =  @@IDENTITY;
>>              RETURN;
>>          END
>>     SET @idkey = -1;
>> END
>
> Instead of SELECT COUNT(*) I would suggest
>
> IF EXISTS (SELECT 1 FROM BugInfo WHERE BugInfo = @bugtext)
> BEGIN
> ...further as written
>
> This will likely be more efficient.

AddThis Social Bookmark Button