Hello everyone,
I once again have a problem :p I need to write a query to get the total amount of rows in a table. Problem is, the table name is dynamic and part of its value is stored in another table. Here's my query: - SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM CDT_DEM_Pages AS a
-
ORDER BY name
What I'm trying to do is to count the total amount of rows in a table named CDT_DEM_DATA_[blank] where [blank] is a name like "test" that is stored in CDT_DEM_Pages as a column (named alphanumericname). I'm not exactly sure how to append the alphanumericname column's data into the query dynamically to find the total amount of rows in it...
MGM out
4 1816
Hello everyone,
I once again have a problem :p I need to write a query to get the total amount of rows in a table. Problem is, the table name is dynamic and part of its value is stored in another table. Here's my query: - SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM CDT_DEM_Pages AS a
-
ORDER BY name
What I'm trying to do is to count the total amount of rows in a table named CDT_DEM_DATA_[blank] where [blank] is a name like "test" that is stored in CDT_DEM_Pages as a column (named alphanumericname). I'm not exactly sure how to append the alphanumericname column's data into the query dynamically to find the total amount of rows in it...
MGM out
use dynamic query: -
declare @mytablename varchar(50)
-
-
set @mytablename = 'CDT_DEM_DATA' + '_Pages'
-
-
exec ('SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @mytablename + ' AS a
-
ORDER BY name')
-
-
set @mytablename = 'CDT_DEM_DATA' + '_NoPages'
-
-
exec ('SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @mytablename + ' AS a
-
ORDER BY name')
-
The values '_Pages' and '_NoPages' may be a variable.
You may also place the entire query in a variable - declare @strquery varchar(50)
-
-
set @strquery = 'SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @SomeVariableForYourTableName + ' AS a
-
ORDER BY name'
-
-
exec (@strquery)
-
-
Happy Coding...
-- CK
use dynamic query: -
declare @mytablename varchar(50)
-
-
set @mytablename = 'CDT_DEM_DATA' + '_Pages'
-
-
exec ('SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @mytablename + ' AS a
-
ORDER BY name')
-
-
set @mytablename = 'CDT_DEM_DATA' + '_NoPages'
-
-
exec ('SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @mytablename + ' AS a
-
ORDER BY name')
-
The values '_Pages' and '_NoPages' may be a variable.
You may also place the entire query in a variable - declare @strquery varchar(50)
-
-
set @strquery = 'SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM ' + @SomeVariableForYourTableName + ' AS a
-
ORDER BY name'
-
-
exec (@strquery)
-
-
Happy Coding...
-- CK
Well the problem with that is that the text that I need to append is not static and is actually a row inside another table (CDT_DEM_Pages). That is to say, CDT_DEM_Pages has a column named alphanumericname that has text in it, this text need's to be appended to the end of [CDT_DEM_DATA_] so that I can get the data I need. The bold part in the query below is what needs to be changed: - SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM CDT_DEM_Pages AS a
-
ORDER BY name
MGM out
Well the problem with that is that the text that I need to append is not static and is actually a row inside another table (CDT_DEM_Pages). That is to say, CDT_DEM_Pages has a column named alphanumericname that has text in it, this text need's to be appended to the end of [CDT_DEM_DATA_] so that I can get the data I need. The bold part in the query below is what needs to be changed: - SELECT id, name,
-
(SELECT COUNT(*) AS amtColumns
-
FROM CDT_DEM_Columns
-
WHERE (pageid = a.id)) AS amtColumns,
-
(SELECT COUNT(*) AS amtRows
-
FROM CDT_DEM_DATA_+a.alphanumericname
-
WHERE (pageid = a.id)) AS amtRows
-
FROM CDT_DEM_Pages AS a
-
ORDER BY name
MGM out
I would need:
1. The datatype of a.id
2. The actual fieldname of a.alphanumericname
-- CK
I would need:
1. The datatype of a.id
2. The actual fieldname of a.alphanumericname
-- CK
a.id is an integer and the identity column for a (CDT_DEM_Pages)
a.alphanumeric is an nvarchar(50) that is NOT NULL
MGM out
Sign in to post your reply or Sign up for a free account.
Similar topics
by: freeagent |
last post by:
Help!!
I am a complete Access newbie and I have to concatenate about 30
tables into one large table. All these tables have the same column
headings as each other.
For example, I have TABLE...
|
by: Access Newbie |
last post by:
I'm using Access 2000 and I'm trying to create a pass-through query to
append all the data from a local table to a remote table.
I'm using the SQL query editor in MS Access to create the query (I...
|
by: oasd |
last post by:
I'm having difficulty appending data.
I have an import macro (using the Transferspreadsheet function) to import
data from an excel spreadsheet (located in a USB attached to the pc) to an
access...
|
by: MLH |
last post by:
I have a query, qryAppend30DayOld260ies that attempts to append
records to tblCorrespondence. When run, it can result in any of the
following: appending no records, appending 1 record or appending...
|
by: PC Datasheet |
last post by:
Looking for suggestions ----
A database was designed for a national automobile inspection program. In
it's simplest form, the database has two tables:
TblOwner
OwnerID
<Year/Make/Model owned...
|
by: deejayquai |
last post by:
Hi
I'm still trying to solve my problem. I have two tables- students and
activity assessments, they're linked one-to-many. From a
lookup/combo/list I'd like to be able create a series of new...
|
by: Daz |
last post by:
Hi everyone!
Is it possible to take a line of text like so:
<tr><td>title1</td><td>title2</td><td>title3</td><td>title4</td></tr>
And append it to a DOM node such as this:
var...
|
by: Nathan Guill |
last post by:
I have an interface that works with an Access back-end. I would like to
store and/or load user defined query strings per each user (i.e. no user can
access another's queries). The idea I had was...
|
by: nbardach |
last post by:
Hope this finds everyone well. Happy New Year!
I'm trying to build a Loop for a set of dropdown menus I'm inserting into a form.
Basically, the client has to be able to select 1 to 20 donors...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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,...
| |