Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hell ALL,
I want to check my database if this View (VW_ME) exists, it result is true it should be droped because of some modification on it, other wise it should create it.
bellow is my SQL STATEMENT
SQL
IF exists(select 1 from [dbo].[vw_Me])
	BEGIN
	   drop view [dbo].[vw_ME]
	END
ELSE 
	BEGIN
	declare @sql varchar(1000)
	select @sql = 'CREATE VIEW VW_ME AS
		   SELECT * FROM drug'
	 exec (@sql)
	END


sql server management studio (SSMS) return the error bellow

Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.vw_Me'.

Any help will be appreciated.

thank you all for your constant support.

[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 29-Jan-16 4:01am
v2
Comments
[no name] 29-Jan-16 9:58am    
a.) IF THEN ELSE makes no sense
That coud be the reason of your Problem, you most probably ran it two time. After first run your view is deleted and gives the error message on second run
b.) Check for view exists a maybe better way:
IF OBJECT_ID('ViewName', 'V') IS NOT NULL
DROP VIEW ViewName;
OriginalGriff 29-Jan-16 10:01am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

1 solution

I suggest checking if the view exists, rather than trying to select from it. When it does not exist you'll get the error you are getting. So, do it this way.

SQL
IF OBJECT_ID('vw_Me', 'V') IS NOT NULL
    DROP VIEW vw_Me;
GO
... -- now create it
 
Share this answer
 
Comments
[no name] 29-Jan-16 10:57am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900