Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
alter proc getdata
@subcompname varchar(50)
as
begin

declare @liststr varchar(max)
select @liststr = coalesce(@liststr+',','')+subcompname from IRNUMBER
declare @subcomp varchar(max)=@liststr
declare @stringval varchar(100)
declare @output table([stringval] varchar(50))
while LEN(@subcomp)>0
begin
set @stringval = LEFT(@subcomp,ISNULL(nullif(charindex(',',@subcomp)-1,-1),len(@subcomp)))
set @subcomp= SUBSTRING(@subcomp,ISNULL(nullif(charindex(',',@subcomp),0),len(@subcomp))+1,len(@subcomp))
insert into @output([stringval]) values(@stringval)
end
select stringval from @output where(substring(stringval,1,10)=@subcompname)



iam having data like this

sno           subcompname            iRnumber           Drawingnumber
1              c1,c2,c3                 001                    1

2               c200,c444,c555          002                     2

3              c777,c878,c999           003                      3



when user enters 'c777' iam getting third record correctly as "stringval c777".
and i also want iRnumber and Drawingnumber............how to write select statement......
please help me..........
Posted
Updated 12-May-15 0:46am
v2
Comments
Andy Lanng 12-May-15 6:50am    
Why do you need to turn this into a select statement? Why not execute the proc from c#?

I could understand if you want to do most of the processing in c#, but that's not what you're asking for.

I cannot imagine any select statement that wont use stored procs and function to achieve this, but maybe there is someone better than I who can.
vemsoft 12-May-15 6:56am    
at present iam getting only subcompname..using this procedure i want irnumber and drawingnumber in this procedure
Andy Lanng 12-May-15 7:05am    
You can just add them to your result. Event a stored procedure can return more than a scalar value. I'll try to fit this into a solution
Andy Lanng 12-May-15 7:15am    
OK - I get it now, but I need more info.
There is a table called IRNUMBER. is there a column called iRnumber?
how does iRnumber relate to subcompname?
Where does Drawingnumber come from?
vemsoft 12-May-15 7:24am    
there exist IRNUMBER table and irnumber and drawing number columns in that table...........

1 solution

Replace your last select statement with the following :

SQL
declare @var varchar(100)
select @var = stringval from @output where(substring(stringval,1,10)=@subcompname)

select sno, @var as stringval, iRnumber, Drawingnumber from iRnumber where subcompname like '%' + @var + '%'
 
Share this answer
 
Comments
vemsoft 13-May-15 3:22am    
here iam getting duplicate values.....when user enters 'c1' this query is retrieving c1,c122.c1422,c1000 etc......

i want only c1.
ConnectingKamlesh 13-May-15 3:54am    
use this :
select sno, @var as stringval, iRnumber, Drawingnumber from iRnumber where subcompname like '%' + @var + ',%'

note : I have only modified the last part of the query (',%')
vemsoft 13-May-15 5:45am    
sno subcompname iRnumber Drawingnumber
1 c1,c2,c123 001 1

2 c200,c444,c555 002 2

3 c777,c878,c999 003 3


if i enter c1 iam getting but when i enter c123 iam not getting
ConnectingKamlesh 13-May-15 7:06am    
is it possible for you update you table values with trailing comma values.
For e.g. update values in column subcompname as c1,c2,c123, (see the comma in the end).
vemsoft 13-May-15 7:50am    
Thank u.......kamlesh.its working....i added comma in the end

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