473,802 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Status Table

I have >200 tables and I want to create a table that lists the name of
each table, the number of records, and the number of locations within
the table.

I've created a cursor to do this but it doesn't like it. I get the
following error.

Invalid column name '<tablename>'.

Here's my script

DECLARE @tbl varchar(100)
DECLARE @sql varchar(1000)
-- Insert statements for procedure here
declare c_table cursor for
select table_name from INFORMATION_SCH EMA.TABLES where table_type =
'base table' order by table_name

open c_table
fetch next from c_table into @tbl

while (@@fetch_status = 0)
begin

set @SQL = 'INSERT INTO [zzTable_Status]
SELECT ('+ @tbl +') as tblname, count(distinct station__no),
count(station__ no)
FROM [bronze_views].'+@tbl+''

exec (@SQL)

Print @tbl + ' Updated'

fetch next from c_table into @tbl
end
close c_table
deallocate c_table

Any help is appreciated...

May 16 '06 #1
13 3044
SQL
try changing this
select table_name from INFORMATION_SCH EMA.TABLES where table_type =
'base table' order by table_name

to this

select quotename(table _name)
from INFORMATION_SCH EMA.TABLES where table_type =
'base table' order by table_name

you probably have a table name with a space in the name

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 16 '06 #2
Unfortunately, that didn't work. I still got the same error the table
just appear with brackets.

Any other ideas?

May 16 '06 #3
SQL
change exec to print and look at the code generated
If I change exec to print the cript runs without a problem
Do all table have this same column name station__no ?

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 16 '06 #4
To answer your question - Yes every table has that column. I think
what the problem is with the script is that I need to insert the name
of the table into the zzTable_Status table. This needs to be marked as
a string. So, '<tablename>' needs to be in the insert script.

Basically, I want the name of the table, number of station__no's in the
same table, and the number or records in the same table.

or another example

select 'cityname', count(distinct streets), count(streets) from
cityname.

What am I missing?

I hate it when you know what you want but you can't think of it.

I appreciate your help!

I have your blog as one of my rss feeds. I'll try to help your adsense
account. :-)

May 16 '06 #5
SQL
I see, you need triple quotes to store the table name
This should do it

set @SQL = 'INSERT INTO [zzTable_Status]
SELECT '''+ @tbl +''' , count(distinct station__no),
count(station__ no)
FROM [bronze_views].'+@tbl+''
Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 16 '06 #6
No dice! <darn it!>

I still get invalid object name '<table_name>' .

Let's try skinning this cat a different way. Do you have any
suggestions.

I want to create a table (or view) with the name of each of the tables
in the db, the number of times a location appears, and the total number
of records. I'm sure someone has done this before.

Any suggestions?

May 16 '06 #7
SQL
I have no problems running this in the pubs DB

use pubs

go
create table zzTable_Status (tblname varchar(600),Di stinctCount
int,RegularCoun t int)
Go

DECLARE @tbl varchar(100)
DECLARE @sql varchar(1000)
-- Insert statements for procedure here
declare c_table cursor for
select table_name from INFORMATION_SCH EMA.TABLES where table_type =
'base table' order by table_name
open c_table
fetch next from c_table into @tbl
while (@@fetch_status = 0)
begin
set @SQL = 'INSERT INTO [zzTable_Status]
SELECT ('''+ @tbl +''') as tblname, count(*),
count(*)
FROM .'+@tbl+''
exec (@SQL)
Print @tbl + ' Updated'
fetch next from c_table into @tbl
end
close c_table
deallocate c_table
select * from zzTable_Status
drop table zzTable_Status

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 16 '06 #8
I still get invalid object name errors.

I'm trying to do this in 2005. Maybe there is something I haven't set
right or something.

This is frustrating...

May 16 '06 #9
SQL
I know what the problem is it's the schema
This will run I just tried it on 2005
this is the change
select table_schema +'.' + table_name from INFORMATION_SCH EMA.TABLES
where table_type =
'base table' order by table_name
use adventureworks
go
create table zzTable_Status (tblname varchar(600),Di stinctCount
int,RegularCoun t int)
Go
DECLARE @tbl varchar(100)
DECLARE @sql varchar(1000)
-- Insert statements for procedure here
declare c_table cursor for
select table_schema +'.' + table_name from INFORMATION_SCH EMA.TABLES
where table_type =
'base table' order by table_name
open c_table
fetch next from c_table into @tbl
while (@@fetch_status = 0)
begin
set @SQL = 'INSERT INTO [zzTable_Status]
SELECT ('''+ @tbl +''') as tblname, count(*),
count(*)
FROM .'+@tbl+''
exec (@SQL)
Print @tbl + ' Updated'
fetch next from c_table into @tbl
end
close c_table
deallocate c_table
select * from zzTable_Status
drop table zzTable_Status

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6582
by: dev | last post by:
how create a temp table as a copy of a existing table and then update1 field and insert the hole temp table back in the existing table? please any help? if i have 10 fields in 1 record and about 100 records and a field.status=1 in a existing_table and i want to create a temp_table with all the recordse and values of the existing_table and then update the field.status to 2 and insert in 1 query the temp_table in the existing_table
0
1162
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
0
3778
by: HumanJHawkins | last post by:
Hi, I am trying to create an index from a stored procedure. The stored procedure runs without error, but when I try to use "CONTAINS" or "FREETEXT" on the table, it tells me that I can't because the table is not indexed. At the point of this code, a table called 'SearchCompareBase' is already created and full of data. The syntax I am using to index that table follows. Can anyone help me get it working:
2
5037
by: Rich Lussier | last post by:
access 2000 newbie here - I need to create a new table (make-table query) selecting from the following fields in a table: netname | IP_address | port_number | status | connection The records can contain duplicate information since the connection and port number fields will be different. Netname and IP_address can be duplicated - and I want to create a new table
6
2487
by: skgolden | last post by:
My husband and I own a small temporary labor company and deal with 4 major clients (A,B,C & D), each of which has about 2 dozen units in our tristate area that we deal with (ie, Company A, units a,b,c,d,e etc). We send temps possessing various skills (cashier, cook, hostess, etc) to the individual units as needed. What I'm trying to do is create a schedule we can use each day, for example: Monday, June 1, 2004 Company A, unit e Alice...
1
6290
by: md7546 | last post by:
Hi, first time posting to this group, but I have followed advice and examples for several months, so thank you. I work for a small telecommunications company. I have a simple Orders database that has an Orders table to track a sales order, using OrdersID as the PK. I have a related one-to-many table called OrderDetails, with the OrdersID as a FK, which shows all the products sold with that order. Then I have an Update table, in a...
2
3135
by: Shrutisinha | last post by:
Hi Guys Really need your help I donno what I am doing wrong in here Want to create a table with another existing table Here is the syntax I am using create table pctemp1 As (SELECT distinct a.Promo,b.Ban,b., b.,b. FROM PC_FUSION_070424 a
4
111188
by: Shrutisinha | last post by:
Hi Guys Really need your help I donno what I am doing wrong in here Want to create a table with another existing table Here is the syntax I am using create table pctemp1 As (SELECT distinct a.Promo,b.Ban,b., b.,b. FROM PC_FUSION_070424 a
1
1219
by: jiaudheen | last post by:
i have admin login form and status updation form. the admin get logged in and the update the status of the the customer smart card. the status page have a gridview which is bound to sqldatabase table which has fields.. 1. Application no. 2. apllication date. 3. customer name 4. status. ( in status column of the grid we have dropdowns holding 3 values. pending, produced, delivered). the status can be updated from the grid. now the...
3
7199
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know if there is a way to use the information in this table to create the many tables that are listed in the source table instead of creating each table individually? Many thanks for any help you can offer.
0
9561
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10281
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7597
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3789
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.