| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2009-01-30 : 09:49:00
|
| I have four reports to select from on a page. If the user select's a report I want them to get the right info from the correct table. How would I write this? I am getting incorrect syntax near As and If.@Rpt char(1)AsSET NOCOUNT ONIf @Rpt = '1' AS SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults1End if@Rpt char(1)AsSET NOCOUNT ONIf @Rpt = '2' AS SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults2End if@Rpt char(1)AsSET NOCOUNT ONIf @Rpt = '3' AS SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults3End if@Rpt char(1)AsSET NOCOUNT ONIf @Rpt = '4' AS SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults4End if |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-30 : 09:53:24
|
One way would be thisCREATE PROCEDURE dbo.spMyProcedure( @Rpt CHAR(1))ASSET NOCOUNT ONIf @Rpt = '1' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt] FROM NatResults1If @Rpt = '2' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt] FROM NatResults2If @Rpt = '3' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt] FROM NatResults3If @Rpt = '4' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt] FROM NatResults2 FROM NatResults4 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-30 : 09:53:29
|
you dont need intermediate AS and endifs@Rpt char(1)AsSET NOCOUNT ONIf @Rpt = '1' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults1If @Rpt = '2' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults2If @Rpt = '3' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults3If @Rpt = '4' SELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults4GO |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-30 : 09:54:20
|
A better way can beCREATE PROCEDURE dbo.spMyProcedure( @Rpt CHAR(1))ASSET NOCOUNT ONSELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults1WHERE @Rpt = '1'UNION ALLSELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults2WHERE @Rpt = '2'UNION ALLSELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults3WHERE @Rpt = '3'UNION ALLSELECT sort, reg, [Jan 08], [Jan 08 rank], [Jan 08 icnt], [Jan 08 tcnt], [Feb 08], [Feb 08 rank], [Feb 08 icnt], [Feb 08 tcnt]FROM NatResults4WHERE @Rpt = '4' E 12°55'05.63"N 56°04'39.26" |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2009-01-30 : 10:17:37
|
| Thanks guys for the different choices they work! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-30 : 10:19:20
|
| welcome |
 |
|
|
|
|
|