Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error on if exists

Posted on 2011-02-13
4
Medium Priority
?
271 Views
Last Modified: 2012-05-11
Hello,
I have an error on if exists command, I try to use @dbname variable :

IF exists (select *  from ' + ' @dbname + '.'+ dbo.sysobjects where name = 'syspublications')
Thanks
bibi
0
Comment
Question by:bibi92
[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
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 2000 total points
ID: 34883344
Try dynamic sql

declare @sql varchar(500)
set @sql = 'IF exists (select *  from ' +  @dbname + '.dbo.sysobjects where name = ''syspublications'')'
exec(@sql)
0
 

Author Comment

by:bibi92
ID: 34883361
Thanks a lot, it helps me to to this :
declare @sqlscript nvarchar(max)
set @sqlscript = 'IF exists (select *  from ' +  @dbname + '.dbo.sysobjects where name = ''syspublications'')
      BEGIN
            EXEC master.dbo.sp_replicationdboption @dbname =N''' + @dbname + ''',@optname = N''publish'', @value = N''false''
            end '
exec sp_executesql @sqlscript
Regards
bibi
0
 

Author Closing Comment

by:bibi92
ID: 34883364
Thanks

bibi
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34883381
Glad to help you
Raj
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

636 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