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)
 help with sql control flow (if else)

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-10-21 : 17:14:52
Hi,

I have a SPROC I am trying to write, and I'm wondering if I'm able to reuse some logic.

I have always just written simple statements because thats all I've needed to do. Like this for example:


IF @var > 2
BEGIN
--DO STUFF 1
END
ELSE
BEGIN
--DO STUFF 2
END

In this new SPROC I have some logic I would like to reuse. Is there a way to put this "DO STUFF 3" in a spot in the sproc where its executed on either IF = true or ELSE = true ?

hope this is clear. Basically just seeing if there is a better practice for reusing code.

Thanks!
mike123



IF @var > 2
BEGIN

--DO STUFF 1
--DO STUFF 3
END
ELSE
BEGIN

--DO STUFF 2
--DO STUFF 3
END


webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-10-21 : 17:19:53
Do you mean this?

IF @var > 2
BEGIN
--DO STUFF 1
END
ELSE
BEGIN
--DO STUFF 2
END
--DO STUFF 3

Webfred

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-21 : 17:20:36
It depends on what "Do stuff 3" is...


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-10-21 : 17:26:02
quote:
Originally posted by Peso

It depends on what "Do stuff 3" is...


E 12°55'05.63"
N 56°04'39.26"




sorry I was trying to make this easier :)

first we create a temp table, then run an insert into with either

Dostuff 1 = Select statement 1
Dostuff 2 = Select statement 2


then dostuff3 is just again selecting from the temp table for paging purposes


hope thats clear!

thanks :)
mike123
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2008-10-21 : 17:26:20
Do stuff 3.. fishy ! :)
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-21 : 17:33:50
DO Stuff 3 in the end just like Web said.
Go to Top of Page

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2008-10-21 : 17:33:59
sorry guys, I was trying to make this easier.. I made it more difficult to understand.... :)

Here is my TSQL below :)

THanks again,
mike123

--Create temporary table to store records
CREATE TABLE #tempUsers
(
ID int IDENTITY,
nameOnline varchar(50)
)
INSERT INTO #tempUsers (nameOnline)


IF @genderID > 2
BEGIN

SELECT TOP 600 nameOnline FROM tblUserDetails

END
ELSE
BEGIN

SELECT TOP 600 nameOnline FROM tblUserDetails WHERE genderID = @genderID
END


--part 3 I want to run below no matter what

-- find out the last and first record we want
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)
--Now Return the set of paged records, plus indication of further records
set @totalRecords =
(
SELECT COUNT(userID)
FROM #tempUsers TU
--WHERE TU.ID > @LastRec
)
SELECT
ID,
nameOnline

FROM #tempUsers
WHERE ID > @FirstRec AND ID < @LastRec
SET NOCOUNT OFF

GO


Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-21 : 17:36:42
should work
Go to Top of Page
   

- Advertisement -