sql - Query to count table from all databases on server with WHERE clause -


i trying count cm.sites availability equal 1 (int).

i have modified query found online, running issues adding clause.

this have far, returns count of records in cm.sites

set nocount on;  if object_id (n'tempdb.dbo.#temp') not null    drop table #temp  create table #temp (       [count] int     , db varchar(50) )  declare @tablename nvarchar(50)  select @tablename = 'cm.sites'  declare @sql nvarchar(max) select @sql = stuff((     select char(13) + 'select ''' + name + ''', count(1) [' + name + '].' + @tablename     sys.databases      object_id(name + '.' + @tablename) not null , @tablename + '.' + 'availability' not null     xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')  insert #temp (db, [count])               exec sys.sp_executesql @sql  select *  #temp t 

this works since looking availability not null, when try change =1, error

conversion failed when converting nvarchar value 'cm.sites.availability' data type int.

i tried add cast select line couldn't figure out.

does know missing here?

i need query run , count each db on server.

you want change:

'availability' not null 

to:

'availability' =1 

what should have is:

'availability not null' 

which becomes:

'availability = 1' 

always confusing place quotes dynamic sql!


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -