Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to create stored procedure to INSERT data

Author  Topic 

ssknoah
Starting Member

1 Post

Posted - 2007-12-10 : 02:42:58
hi all
i am a beginner, i have written an insert stored procedure.
i need my SP to check for the ID field whether the data is already present.

pls help ,me.

ALTER PROCEDURE [dbo].[SP_SaveGroupClassification]
@SubGroupName nvarchar(50),
--@GroupId int,
@ParentId int,
@ClassType nchar(2),
@RootId int
as
begin
INSERT INTO GroupClassification
(GroupName, ParentId, ClassType, RootId)
VALUES (@SubGroupName,@ParentId,@ClassType,@RootId)
end
return

Here i need to check the value in (@SubGroupName not to be repeated.it is my key field.

thank u

ssk

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-10 : 02:47:52
[code]INSERT INTO GroupClassification
(GroupName, ParentId, ClassType, RootId)
SELECT @SubGroupName,@ParentId,@ClassType,@RootId
WHERE not exists (select * from GroupClassification where GroupName = @SubGroupName) [/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-12-10 : 02:48:07
Change the INSERT like this:-
IF NOT EXISTS(SELECT * FROM GroupClassification WHERE GroupName=@SubGroupName)
BEGIN
INSERT INTO GroupClassification
(GroupName, ParentId, ClassType, RootId)
VALUES (@SubGroupName,@ParentId,@ClassType,@RootId)
END
Go to Top of Page
   

- Advertisement -