Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have an sp and passing values like 'xxx~yyy', '10~12~15', '1~2~7' ,etc... and i have to loop thru the data to get the resultset
i need to loop thru like
1. xxx-10-1
2. xxx-10-2
3. xxx-10-7
4. xxx-12-1
5. xxx-12-2
6. xxx-12-7
...................
...................
yyy-15-1
yyy-15-2
yyy-15-7

how to acheive this with in sp
Posted

1 solution

It is working...


USE [DBNAME]
GO
/****** Object:  StoredProcedure [dbo].[ohms_sp_add_services_list]    Script Date: 02/16/2014 17:51:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE PROCEDURE [dbo].[sp2_loop]
	@param1 varchar(1000)='xxx~yyy'
	AS

SET NOCOUNT ON

DECLARE @Prm_id varchar(10), @Pos int
	SET @param1  = LTRIM(RTRIM(@param1)) + '~'
	SET @Pos = CHARINDEX('~', @param1, 1)

IF REPLACE(@param1, '~', '') <> ''
	BEGIN
		WHILE @Pos > 0
		BEGIN
			SET @Prm_id = LTRIM(RTRIM(LEFT(@param1, @Pos - 1)))
			IF @prm_id <> ''
			IF(EXISTS(select 1 from Table_2 where substring(Test1,1,3) = @prm_id))

			BEGIN
			select * from Table_2 where substring(Test1,1,3) = @prm_id
			END

			SET @param1 = RIGHT(@param1, LEN(@param1) - @Pos)
			SET @Pos = CHARINDEX('~', @param1, 1)
			END
			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