Steven Graff
asked on
Conditionally drop/create view using local variable
I want to test for the existence of a view and, if it does exist, drop it.
Whether it exists or not, I then want to re-create it. I can do that as follows:
=================
use gmdemo
IF OBJECT_ID('bbUserList') IS NOT NULL
DROP View bbUserList
/****** Object: View [dbo].[bbUserList] Script Date: 10/18/2008 18:09:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE View bbUserList as
select * from simplecrmlookup..bbusers where pin <> ''
================
So, the code above works fine.
But what I really need to do is somehow "parameterize" the view name:
================
use gmdemo
declare @viewName varchar(255)
set @viewName = 'bbUserList'
IF OBJECT_ID(@viewName) IS NOT NULL
DROP View @viewName
/****** Object: View [dbo].[bbUserList] Script Date: 10/18/2008 18:09:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE View @viewName as
select * from simplecrmlookup..bbusers where pin <> ''
================
Results in:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@viewName'.
Can this be done?
Whether it exists or not, I then want to re-create it. I can do that as follows:
=================
use gmdemo
IF OBJECT_ID('bbUserList') IS NOT NULL
DROP View bbUserList
/****** Object: View [dbo].[bbUserList] Script Date: 10/18/2008 18:09:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE View bbUserList as
select * from simplecrmlookup..bbusers where pin <> ''
================
So, the code above works fine.
But what I really need to do is somehow "parameterize" the view name:
================
use gmdemo
declare @viewName varchar(255)
set @viewName = 'bbUserList'
IF OBJECT_ID(@viewName) IS NOT NULL
DROP View @viewName
/****** Object: View [dbo].[bbUserList] Script Date: 10/18/2008 18:09:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE View @viewName as
select * from simplecrmlookup..bbusers where pin <> ''
================
Results in:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@viewName'.
Can this be done?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I haven't tested it yet (though I assume you have).
Are the key elements here:
1. Using sysname as the data type? and
2. Using the exec command?
And I can use similar commands to re-create the view?
Thanks much.
Are the key elements here:
1. Using sysname as the data type? and
2. Using the exec command?
And I can use similar commands to re-create the view?
Thanks much.
No I have not tested it.
1. You can use nchar(128) if you prefer.
2. Yes. The exec command in this case implies the use of Dynamic SQL. Which has other drawbacks, but in view (no pun intended) of your requirements permissions does not seem to be high on your priority list, and Dynamic SQL may be your only choice.
Yes. You should be able to, provided you have the correct permissions.
1. You can use nchar(128) if you prefer.
2. Yes. The exec command in this case implies the use of Dynamic SQL. Which has other drawbacks, but in view (no pun intended) of your requirements permissions does not seem to be high on your priority list, and Dynamic SQL may be your only choice.
Yes. You should be able to, provided you have the correct permissions.
ASKER
Thanks much -- you're a wiz -- that's exactly what I needed!
Set @ViewName = 'bbUserList'
IF EXISTS(
Select 1
from dbo.sysobjects
where id = object_id(@ViewName)
and OBJECTPROPERTY(id, N'IsView') = 1)
Print @ViewName
exec ('drop view ' + @ViewName)