473,547 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Appending strings in query to create table name?

19 New Member
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 alphanumericnam e). I'm not exactly sure how to append the alphanumericnam e column's data into the query dynamically to find the total amount of rows in it...

MGM out
Apr 18 '08 #1
4 1825
ck9663
2,878 Recognized Expert Specialist
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 alphanumericnam e). I'm not exactly sure how to append the alphanumericnam e 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 New Member
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 alphanumericnam e 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 Recognized Expert Specialist
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 alphanumericnam e 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.alphanumericn ame

-- CK
Apr 18 '08 #4
MGM
19 New Member
I would need:
1. The datatype of a.id
2. The actual fieldname of a.alphanumericn ame

-- 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
11194
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 1 which contains COLUMN1, COLUMN2, COLUMN3 ....
6
10213
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 don't want to do this in VB code). I've set up the ODBC connection string in the query properties, which has been tested and works:...
1
1999
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 database. The data in the USB has addtional info to be added to the respective record in the database. The problem is that the data is not appended to...
3
1902
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 many records. Two of the target fields in tblCorrespondence receiving values in the append operation are and . For any given VehicleJobID value, I...
7
2408
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 owner, Owner name/address, etc) TblInspection InspectionID
6
1308
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 activity assessment records each with the activity name field populated per student or group of students. i.e. Bob has no activity assessments...
7
9475
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 nodeToAppendTo = document.getElementById('tbody');
3
1750
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 a "table" stored with the C# front-end (not in the Access database), but don't know if this is even possible. If it is, can someone let me know...
0
2624
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 with the dropdown options (member names) pulled from the DB. When the client is editing a donation record, she needs to be able to view the correct...
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
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...
0
7703
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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...
1
7463
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...
0
7797
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...
0
6032
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...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.