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 |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2009-09-29 : 09:56:53
|
| I'm hoping to have an SP that can follow the logic below, but no idea of the correct syntax, can anyone helpCREATE PROCEDURE sp_Test@vehicleref, intIF @vehicleref IN (SELECT vehicleRef FROM table 1 WHERE source = 'A' AND [type] = 'car') THENSELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = {0} AND term = 3 ORDER BY ch ASCELSEIF @vehicleref IN (SELECT vehicleRef FROM table 1 WHERE source = 'A' AND [type] = 'van') THENSELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = {0} AND term = 4 ORDER BY ch ASCELSESELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = {0} ORDER BY ch ASCThanks |
|
|
rocknpop
Posting Yak Master
201 Posts |
Posted - 2009-09-29 : 10:19:00
|
| CREATE PROCEDURE sp_Test@vehicleref intASIF @vehicleref IN (SELECT vehicleRef FROM table1 WHERE source = 'A' AND [type] = 'car') SELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = @vehicleref AND term = 3 ELSE IF @vehicleref IN (SELECT vehicleRef FROM table1 WHERE source = 'A' AND [type] = 'van') SELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = @vehicleref AND term = 4ELSE SELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures WHERE ch > 0 AND vehicleref = @vehicleref--------------------Rock n Roll with SQL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 13:10:22
|
you dont need IF ELSE at all. do something likeCREATE PROCEDURE sp_Test@vehicleref, intSELECT TOP 1 term,milespa,maintained,ch,pch FROM tblNewMatrixFigures tJOIN table 1 t1ON t1.vehicleRef=t.vehiclerefWHERE t.ch > 0 AND t.vehicleref = {0} AND t.term = CASE WHEN t1.source = 'A' AND t1.[type] = 'car' THEN 3 WHEN t1.source = 'A' AND t1.[type] = 'van' THEN 4 ELSE t.term ENDORDER BY t.ch ASC |
 |
|
|
|
|
|
|
|