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 simplify code

Author  Topic 

juicyapple
Posting Yak Master

176 Posts

Posted - 2009-03-29 : 04:41:41
Hi, anyway to simplify the code below? Thanks.

DECLARE DataFormula_cursor CURSOR FAST_FORWARD FOR        
select distinct Data_Formula, Query_Sequence
from tbl_Data_Formula
where App = @App And Area = @Area AND Status='active'
order by Query_Sequence

OPEN DataFormula_cursor
FETCH DataFormula_cursor INTO @strSelect, @intQuerySequence
WHILE @@FETCH_STATUS = 0
BEGIN
IF @intQuerySequence = 1
BEGIN
set @strSelect1 = @strSelect
END
ELSE IF @intQuerySequence = 2
BEGIN
set @strSelect2 = @strSelect
END
ELSE IF @intQuerySequence = 3
BEGIN
set @strSelect3 = @strSelect
END
ELSE IF @intQuerySequence = 4
BEGIN
set @strSelect4 = @strSelect
END
ELSE IF @intQuerySequence = 5
BEGIN
set @strSelect5 = @strSelect
END
ELSE IF @intQuerySequence = 6
BEGIN
set @strSelect6 = @strSelect
END
ELSE IF @intQuerySequence = 7
BEGIN
set @strSelect7 = @strSelect
END
ELSE IF @intQuerySequence = 8
BEGIN
set @strSelect8 = @strSelect
END
ELSE IF @intQuerySequence = 9
BEGIN
set @strSelect9 = @strSelect
END
ELSE IF @intQuerySequence = 10
BEGIN
set @strSelect10 = @strSelect
END
FETCH DataFormula_cursor INTO @strSelect, @intQuerySequence
END

CLOSE DataFormula_cursor
DEALLOCATE DataFormula_cursor

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2009-03-29 : 06:05:29
Have you looked at CASE functionality?

Jack Vamvas
--------------------
http://www.ITjobfeed.com
Go to Top of Page

juicyapple
Posting Yak Master

176 Posts

Posted - 2009-03-29 : 07:09:06

quote:
Originally posted by jackv

Have you looked at CASE functionality?

Jack Vamvas
--------------------
http://www.ITjobfeed.com




You mean using case in this select statement?
select distinct Data_Formula, Query_Sequence 
from tbl_Data_Formula
where App = @App And Area = @Area AND Status='active'
order by Query_Sequence

But how do I set the @strSelect1, ....? Please give some hints.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-03-29 : 10:19:49
Can we assume that the App and Area columns have only 1 row for each QuerySequence number?
Go to Top of Page

juicyapple
Posting Yak Master

176 Posts

Posted - 2009-03-29 : 22:51:06
quote:
Originally posted by robvolk

Can we assume that the App and Area columns have only 1 row for each QuerySequence number?



The tbl_Data_Formula data as below. In the example code, I select data_formula with App=1 and Area=1 and status='active' only. The Data_Formula datatype is varchar(max). So I need to pass each data_formula to @strSelect1, @strSelect2, ... then just construct a complete query string.

App Area Status Query_Sequence Data_Formula
----- ------ -------- --------------- -----------------------
1 1 Active 1 Select xxxxx
1 1 Active 2 Union all select xxxx
1 1 Active 3 Union all select xxxx
1 1 Archive 1 Select xxxxx
1 1 Archive 2 Union all select xxxx
1 1 Archive 3 Union all select xxxx
1 2 Active 1 Select xxxxx
1 2 Active 2 Union all select xxxx
1 2 Active 3 Union all select xxxx
1 2 Archive 1 Select xxxxx
1 2 Archive 2 Union all select xxxx
1 2 Archive 3 Union all select xxxx

Go to Top of Page
   

- Advertisement -