Solved

Error on if exists

Posted on 2011-02-13
4
263 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
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now