?
Solved

Error on if exists

Posted on 2011-02-13
4
Medium Priority
?
267 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

770 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