473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Appending strings in query to create table name?

19
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT id, name,
  2.       (SELECT   COUNT(*) AS amtColumns
  3.       FROM      CDT_DEM_Columns
  4.       WHERE     (pageid = a.id)) AS amtColumns,
  5.       (SELECT   COUNT(*) AS amtRows
  6.       FROM      CDT_DEM_DATA_+a.alphanumericname
  7.       WHERE     (pageid = a.id)) AS amtRows
  8. FROM CDT_DEM_Pages AS a
  9. 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
Apr 18 '08 #1
4 1816
ck9663
2,878 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT id, name,
  2.       (SELECT   COUNT(*) AS amtColumns
  3.       FROM      CDT_DEM_Columns
  4.       WHERE     (pageid = a.id)) AS amtColumns,
  5.       (SELECT   COUNT(*) AS amtRows
  6.       FROM      CDT_DEM_DATA_+a.alphanumericname
  7.       WHERE     (pageid = a.id)) AS amtRows
  8. FROM CDT_DEM_Pages AS a
  9. 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:
Expand|Select|Wrap|Line Numbers
  1. declare @mytablename varchar(50)
  2.  
  3. set @mytablename = 'CDT_DEM_DATA' + '_Pages'
  4.  
  5. exec ('SELECT id, name,
  6.       (SELECT   COUNT(*) AS amtColumns
  7.       FROM      CDT_DEM_Columns
  8.       WHERE     (pageid = a.id)) AS amtColumns,
  9.       (SELECT   COUNT(*) AS amtRows
  10.       FROM      CDT_DEM_DATA_+a.alphanumericname
  11.       WHERE     (pageid = a.id)) AS amtRows
  12. FROM ' + @mytablename + '  AS a
  13. ORDER BY name')
  14.  
  15. set @mytablename = 'CDT_DEM_DATA' + '_NoPages'
  16.  
  17. exec ('SELECT id, name,
  18.       (SELECT   COUNT(*) AS amtColumns
  19.       FROM      CDT_DEM_Columns
  20.       WHERE     (pageid = a.id)) AS amtColumns,
  21.       (SELECT   COUNT(*) AS amtRows
  22.       FROM      CDT_DEM_DATA_+a.alphanumericname
  23.       WHERE     (pageid = a.id)) AS amtRows
  24. FROM ' + @mytablename + '  AS a
  25. ORDER BY name')
  26.  
The values '_Pages' and '_NoPages' may be a variable.


You may also place the entire query in a variable

Expand|Select|Wrap|Line Numbers
  1. declare @strquery varchar(50)
  2.  
  3. set @strquery = 'SELECT id, name,
  4.       (SELECT   COUNT(*) AS amtColumns
  5.       FROM      CDT_DEM_Columns
  6.       WHERE     (pageid = a.id)) AS amtColumns,
  7.       (SELECT   COUNT(*) AS amtRows
  8.       FROM      CDT_DEM_DATA_+a.alphanumericname
  9.       WHERE     (pageid = a.id)) AS amtRows
  10. FROM ' + @SomeVariableForYourTableName + '  AS a
  11. ORDER BY name'
  12.  
  13. exec (@strquery)
  14.  
  15.  
Happy Coding...

-- CK
Apr 18 '08 #2
MGM
19
use dynamic query:
Expand|Select|Wrap|Line Numbers
  1. declare @mytablename varchar(50)
  2.  
  3. set @mytablename = 'CDT_DEM_DATA' + '_Pages'
  4.  
  5. exec ('SELECT id, name,
  6.       (SELECT   COUNT(*) AS amtColumns
  7.       FROM      CDT_DEM_Columns
  8.       WHERE     (pageid = a.id)) AS amtColumns,
  9.       (SELECT   COUNT(*) AS amtRows
  10.       FROM      CDT_DEM_DATA_+a.alphanumericname
  11.       WHERE     (pageid = a.id)) AS amtRows
  12. FROM ' + @mytablename + '  AS a
  13. ORDER BY name')
  14.  
  15. set @mytablename = 'CDT_DEM_DATA' + '_NoPages'
  16.  
  17. exec ('SELECT id, name,
  18.       (SELECT   COUNT(*) AS amtColumns
  19.       FROM      CDT_DEM_Columns
  20.       WHERE     (pageid = a.id)) AS amtColumns,
  21.       (SELECT   COUNT(*) AS amtRows
  22.       FROM      CDT_DEM_DATA_+a.alphanumericname
  23.       WHERE     (pageid = a.id)) AS amtRows
  24. FROM ' + @mytablename + '  AS a
  25. ORDER BY name')
  26.  
The values '_Pages' and '_NoPages' may be a variable.


You may also place the entire query in a variable

Expand|Select|Wrap|Line Numbers
  1. declare @strquery varchar(50)
  2.  
  3. set @strquery = 'SELECT id, name,
  4.       (SELECT   COUNT(*) AS amtColumns
  5.       FROM      CDT_DEM_Columns
  6.       WHERE     (pageid = a.id)) AS amtColumns,
  7.       (SELECT   COUNT(*) AS amtRows
  8.       FROM      CDT_DEM_DATA_+a.alphanumericname
  9.       WHERE     (pageid = a.id)) AS amtRows
  10. FROM ' + @SomeVariableForYourTableName + '  AS a
  11. ORDER BY name'
  12.  
  13. exec (@strquery)
  14.  
  15.  
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT id, name,
  2.       (SELECT   COUNT(*) AS amtColumns
  3.       FROM      CDT_DEM_Columns
  4.       WHERE     (pageid = a.id)) AS amtColumns,
  5.       (SELECT   COUNT(*) AS amtRows
  6.       FROM      CDT_DEM_DATA_+a.alphanumericname
  7.       WHERE     (pageid = a.id)) AS amtRows
  8. FROM CDT_DEM_Pages AS a
  9. ORDER BY name
MGM out
Apr 18 '08 #3
ck9663
2,878 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT id, name,
  2.       (SELECT   COUNT(*) AS amtColumns
  3.       FROM      CDT_DEM_Columns
  4.       WHERE     (pageid = a.id)) AS amtColumns,
  5.       (SELECT   COUNT(*) AS amtRows
  6.       FROM      CDT_DEM_DATA_+a.alphanumericname
  7.       WHERE     (pageid = a.id)) AS amtRows
  8. FROM CDT_DEM_Pages AS a
  9. ORDER BY name
MGM out
I would need:
1. The datatype of a.id
2. The actual fieldname of a.alphanumericname

-- CK
Apr 18 '08 #4
MGM
19
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
Apr 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
6
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...
1
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...
3
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...
7
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...
6
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...
7
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...
3
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
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,...

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.