Tuesday, March 27, 2012

Creating A View in a Stored Procedure

Hi Everyone

Im trying to create a view from within a stored procedure and are having problems. Is it possible to do this? And if so, how? I've been trying with the code below.

CREATE PROC upProcName AS

DECLARE @.Variable varchar(50)

CREATE VIEW vwName AS

SELECT DISTINCT Table1.*, Table2.*
FROM dbo.Table1
INNER JOIN dbo.Table2 AS BUG
ON Table1.Col1 = Table2.Col1
WHERE LI.accname = @.Variable

GO

Any Thoughts ideas would be great

Cheersyou can not create a view in a stored procedure.
Create view should be the first statement of the batch.|||I thought that may be the case. Is there a way to change the view or pass it parameters?|||I thought that may be the case. Is there a way to change the view or pass it parameters?|||Execute a sp_dboption YourDB, "DDL in tran", true|||why would you want to create a view within a store procedure??

I alway thought of views are used for
1. quick query
2. row level permission control

in what other situations would a view be used for?? for the reasons above i don't see why you would need to dynamically create a view.

cheers
James :)|||What about this one?

drop view test2
drop proc test
go
create proc test
as
exec('create view test2 as select getdate() as now')
select * from test2|||Originally posted by snail
What about this one?

drop view test2
drop proc test
go
create proc test
as
exec('create view test2 as select getdate() as now')
select * from test2

YUP!

But I like the question of why you need to do this though?

No comments:

Post a Comment