Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
I have table with following columns :-
userid (int , pk, identity) , username (nvarchar(50))

the following code of procedure :
ALTER PROCEDURE dbo.ManageUsers
(

@username nvarchar(50)
)

AS
	
	

INSERT INTO users (username) Values (@username)

	END
	
	RETURN



I can't insert data to this table with the identity column
but if isidentity option set to false and try to insert userid manually , it worked well


what's problem ??
Posted

Ignoring any possible problems with the connection from the asp.net front-end; the RETURN following the END in the stored procedure will give you a syntax error. So won't run.

If you're using Query Analyser/SQL Server Management Studio then highlighting a block of SQL and hitting <ctrl-f5> will give you a syntax check on that block. Can save a lot of time.

Just tried this and it works fine on SQL2005....

SQL
create table users
(
  ID int identity
     constraint PK_users primary key (ID),
  [name] nvarchar(50) not null
     constraint DF_username default('')
)

insert into [users] ([name]) values('sheila')
select * from users

create procedure adduser( @name nvarchar(50) )
as
begin
insert into [users] ([name]) values(@name)
return
end

exec adduser 'jim'
select * from users
 
Share this answer
 
v2
Comments
shms_rony 16-Jun-13 6:11am    
not working :(
I did the insertion using c# class
Use SET IDENTITY_INSERT[^] OFF/ON to insert values in identity column.
Try this:
SQL
ALTER PROCEDURE dbo.ManageUsers
(
    @username nvarchar(50),
    @userid int
)
AS
    SET IDENTITY_INSERT users ON 
    INSERT INTO users (userid, username) Values (@userid, @username)
    SET IDENTITY_INSERT users OFF
END
--RETURN

You can refer

--Amit
 
Share this answer
 
v2
Comments
shms_rony 16-Jun-13 6:12am    
I want automatic inserted values in the identity column
For that when you create user table just assign the primary key of id and set identity seed and increment as a "Yes" and after that create procedure as like:

Create procedure InsertUSerDetails
(
@username nvarchar(50)
)
as
begin
Insert into users values(@username);
end

---Its work fine if you want to try
 
Share this answer
 
v2
Thanks Dears, It works for me :)
 
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