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.
| 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!mike123IF @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 > 2BEGIN--DO STUFF 1END ELSEBEGIN--DO STUFF 2END --DO STUFF 3WebfredNo, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
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" |
 |
|
|
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 eitherDostuff 1 = Select statement 1 Dostuff 2 = Select statement 2then dostuff3 is just again selecting from the temp table for paging purposeshope thats clear!thanks :)mike123 |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-10-21 : 17:26:20
|
| Do stuff 3.. fishy ! :) |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-21 : 17:33:50
|
| DO Stuff 3 in the end just like Web said. |
 |
|
|
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 recordsCREATE 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 wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPage SELECT @LastRec = (@Page * @RecsPerPage + 1)--Now Return the set of paged records, plus indication of further recordsset @totalRecords = ( SELECT COUNT(userID) FROM #tempUsers TU --WHERE TU.ID > @LastRec )SELECT ID,nameOnline FROM #tempUsersWHERE ID > @FirstRec AND ID < @LastRecSET NOCOUNT OFF GO |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-21 : 17:36:42
|
| should work |
 |
|
|
|
|
|