SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Schema + Table name as a header


Schema + Table name as a header

Author
Message
NewBornDBA2017
NewBornDBA2017
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4110 Visits: 924
I am wondering if someone can help me out. I have tons of tables in my DBs and I am trying to find out the count where the column state is either NULL or has values like ESX which is in UK and not in United Sates and Canada.
select 'select count(*) from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'

So when I run this above command, it generates a query for me but when I run it, I get "No column name" but what I want is the schema+table as a header so I know the total count from each table but I can't seem to figure that out. This is what I did but of course it didn't work.

select 'select count(*) as '+s.name+'.'+t.name+' from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'

Michael L John
Michael L John
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22096 Visits: 10570
NewBornDBA2017 - Tuesday, March 6, 2018 11:51 AM
I am wondering if someone can help me out. I have tons of tables in my DBs and I am trying to find out the count where the column state is either NULL or has values like ESX which is in UK and not in United Sates and Canada.
select 'select count(*) from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'

So when I run this above command, it generates a query for me but when I run it, I get "No column name" but what I want is the schema+table as a header so I know the total count from each table but I can't seem to figure that out. This is what I did but of course it didn't work.

select 'select count(*) as '+s.name+'.'+t.name+' from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'

Change this:
'+s.name+'.'+t.name+'
To this:
['+s.name+'.'+t.name+']

Your alias is being interpreted as a column


Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)

Group: General Forum Members
Points: 401216 Visits: 43394
NewBornDBA2017 - Tuesday, March 6, 2018 11:51 AM
I am wondering if someone can help me out. I have tons of tables in my DBs and I am trying to find out the count where the column state is either NULL or has values like ESX which is in UK and not in United Sates and Canada.
select 'select count(*) from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'

So when I run this above command, it generates a query for me but when I run it, I get "No column name" but what I want is the schema+table as a header so I know the total count from each table but I can't seem to figure that out. This is what I did but of course it didn't work.

select 'select count(*) as '+s.name+'.'+t.name+' from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'


Have you tried this:

select 'select count(*) as '''+s.name+'.'+t.name+''' from '+s.name+'.'+t.name+' where '+c.name+ ' not in (''AB'',''AL'',''AK'',''AZ'',''AR'',''BC'',''CA'',''CO'',''CT'',''DC'',''DE'',''FL'',''GA'',''HI'',''ID'',''IL'',''IN'',''IA'',''KS'',''KY'',''LA'',''MB'',''ME'',''MD'',''MA'',''MI'',''MN'',''MS'',''MO'',''MT'',''NE'',''NL'',''NT'',''NS'',''NV'',''NH'',
''NJ'',''NM'',''NY'',''NC'',''ND'',''OH'',''OK'',''ON'',''OR'',''PA'',''PE'',''QC'',''RI'',''SC'',''SD'',''SK'',''TN'',''TX'',''UT'',''VT'',''VA'',''WA'',''WV'',''WI'',''WY'',''GU'',''PR'',''VI'',''YT'')'
from sys.schemas s
INNER JOIN sys.tables t on s.schema_id=t.schema_id
INNER JOIN sys.columns c on t.object_id=c.object_id
where c.name = 'State'
and t.name like '%Contact'


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search