Question

I am very new to stored procedures. I have a table with an Id auto increment. I want to insert, update and delete rows in my table.

What is wrong with my stored procedure?

CREATE PROCEDURE [dbo].[hrm_Languages]
(
    @Name varchar(120),
    @CreatedBy bigint=0,
    @UpdatedBy bigint=0,
    @IsDeleted bit=0
    @ID int OUTPUT
)
AS
BEGIN
    SELECT @ID = ISNULL(MAX(ID), 0) + 1
    FROM [dbo].[Languages]

    IF @StatementType = 'Insert'
    BEGIN
       insert into [dbo].[Languages] (Name, CreatedOn, UpdatedOn, CreatedBy, UpdatedBy, IsDeleted) 
       values(@Name, DateTime(), DateTime, @CreatedBy, @UpdatedBy, @IsDeleted)

       SELECT @Name
       WHERE NOT EXISTS (SELECT ID, NAME 
                         FROM TableName
                         WHERE NAME = @Name)
      BEGIN

      END
    END

    IF @StatementType = 'Select'
    BEGIN
         select * from [dbo].[Languages]
    END 

    IF @StatementType = 'Update'
    BEGIN
        UPDATE [dbo].[Languages] 
        SET Name =  @Name, UpdateOn = DateTime()
        WHERE ID = @ID
    END

    else IF @StatementType = 'Delete'
    BEGIN
        DELETE FROM [dbo].[Languages] WHERE ID = @ID
    END
end

Getting these errors:

Msg 102, Level 15, State 1, Procedure hrm_Languages, Line 7
Incorrect syntax near '@ID'.

Msg 137, Level 15, State 1, Procedure hrm_Languages, Line 12
Must declare the scalar variable "@ID".

Msg 137, Level 15, State 2, Procedure hrm_Languages, Line 15
Must declare the scalar variable "@StatementType".

Msg 195, Level 15, State 10, Procedure hrm_Languages, Line 17
'DateTime' is not a recognized built-in function name.

Msg 156, Level 15, State 1, Procedure hrm_Languages, Line 24
Incorrect syntax near the keyword 'END'.

Msg 137, Level 15, State 2, Procedure hrm_Languages, Line 26
Must declare the scalar variable "@StatementType".

Msg 137, Level 15, State 2, Procedure hrm_Languages, Line 31
Must declare the scalar variable "@StatementType".

Msg 195, Level 15, State 10, Procedure hrm_Languages, Line 34
'DateTime' is not a recognized built-in function name.

Msg 137, Level 15, State 2, Procedure hrm_Languages, Line 38
Must declare the scalar variable "@StatementType".

Msg 137, Level 15, State 2, Procedure hrm_Languages, Line 40
Must declare the scalar variable "@ID".

I also want to show message on my web page using label that Name inserted, Nme deleted, NAme already exist

Was it helpful?

Solution 3

I have corrected stored procedure syntactically. I have written comments for changes I have made. Correct sql will create SP for you.

CREATE PROCEDURE [dbo].[hrm_Languages]
(
 @Name varchar(120),
 @CreatedBy bigint=0,
 @UpdatedBy bigint=0,
 @IsDeleted bit=0, -- Insert comma
 @StatementType Varchar(20), -- Add variable @StatementType 
 @ID int OUTPUT
)

 AS
 BEGIN



 IF @StatementType = 'Insert'
 BEGIN

 -- As @ID will reset again after insertion of record so following two line are not    needed
 SELECT @ID = ISNULL(MAX(ID), 0) + 1
 FROM [dbo].[Languages]

 SELECT @Name
 WHERE NOT EXISTS (SELECT ID,NAME FROM TableName -- What is table name for 
                 WHERE NAME=@Name)

insert into [dbo].[Languages] (Name,CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted) 
values( @Name, SYSDATETIME(), SYSDATETIME(),  @CreatedBy, @UpdatedBy,@IsDeleted) 
-- SysDateTime is used to get current datetime

SELECT @ID = SCOPE_IDENTITY()    

End

ELSE IF @StatementType = 'Select'
Begin
select * from [dbo].[Languages]
END 

ELSE IF @StatementType = 'Update'
BEGIN
UPDATE [dbo].[Languages] SET
        Name =  @Name, UpdatedOn = SYSDATETIME()
  WHERE ID = @ID
END

ELSE IF @StatementType = 'Delete'
BEGIN
 DELETE FROM [dbo].[Languages] WHERE ID = @ID
END

END

OTHER TIPS

OK, problems with your sproc (after reading it again), please see comments:

    -- If you worked for me, I'd be having words about this name, it does not state
    -- what this sproc does.
    CREATE PROCEDURE [dbo].[hrm_Languages]
    (
        @Name varchar(120),
        @CreatedBy bigint=0,
        @UpdatedBy bigint=0,
        @IsDeleted bit=0, -- You need a comma here.  You hadn't posted your errors when I answered
                          -- this site isn't really for fixing your compile errors... read
                          -- the error text, it's quite helpful.  Something wrong on line 7...
        @ID int OUTPUT
    )

    AS
    BEGIN

    -- Problem 1.  You are selecting this ID prior to inserting.  This won't always be the 
    -- ID inserted, if your system is getting heavy use.

    SELECT @ID = ISNULL(MAX(ID), 0) + 1
        FROM [dbo].[Languages]

    -- Problem 2. You don't pass in @StatementType - this won't even compile.
    IF @StatementType = 'Insert'
    BEGIN
    insert into [dbo].[Languages] (Name,CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted) 
    values( @Name, DateTime(), DateTime,  @CreatedBy, @UpdatedBy,@IsDeleted)

    -- To get the inserted ID, do the following
    SELECT @ID = SCOPE_IDENTITY()


    -- Not sure what the following is for.
    SELECT @Name
       WHERE NOT EXISTS (SELECT ID,NAME FROM TableName
                         WHERE NAME=@Name)
    BEGIN

    END
    END


    -- This is where your sproc should end and the following should be three other sprocs.

    IF @StatementType = 'Select'
    BEGIN
    select * from [dbo].[Languages]
    END 


   -- Problem 3.  You don't seem to be passing in and ID, just for output, and you set the ID in the
   -- first line.  If your system is heavily used, this might just end up updating a newly inserted
    -- row, but most likely will update nothing.

    -- Same goes for the delete below.  Create separate sprocs for doing these things.

    IF @StatementType = 'Update'
    BEGIN
    UPDATE [dbo].[Languages] SET
                Name =  @Name, UpdateOn= DateTime()
          WHERE ID = @ID
    END

    else IF @StatementType = 'Delete'
    BEGIN
    DELETE FROM [dbo].[Languages] WHERE ID = @ID
    END
    end

In addition to the Paddy's Answer you must use GetDate() Instead of DateTime() to get the current DateTime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top