Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqlpal2007
Posting Yak Master
200 Posts |
Posted - 2008-10-15 : 15:54:49
|
Hello All,I get a syntax error while running the following query.There a cursor to go through all the databases from different servers (subscribers and publishers). The cursor sets server name and database names to @SubscriberServer and @SubscriberDatabase respectively. insert into execreplcmdselect 'insert into repldbsubscriber select ' + ''''+ dest_table + '''' + ',' + ' count(*) from ' + @SubscriberServer + '.' + @SubscriberDatabase + '.dbo.' + dest_table + ' where + convert (varchar(2000), filter_clause) from ' + @SubscriberServer + '.' + @SubscriberDatabase + '.dbo.' + 'sysarticles' + where filter != 0 Can some one help me?Thanks in advance,-P |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-15 : 18:25:31
|
| [code]select'insert into repldbsubscriber select dest_table , count(*) from '+@SubscriberServer+'.'+@SubscriberDatabase+'.dbo.dest_table where convert (varchar(2000), filter_clause) from '+@SubscriberServer+'.'+@SubscriberDatabase+'.dbo.sysarticles where filter != 0 '[/code] |
 |
|
|
sqlpal2007
Posting Yak Master
200 Posts |
Posted - 2008-10-15 : 19:00:28
|
| Thanks hanbingl.That worked -:)-P |
 |
|
|
sqlpal2007
Posting Yak Master
200 Posts |
Posted - 2008-10-16 : 00:30:18
|
Sorry Hanbingl. The query compiled successfully. but the results were wrong.The result was insert into repldbsubscriber select dest_table , count(*) from [servername].[databasename].dbo.dest_table where convert (varchar(2000), filter_clause) from [servername].[databasename].dbo.sysarticles where filter != 0 dest_table is not a table it is a column in sysarticles table.My query runs correctly if I hardcode the server name and database name. Following is a query. I get an error when I get server and database from variable through cursor as I have to do it on few servers.select 'insert into repldbsubscriber select ' + ''''+ dest_table + '''' + ',' + ' count(*) from [myserver].[mydatabase].dbo.' + dest_table + ' where ' + convert (varchar(2000), filter_clause) from [myserver].[mydatabase].dbo.sysarticleswhere filter != 0 Following is the resultset. It shows all the articles as rows from sysarticles table.insert into repldbsubscriber select 'Employees', count(*) from [myserver].[mydatabase].dbo.Employees where (ID IN (1,2))insert into repldbsubscriber select 'Departments', count(*) from [myserver].[mydatabase].dbo.Departments where (ID IN (1,2))ETC....Please let me know where I am going wrong in dynamic sql.Thanks,-P |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sqlpal2007
Posting Yak Master
200 Posts |
Posted - 2008-10-16 : 09:00:27
|
Hi tkizerIf you see code in my previous post the query runs if I hard code server and database names Here is a full codecreate table replcmds( execstatement varchar(2000))create table replvalsubscriber( tablename varchar(60), reccount int)DECLARE @SubscriberServer VARCHAR(100), @SubscriberDatabase VARCHAR(100)DECLARE ReplSites CURSOR FOR SELECT SubscriberServer1, SubscriberDatabase1 FROM ReplicationDatabases --to get server and databaseOPEN ReplSitesFETCH NEXT FROM ReplSites INTO @SubscriberServer, @SubscriberDatabaseWHILE (@@FETCH_STATUS <> -1)BEGINinsert into replcmdsselect 'insert into replvalsubscriber select ' + ''''+ dest_table + '''' + ',' + ' count(*) from ' + @SubscriberServer + '.' + @SubscriberDatabase + '.dbo.' + dest_table + ' where + convert (varchar(2000), filter_clause) from ' + @SubscriberServer + '.' + @SubscriberDatabase + '.dbo.' + 'sysarticles' + where filter != 0DECLARE getarticles CURSOR FOR SELECT execstatement FROM replcmdsDECLARE @execstatement varchar(2000);OPEN getarticlesFETCH NEXT FROM getarticles INTO @execstatementWHILE (@@FETCH_STATUS <> -1)BEGIN EXEC ( @execstatement) FETCH NEXT FROM getarticles INTO @execstatementENDFETCH NEXT FROM ReplSites INTO @SubscriberServer, @SubscriberDatabaseENDCLOSE getarticlesDEALLOCATE getarticlesCLOSE ReplSitesDEALLOCATE ReplSitesGO Following is an errorMsg 156, Level 15, State 1, Line 24Incorrect syntax near the keyword 'where'. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|