[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

Error on if exists

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
bibi92
Asked:
bibi92
  • 2
  • 2
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
Try dynamic sql

declare @sql varchar(500)
set @sql = 'IF exists (select * ¬†from ' +  @dbname + '.dbo.sysobjects where name = ''syspublications'')'
exec(@sql)
0
 
bibi92Author Commented:
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
 
bibi92Author Commented:
Thanks

bibi
0
 
Rajkumar GsSoftware EngineerCommented:
Glad to help you
Raj
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now