Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
my stored procedure is as follows:
SQL
ALTER procedure [dbo].[spAllinone]
(
@event tinyint,
@Empid int Identity(1,1) PRIMARY KEY NOT NULL,
@Empname nvarchar(50)=null,
@Salary int=null,
@City nvarchar(50)=null
)

as
if @event = 1
begin
insert into employee_tab(Empname,Salary,City)
values (@Empname,@Salary,@City)
end

else if @event = 2
begin
update employee_tab
set Empname = @Empname,Salary = @Salary,City = @City
where (Empid = @Empid)
End

else if @event = 3
begin
delete from employee_tab
where (Empid = @Empid)
End

but when execute it ,it gives me error like this:

Msg 156, Level 15, State 1, Procedure spAllinone, Line 4
Incorrect syntax near the keyword 'Identity'.
Msg 137, Level 15, State 2, Procedure spAllinone, Line 14
Must declare the scalar variable "@Empname".
Msg 137, Level 15, State 2, Procedure spAllinone, Line 20
Must declare the scalar variable "@Empname".
Msg 137, Level 15, State 2, Procedure spAllinone, Line 27
Must declare the scalar variable "@Empid"
What i want is to auto increase the column Empid value. i have a web application on visual studio 2008 . i dont want to pass the Empid . only i want to pass Empname,Salary,City when performing insert operation.Thanks.
Posted
Updated 9-Jun-13 22:06pm
v2
Comments
kumar2413 10-Jun-13 4:08am    
@Empname nvarchar(50) is enough no need of = symbol all

1)You should try below query. If you have already created procedure then use 'ALTER PROCEDURE' else use 'CREATE PROCEDURE'.

2)You just need to remove 'Identity(1,1) PRIMARY KEY NOT NULL' portion from parameter of SP.
Please let me know if this helps. Thanks.

SQL
ALTER procedure [dbo].[spAllinone]
(
@event tinyint,
@Empid int,
@Empname nvarchar(50)=null,
@Salary int=null,
@City nvarchar(50)=null
)
 
as
if @event = 1
begin
insert into employee_tab(Empname,Salary,City)
values (@Empname,@Salary,@City)
end
 
else if @event = 2
begin
update employee_tab
set Empname = @Empname,Salary = @Salary,City = @City
where (Empid = @Empid)
End
 
else if @event = 3
begin
delete from employee_tab
where (Empid = @Empid)
End

[edit]Code block added[/edit]
 
Share this answer
 
v3
Comments
Rambo_Raja 10-Jun-13 5:42am    
yes it...but how to ato inc the column Empid.
As your Empid column of table employee_tab is identity(auto incremented), then no need to specify that column while insertion. You need to specify columns other than identity. Identity column will inserted automatically. e.g.
SQL
insert into employee_tab(Empname,Salary,City)
values (@Empname,@Salary,@City)


In this query, you just need to pass three parameters. i.e.@Empname,@Salary,@City.
Empid will be inserted automatically.

If you want to make Empid as identity column.
1) Modify the table emplyee_tab.
2)after clicking on empid column,you can see column properties below.
3)set Is Identity(under Identity specification tab) property to true.
 
Share this answer
 
v2
Sql Server - How to write a Stored procedure in Sql server[^]
Have a look in this site will help u better
 
Share this answer
 
Try this...
SQL
ALTER procedure [dbo].[spAllinone]
(
@event tinyint,
@Empid int ,
@Empname nvarchar(50)=null,
@Salary int=null,
@City nvarchar(50)=null
) 
as
BEGIN
    if @event = 1
    begin
    insert into employee_tab(Empname,Salary,City)
    values (@Empname,@Salary,@City)
    end
     
    else if @event = 2
    begin
    update employee_tab
    set Empname = @Empname,Salary = @Salary,City = @City
    where (Empid = @Empid)
    End
     
    else if @event = 3
    begin
    delete from employee_tab
    where (Empid = @Empid)
    End
END

Happy Coding!
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900