Solved

Conditionally drop/create view using local variable

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update a summary table with values from detail records 6 42
Format Date fields 11 64
Present Absent from working date rage 11 56
Replication failure 1 31
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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