Solved

Error on if exists

Posted on 2011-02-13
4
264 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 46
Need help on t-sql 2012 10 53
SQL Server Designer 19 39
Tsql query 6 19
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

813 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

18 Experts available now in Live!

Get 1:1 Help Now