Solved

Conditionally drop/create view using local variable

Posted on 2008-10-18
5
1,109 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:stevengraff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22750933
Declare @ViewName sysname
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)
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 22750942
Let's try that again:

Declare @ViewName sysname

Set @ViewName = 'bbUserList'

IF EXISTS(
      Select      1
      from      dbo.sysobjects
      where      id = object_id(@ViewName)
                And OBJECTPROPERTY(id, N'IsView') = 1)
   Begin      
      exec ('drop view ' + @ViewName)
   End
0
 
LVL 11

Author Comment

by:stevengraff
ID: 22752540
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22754047
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.
0
 
LVL 11

Author Closing Comment

by:stevengraff
ID: 31507519
Thanks much -- you're a wiz -- that's exactly what I needed!
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question