I am trying to create a view out of a stored procedure and am perplexed to see two opposing results from a very similar approach.
Example 1
CREATE PROCEDURE cv AS
GO
DECLARE @sql nvarchar(MAX)
SET @sql = 'CREATE VIEW test AS SELECT * FROM someOtherTable'
exec (@sql)
Whereas this example creates the view once the procedure is created for the 1st time, it will not recreate the view when I execute the procedure at a later stage using:
EXEC cv
Example 2
CREATE PROCEDURE cv
@table SYSNAME
AS
DECLARE @sql nvarchar(MAX)
SET @sql = 'CREATE VIEW '+ @table +' AS SELECT * FROM someOtherTable'
This one instead does not create the view when the procedure is created for the first time but creates the view afterwards every time it is called by:
EXEC @sql;
Why is this the case? I think this is really confusing and does not make sense or does it?
CREATE PROCEDUREmean create procedure not view. When you execute SP then you callCREATE VIEWwhich is creates view.