Solved

Conditionally drop/create view using local variable

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete from table 6 48
Problem with SqlConnection 4 184
Whats wrong in this query - Select * from tableA,tableA 11 53
How can I use this function? 3 32
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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