473,386 Members | 1,598 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,386 software developers and data experts.

Variable table name in SQL

I want to write a query where one of the two tables in it is variable and
its name is picked up from a text box control on an open form. The query is
very simple and is only a select query with three fields, but the problem is
that one of its tables varies with the Calendar Year like tblYear2002,
tblYear2003, etc.

Is there a simple way of doing this?

dixie
Nov 12 '05 #1
4 13747
> I want to write a query where one of the two tables in it is variable and
its name is picked up from a text box control on an open form. The query is
very simple and is only a select query with three fields, but the problem is
that one of its tables varies with the Calendar Year like tblYear2002,
tblYear2003, etc.

Is there a simple way of doing this?


There is no simple way of doing things the hard way. You shouldn't have data
separated into separate tables like that based on any attribute. You should have
all the records in one table and filter the results of your query to return only
those records you require.

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Nov 12 '05 #2
Like this?

DoCmd.RunSQL ("SELECT * from " & me!cboName & " WHERE thisistrue";)

dixie wrote:
I want to write a query where one of the two tables in it is variable and
its name is picked up from a text box control on an open form. The query is
very simple and is only a select query with three fields, but the problem is
that one of its tables varies with the Calendar Year like tblYear2002,
tblYear2003, etc.

Is there a simple way of doing this?

dixie


--
*** Remove SPLAT to email directly ***

Nov 12 '05 #3

Well, I am not sure if there are situations where you may need variable
table names, however below you will find a simple script where you can
really use variable table name!! I modified a script I found at
microsoft to simply run a select query for all user tables in a given
database. Hope this is of any use.
/*
SET NOCOUNT ON

DECLARE @TableName varchar(255),@execstr VARCHAR (255)

DECLARE TableName_cursor CURSOR
FOR SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES where
Table_Type='BASE TABLE'
OPEN TableName_cursor

FETCH NEXT FROM TableName_cursor INTO @TableName

WHILE @@FETCH_STATUS=0
BEGIN
set @execstr='select * from '+ @TableName
EXEC (@execstr)
FETCH NEXT FROM TableName_cursor INTO @TableName
END

CLOSE TableName_cursor

DEALLOCATE TableName_cursor
*/

gwarning! wrote:
Like this?

DoCmd.RunSQL ("SELECT * from " & me!cboName & " WHERE thisistrue";)

dixie wrote:
I want to write a query where one of the two tables in it is variable and its name is picked up from a text box control on an open form. The query is very simple and is only a select query with three fields, but the problem is that one of its tables varies with the Calendar Year like tblYear2002, tblYear2003, etc.

Is there a simple way of doing this?

dixie


--
*** Remove SPLAT to email directly ***


Nov 13 '05 #4

Well, I am not sure if there are situations where you may need variable
table names, however below you will find a simple script where you can
really use variable table name!! I modified a script I found at
microsoft to simply run a select query for all user tables in a given
database. Hope this is of any use.
/*
SET NOCOUNT ON

DECLARE @TableName varchar(255),@execstr VARCHAR (255)

DECLARE TableName_cursor CURSOR
FOR SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES where
Table_Type='BASE TABLE'
OPEN TableName_cursor

FETCH NEXT FROM TableName_cursor INTO @TableName

WHILE @@FETCH_STATUS=0
BEGIN
set @execstr='select * from '+ @TableName
EXEC (@execstr)
FETCH NEXT FROM TableName_cursor INTO @TableName
END

CLOSE TableName_cursor

DEALLOCATE TableName_cursor
*/

gwarning! wrote:
Like this?

DoCmd.RunSQL ("SELECT * from " & me!cboName & " WHERE thisistrue";)

dixie wrote:
I want to write a query where one of the two tables in it is variable and its name is picked up from a text box control on an open form. The query is very simple and is only a select query with three fields, but the problem is that one of its tables varies with the Calendar Year like tblYear2002, tblYear2003, etc.

Is there a simple way of doing this?

dixie


--
*** Remove SPLAT to email directly ***


Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid =...
3
by: Geoff Winsor | last post by:
Hi, I am experiencing a problem with recalling a session variable which stores whether a person is logged in to a "members only" section of a website. This area of the site has been working...
1
by: farooqazeem | last post by:
Hi guys, I’m facing some problem can u solve it. Problem is: I’m giving user Id and password in (Login_sess.asp) and submit it to page (sess_test.asp). I am setting session variable...
4
by: Andre Arpin | last post by:
I am new at sql so would appreciate some help I have the name of a table in alocal variable is it possible to select this table DECLARE @name sysname SET @name = 'tblSniffedItems' PRINT...
1
by: stephane | last post by:
I have a problem which must be in this : print" <script type='text/javascript'> document.location.replace('http://127.0.0.1/add_task.php?req_id={$maxValue}&tk_request_name={$req_name}');...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
2
by: Kevin | last post by:
I am having difficulty updating a variable page-time-stamp in the following snippit. The variable time-stamp is initialized from the attribute time-stamp from the log element. Some of the page...
3
by: adiel_g | last post by:
Hello everyone, I am trying to move a field name to a variable in vb.net. For example, first I retrieve the record from the database and save its value: .... userGroup =...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
9
by: Slickuser | last post by:
Hi, I'm trying to use my variable in another variable. How can I achieve that? I can use + (concat) but it will be a lot me to concatenate. I have to use 3 variables with 15 replacement. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...

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.