Solved

Conditionally drop/create view using local variable

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article I will describe the Copy Database Wizard 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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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