Click here to Skip to main content
15,891,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Basically i have a Stored Proc, the sql query result-set look like this:-

SQL
TID	CategoryFieldID	FieldName           FieldType ISDutyOnChange
17007	A	345022	Facebook Link	           L	1
17007	A	367736	main shared                T	1
17008	C	345024	Twitter                    L	1
17008	C	345025	Facebook Link	           L	1
17008	C	367737	main shared                T	1
17006	O	345019	Facebook Link	           L	1
17006	O	362042	High School req	           Z	1
17006	O	367738	main shared                T	1
16996	R	365285	High School req	           Z	1
16996	R	365725	First - Name	           T	1
16996	R	367739	main shared                T	1
16996	R	368540	Twitter User	           L	1
16996	R	368541	Facebook Link	           L	1
16996	R	368870	dutyonchange	           T	1


Now for each row of the above result-set, i have to first check "Category" column value and on the basis of which i have to insert data into different tables like :-
If its Category 'A' in the row ,then enter the row into table A and so-on...
Please help!
Posted
Updated 11-Feb-15 7:22am
v2

 
Share this answer
 
Comments
Maciej Los 11-Feb-15 13:43pm    
+5
Abhinav S 11-Feb-15 13:58pm    
Thank you.
[no name] 11-Feb-15 14:08pm    
+5Thankyou Abhinav S, Appreciated your help!
[no name] 11-Feb-15 16:56pm    
Which one is better CTE,Cursor or While loop in SQL server for around 100-1000 records?
Any help would be appreciated
Transact SQL knows: While[^] loop and CTE[^] also.

Now, you need to build and execute dynamic queries.
Building Dynamic SQL In a Stored Procedure[^]
Execute Dynamic SQL commands in SQL Server[^]
 
Share this answer
 
Comments
Abhinav S 11-Feb-15 13:58pm    
5.
Maciej Los 11-Feb-15 13:59pm    
Thank you ;)
[no name] 11-Feb-15 14:07pm    
Thank you for alternative(s), but i think Cursor would be perfect for row-by-row processing. +5
Maciej Los 11-Feb-15 14:27pm    
Common Table Epressions too ;)
BTW: cursor need to use While loop ;)
Thank you ;)
[no name] 11-Feb-15 16:43pm    
Actually i needed row-by-row processing. I mean get and store Category column value and Fieldid column value row by row as it traverses and process them to insert into table A,B,C,D according to the type of stored row column value

DECLARE @category CHAR,@fieldid INT,@fieldname VARCHAR(50)
DECLARE fieldcursor CURSOR FOR
SELECT
T.Category,
F.FieldID
,F.FieldName
FROM Field F

INNER JOIN Template T ON F.TemplateID = T.TemplateID

WHERE T.SportID = 1706-- variable @SPORTID

AND T.Category in ('R','O','A','C') -- variable @categoty

AND F.IsActive = 1

AND F.ISDutyOnChange = 1
OPEN fieldcursor
FETCH NEXT from fieldcursor into @category,@fieldid,@fieldname --ist row

while(@@FETCH_STATUS=0)
BEGIN
print @category print @fieldid print @fieldname

--My logic here
FETCH NEXT from fieldcursor into @category,@fieldid,@fieldname --Move to Second row
END

CLOSE fieldcursor
DEALLOCATE fieldcursor

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