Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a table 'Details' with data as below
NO R_NAME
--- ----
KL22 abc

then, when i add new value 'b' to R_NAME column, how to check is it already existing one in column
using stored procedure?

adding a new value without delimeter eg:- b.so my qstn is how to write a sp
to split delimeter and then check the value exist?


Thanks.
Posted
Updated 6-Feb-13 0:19am
v3

Put code as below in your stored procedure
IF NOT EXISTS (SELECT * FROM Details WHERE R_NAME= @R_NAME)
BEGIN
    --INSERT A NEW ROW HERE
END
 
Share this answer
 
Comments
hasbina 6-Feb-13 6:17am    
hmm.adding a new value without delimeter eg:- b.so my qstn is how to write a sp
to split delimeter and then check the value exist?
.
Based on your revised question and using abhinav's example ..

SQL
DECLARE @vLike as varchar(30)
set @vLike = '%' + @R_NAME + '%'
IF NOT EXISTS (SELECT * FROM Details WHERE R_NAME like @vLike)


However if you definitely want to split the string based on the delimiters then have a look
here[^]

and another method using PATINDEX[^]

A really indepth article about splitting [^]

Useful (ish) resource for learning about stored procedures http://www.mssqltips.com/sqlservertutorial/160/sql-server-stored-procedure/[^]
 
Share this answer
 
Actually the question should be more structured but i think i understand what you trying to explain to some extent. It's like you don't want duplicate values in that column [R_NAME]. What i will suggest is.
1. Create a Uniques constraint on that column [R_NAME] and use your code to catch any error thrown when user enters a value already existing.

2. Use a stored procedure as follows
SQL
Create Procedure Pr_DDetails
(
@No nvarchar(20), @r_name nvarchar(256)
)
As
Begin
declare @rname nvarchar(256),
select @rname = R_NAME from [TableName] where R_NAME = @rname
if(@rname IS NULL)
return 
Else
Insert Into [TableName] (NO, R_NAME) Values(@No, @r_name)
End


Let me know if you understand it as intended.
 
Share this answer
 
v2
Comments
Herman<T>.Instance 6-Feb-13 7:47am    
you check in the select (just below the declare) @rname with = @rname instead of = @r_name. Your solution will not work.
ahmedfaruk88 7-Feb-13 4:39am    
Thanks, i see that?

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