Connecting Tech Pros Worldwide Help | Site Map

Appending strings in query to create table name?

MGM MGM is offline
Newbie
 
Join Date: Aug 2007
Posts: 19
#1: Apr 18 '08
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
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Apr 18 '08

re: Appending strings in query to create table name?


Quote:

Originally Posted by MGM

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
MGM MGM is offline
Newbie
 
Join Date: Aug 2007
Posts: 19
#3: Apr 18 '08

re: Appending strings in query to create table name?


Quote:

Originally Posted by ck9663

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
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#4: Apr 18 '08

re: Appending strings in query to create table name?


Quote:

Originally Posted by MGM

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
MGM MGM is offline
Newbie
 
Join Date: Aug 2007
Posts: 19
#5: Apr 21 '08

re: Appending strings in query to create table name?


Quote:

Originally Posted by ck9663

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
Reply