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... 13 3044
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/
Unfortunately, that didn't work. I still got the same error the table
just appear with brackets.
Any other ideas?
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/
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. :-)
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/
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?
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/
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...
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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:
|
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
|
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...
| |
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...
|
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
|
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
|
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...
|
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.
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |