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
 General SQL Server Forums
 New to SQL Server Programming
 Need help writing stored procedure

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)
As
SET NOCOUNT ON
If @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 NatResults1

End if


@Rpt char(1)
As
SET NOCOUNT ON
If @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 NatResults2

End if


@Rpt char(1)
As
SET NOCOUNT ON
If @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 NatResults3

End if

@Rpt char(1)
As
SET NOCOUNT ON
If @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 NatResults4

End if

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-30 : 09:53:24
One way would be this
CREATE PROCEDURE dbo.spMyProcedure
(
@Rpt CHAR(1)
)
AS

SET NOCOUNT ON

If @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 NatResults1

If @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 NatResults2

If @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 NatResults3

If @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"
Go to Top of Page

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)
As
SET NOCOUNT ON
If @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 NatResults1


If @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 NatResults2


If @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 NatResults3


If @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 NatResults4


GO
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-30 : 09:54:20
A better way can be
CREATE PROCEDURE dbo.spMyProcedure
(
@Rpt CHAR(1)
)
AS

SET NOCOUNT ON

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 NatResults1
WHERE @Rpt = '1'

UNION ALL

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
WHERE @Rpt = '2'

UNION ALL

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 NatResults3
WHERE @Rpt = '3'

UNION ALL

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 NatResults4
WHERE @Rpt = '4'



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

JJ297
Aged Yak Warrior

940 Posts

Posted - 2009-01-30 : 10:17:37
Thanks guys for the different choices they work!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-30 : 10:19:20
welcome
Go to Top of Page
   

- Advertisement -