Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables arts and artist. I have written stored procedure for arts in which artistId is a foreign key. procedure for inserting data in table arts is as follows:

SQL
create procedure spInsertArts(
@artsId int out,
@name varchar(50),
@category varchar(50),
@artistId int
)
as
     begin
	select @artistId=SCOPE_IDENTITY()
		if exists(select artistId from artist)
         insert into arts(name,category,artistId) values(@name,@category,@artistId)
      end



when i execute it,it is giving error as:Procedure or function 'spInsertArts' expects parameter '@artistId', which was not supplied.i want to set artistId to latest value of artistId in artist Table.pls help.
Posted
Updated 23-Apr-14 3:15am
v2

If artsID is an identity column (guaranteeing uniqueness and monotonicity), you could ask for
SQL
SELECT @artsID = max(artID) from arts

(assuming that's the column name) It may be sensible for you to use this in a subquery, depending upon how you arrange things.

Now, after an insert, and before you close the connection, you can use
SELECT TOP 1 @@IDENTITY From arts


and it would be returned from your stored procedure.

Do not forget to include
SET NOCOUNT ON
in your procedure or the return record set will be a mess

 
Share this answer
 
change like this

create procedure spInsertArts(
@artsId int out=null,
@name varchar(50)=null,
@category varchar(50)=null,
@artistId int<pre>=null
)
as
     begin
	select @artistId=SCOPE_IDENTITY()
		if exists(select artistId from artist)
         insert into arts(name,category,artistId) values(@name,@category,@artistId)
      end
 
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