Solved

Conditionally drop/create view using local variable

Posted on 2008-10-18
5
1,092 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now