CREATE PROCEDURE ViewCreater
AS
BEGIN
CREATE VIEW [V97] AS
SELECT title,type1,Tedat,DATEADD(DAY,-2,DATEADD(YEAR,-1,Tarikh))AS NewDate97, Descrip FROM Test1;
Go;
Asked
Active
Viewed 470 times
0

TheGeneral
- 79,002
- 9
- 103
- 141

Rozbeh
- 19
- 3
-
4Does this answer your question? [Creating a View using stored procedure](https://stackoverflow.com/questions/7712702/creating-a-view-using-stored-procedure) – Tomislav Baljint Feb 10 '20 at 08:06
-
1A stored procedure is meant to be called repeatedly, a view is meant to be *created* once and *queried* repeatedly. So creating a view in an SP sounds like a mismatch ... – Hans Kesting Feb 10 '20 at 08:50
2 Answers
0
A stored procedure cannot be created with the create View command in it
More information available in https://social.msdn.microsoft.com/Forums/sqlserver/en-US/af8b0c2d-8117-42e3-a701-05035644f73d/is-it-possible-to-create-a-view-within-a-stored-procedure?forum=sqlgetstarted

Aswani Madhavan
- 816
- 6
- 19
0
You can use the following code.
CREATE PROCEDURE ViewCreater
AS
BEGIN
IF EXISTS (
SELECT 1
FROM sysobjects
WHERE NAME = 'Test1'
AND xtype = 'V'
)
BEGIN
DROP VIEW dbo.vw00Menu;
END
EXEC sp_executesql
N'CREATE VIEW [V97] AS
SELECT title,type1,Tedat,
DATEADD(DAY,-2,DATEADD(YEAR,-1,Tarikh))AS NewDate97, Descrip FROM Test1'
END

Reza Jenabi
- 3,884
- 1
- 29
- 34