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
 run a stored procedure from a view

Author  Topic 

pmccann1
Posting Yak Master

107 Posts

Posted - 2009-01-07 : 07:44:25
is it possible to run a stored procedure from a view then look at the table that has been created from the procedure?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 08:00:04
No. Not in the same statement.
And a view can only have one SELECT statement.

But you can try with

SELECT *
FROM OPENDATASOURCE(.., 'EXEC MyStoredProcedure) AS f


I think what you need is a table function.



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

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-01-07 : 08:03:24
Umh...what? You can have a procedure that creates a view but not the other way around. That just doesn't make sense...

What is it you actually want to achieve?

- Lumbago
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-07 : 08:51:14
quote:
Originally posted by pmccann1

is it possible to run a stored procedure from a view then look at the table that has been created from the procedure?


can i ask the purpose for such a requirement?
Go to Top of Page

pmccann1
Posting Yak Master

107 Posts

Posted - 2009-01-07 : 09:41:19
Bascially say i have the follwoing information

id course dept status
1 edexcel lit 1
2 geog sci 5
2 maths sci 7

i want one instance of each, as in one row for id 1 and one row for id 2 either of the ones that are there. the user that want this information can only use views that they have been give permission to. there is already a sp that does something likethis so i was just going to use it.

suggestions to what way i could do it are welcome, could this just be done in a view only
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-07 : 09:45:30
quote:
Originally posted by pmccann1

Bascially say i have the follwoing information

id course dept status
1 edexcel lit 1
2 geog sci 5
2 maths sci 7

i want one instance of each, as in one row for id 1 and one row for id 2 either of the ones that are there. the user that want this information can only use views that they have been give permission to. there is already a sp that does something likethis so i was just going to use it.

suggestions to what way i could do it are welcome, could this just be done in a view only



yup. you can do this in view itself. just use like below

CREATE VIEW Yourview
AS
SELECT t.id,
t.course,
t.dept,
t.status
FROM YourTable t
INNER JOIN (SELECT id,MAX(status) AS MaxStatus
FROM YourTable
GROUP BY id) t1
ON t1.id=t.id
AND t1.MaxStatus=t.status
Go to Top of Page

pmccann1
Posting Yak Master

107 Posts

Posted - 2009-01-07 : 10:19:52
this is the actual sql select statement will that work ok with that

SELECT DISTINCT dbo.stmbiogr.student_id, RIGHT(dbo.stmbiogr.student_id, 8) AS [Access No], dbo.stmaos.aos_code, dbo.stmaos.acad_period, dbo.stmaos.aos_period, dbo.stcsessd.aos_end_dt, dbo.stmaos.stage_code, dbo.stmaos.attend_mode, dbo.stmbiogr.forename, dbo.stmbiogr.surname,
dbo.stmbiogr.birth_dt, dbo.stcstatd.full_desc

FROM dbo.stmbiogr INNER JOIN
dbo.stmaos ON dbo.stmbiogr.student_id = dbo.stmaos.student_id INNER JOIN
dbo.stcstatd ON dbo.stmaos.aos_code = dbo.stcstatd.aos_code INNER JOIN
dbo.stcsttyp ON dbo.stcstatd.aos_code = dbo.stcsttyp.aos_code INNER JOIN
dbo.stcsessd ON dbo.stmaos.aos_code = dbo.stcsessd.aos_code AND dbo.stmaos.acad_period = dbo.stcsessd.acad_period AND
dbo.stmaos.aos_period = dbo.stcsessd.aos_period
WHERE (dbo.stcsttyp.aos_type = 'C') AND (dbo.stmaos.acad_period = '08/09') AND (dbo.stmaos.stage_code = 'ENR') AND
(dbo.stcsessd.geolocn_code IN ('D0001', 'C0001'))
ORDER BY dbo.stmbiogr.student_id, dbo.stmaos.attend_mode, dbo.stcsessd.aos_end_dt
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-07 : 10:28:54
are you using sql 2005? then you can just use

SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY dbo.stmbiogr.student_id ORDER BY dbo.stcsessd.aos_end_dt DESC) AS RowNo,
dbo.stmbiogr.student_id, RIGHT(dbo.stmbiogr.student_id, 8) AS [Access No], dbo.stmaos.aos_code, dbo.stmaos.acad_period, dbo.stmaos.aos_period, dbo.stcsessd.aos_end_dt, dbo.stmaos.stage_code, dbo.stmaos.attend_mode, dbo.stmbiogr.forename, dbo.stmbiogr.surname,
dbo.stmbiogr.birth_dt, dbo.stcstatd.full_desc

FROM dbo.stmbiogr INNER JOIN
dbo.stmaos ON dbo.stmbiogr.student_id = dbo.stmaos.student_id INNER JOIN
dbo.stcstatd ON dbo.stmaos.aos_code = dbo.stcstatd.aos_code INNER JOIN
dbo.stcsttyp ON dbo.stcstatd.aos_code = dbo.stcsttyp.aos_code INNER JOIN
dbo.stcsessd ON dbo.stmaos.aos_code = dbo.stcsessd.aos_code AND dbo.stmaos.acad_period = dbo.stcsessd.acad_period AND
dbo.stmaos.aos_period = dbo.stcsessd.aos_period
WHERE (dbo.stcsttyp.aos_type = 'C') AND (dbo.stmaos.acad_period = '08/09') AND (dbo.stmaos.stage_code = 'ENR') AND
(dbo.stcsessd.geolocn_code IN ('D0001', 'C0001'))
)t
WHERE RowNo=1
ORDER BY student_id, attend_mode, aos_end_dt
Go to Top of Page

pmccann1
Posting Yak Master

107 Posts

Posted - 2009-01-07 : 10:42:02
I am using sql2005 but when i use the above it says the
SELECT ROW_NUMBER() OVER is not supported
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-07 : 11:50:08
You have to change the compatibility level to 90 or more.



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-07 : 11:59:27
quote:
Originally posted by pmccann1

I am using sql2005 but when i use the above it says the
SELECT ROW_NUMBER() OVER is not supported


what does this return?

EXEC sp_dbcmptlevel 'your db'
Go to Top of Page
   

- Advertisement -