473,770 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create table names by using macro variable? Thanks!

Greetings!

I am now doing one type of analysis every month, and wanted to creat
table names in a more efficient way.

Here is what happens now, everytime I do the analysis, I will create a
table called something like customer_200509 30, and then update the
table by using several update steps. Then next month I will create a
table called customer_200510 31. Does anyone know if there is a better
way to do it? like using a macro variable for the month-end date? Now
everytime I have to change the table name in every single update step,
which would cause errors if I forget to change one of them. By using a
macro, I would only need to change it once.

Thanks!

Oct 21 '05 #1
8 5768
This is how we programmed tape file systems in the 1950's. The IBM
convention was to have "yyddd" numbering on the tape labels.

In SQL, you would have one table, and build VIEWs from the appropriate
date column. The idea of an RDBMS is that you have a data model and
the tables represent entites in that model. Creating tables on the
fly says that you have no validate data model and in your Universe,
elephants fall out of the sky.

Oct 21 '05 #2
On 21 Oct 2005 10:24:10 -0700, ro******@gmail. com wrote:
Greetings!

I am now doing one type of analysis every month, and wanted to creat
table names in a more efficient way.

Here is what happens now, everytime I do the analysis, I will create a
table called something like customer_200509 30, and then update the
table by using several update steps. Then next month I will create a
table called customer_200510 31. Does anyone know if there is a better
way to do it?


Hi rong.guo,

Yes: add a column "Month" (or, better yet, two columns PeriodStart and
PeriodEnd) to your table, and add it to the primary key. I.e. if the
current primary key definition is

ALTER TABLE xxx
ADD PRIMARY KEY (DivisionID, ProductID)

you'll change it to

ALTER TABLE xxx
ADD PRIMARY KEY (PeriodStart, DivisionID, ProductID)
ALTER TABLE xxx
ADD UNIQUE (DivisionID, ProductID, PeriodEnd)

Then, in your code, add variables to hold PeriodStart and PeriodEnd and
use them in your queries. Next month, you'll only have to change the
values assigned to @PeriodStart and @PeriodEnd at the start of the
script.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Oct 21 '05 #3
Thank you both for your advise!!

I think i got myself a little bit confused... As I always use the
tables created by our DBA, I am not very familiar with
creating/altering tables, as well as the primary key (sorry). Here is
what i do now every month:

--At the end of September
select A, B (A B would change every month)
into customer_200509 30
from table_A (already in our database and will be updated monthly)
group by A, B

--At the end of October
select A, B (A B would change every month)
into customer_200510 31
from table_A (already in our database and will be updated monthly)
group by A, B

I am wondering how I can apply what you suggested to my current query?
Could you please show me the query?

Many thanks!
Hugo Kornelis wrote:
On 21 Oct 2005 10:24:10 -0700, ro******@gmail. com wrote:
Greetings!

I am now doing one type of analysis every month, and wanted to creat
table names in a more efficient way.

Here is what happens now, everytime I do the analysis, I will create a
table called something like customer_200509 30, and then update the
table by using several update steps. Then next month I will create a
table called customer_200510 31. Does anyone know if there is a better
way to do it?


Hi rong.guo,

Yes: add a column "Month" (or, better yet, two columns PeriodStart and
PeriodEnd) to your table, and add it to the primary key. I.e. if the
current primary key definition is

ALTER TABLE xxx
ADD PRIMARY KEY (DivisionID, ProductID)

you'll change it to

ALTER TABLE xxx
ADD PRIMARY KEY (PeriodStart, DivisionID, ProductID)
ALTER TABLE xxx
ADD UNIQUE (DivisionID, ProductID, PeriodEnd)

Then, in your code, add variables to hold PeriodStart and PeriodEnd and
use them in your queries. Next month, you'll only have to change the
values assigned to @PeriodStart and @PeriodEnd at the start of the
script.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Oct 22 '05 #4
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

What you did post makes no sense. There is no consistent definition of
these tables, since the coumns change from month to month. You cannot
compare them at all.

The GROUP BY can be replaced with a SELECT DISTINCT in your pseudo-code
There is no date value to use for the groupings. Etc. What are you
trying to do?

Oct 22 '05 #5
rola (ro******@gmail .com) writes:
I think i got myself a little bit confused... As I always use the
tables created by our DBA, I am not very familiar with
creating/altering tables, as well as the primary key (sorry). Here is
what i do now every month:

--At the end of September
select A, B (A B would change every month)
into customer_200509 30
from table_A (already in our database and will be updated monthly)
group by A, B

--At the end of October
select A, B (A B would change every month)
into customer_200510 31
from table_A (already in our database and will be updated monthly)
group by A, B

I am wondering how I can apply what you suggested to my current query?
Could you please show me the query?

If the tables can look different from month to month, then it is a little
more tricky to use one table with month as key. Of course, you can alter
the table to add or drop columns, but this may not be feasible.

I would suggest that the best is to use any text editor with a find-replace
functon to replace the strings. Since the queries change from to month, you
need to edit anyway.

The alternative is to use dynamic SQL, but that would make the script more
difficult to maintain.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Oct 22 '05 #6
--CELKO-- (jc*******@eart hlink.net) writes:
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

What you did post makes no sense. There is no consistent definition of
these tables, since the coumns change from month to month. You cannot
compare them at all.


Depends on exactly which columns he puts in, on how much he want to compare.

I have actually been playing this game recently. I've been running traces
at some customer sites, and to anaylse the trace, I say:

SELECT ...
INTO trc1024
FROM ::fn_get_trace_ table(...)

Thus, each trace gets a new table. Partly this is out of laziness, but
it could well be that the tables are different, if I decide to include
another column in the trace.

If I wanted to compare data over several days, this would be a difficult
setup to work with. But typically I'm mainly interested in the most recent
trace file, and the worst performance hogs in this trace. If I want to
compare data over several days, I can run a SELECT from a couple of tables
(rarely more than 3), since I'm usually only interested in one procedure
in such case.

The bottom line: don't do more work than you have to do!

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Oct 22 '05 #7
Since I have worked in a job oriented shop before, (i.e. each class action
suit we processed was unique in some way and had differing on-the-fly data
needs) - I am not as strident as -CELKO- is here, but there should be a
commonality of design and process designed into each round, with those
'special needs' handled in seperate side car tables.

Rather than change the procs / scripts each and every time you need to
change the tables, (and make cut/paste search/replace errors) -
I would recommend creating views before each analysis session that point to
the set of tables in process.

That way the procs/scripts can refer to the same abstract table names (and
they can even be tempoary views if possible).

If there are differing columns each set, the view would pick them up if you
create it with a "select * ".
"Erland Sommarskog" <es****@sommars kog.se> wrote in message
news:Xn******** **************@ 127.0.0.1...
rola (ro******@gmail .com) writes:
I think i got myself a little bit confused... As I always use the
tables created by our DBA, I am not very familiar with
creating/altering tables, as well as the primary key (sorry). Here is
what i do now every month:

--At the end of September
select A, B (A B would change every month)
into customer_200509 30
from table_A (already in our database and will be updated monthly)
group by A, B

--At the end of October
select A, B (A B would change every month)
into customer_200510 31
from table_A (already in our database and will be updated monthly)
group by A, B

I am wondering how I can apply what you suggested to my current query?
Could you please show me the query?

If the tables can look different from month to month, then it is a little
more tricky to use one table with month as key. Of course, you can alter
the table to add or drop columns, but this may not be feasible.

I would suggest that the best is to use any text editor with a
find-replace
functon to replace the strings. Since the queries change from to month,
you
need to edit anyway.

The alternative is to use dynamic SQL, but that would make the script more
difficult to maintain.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Oct 22 '05 #8
On 21 Oct 2005 21:35:46 -0700, rola wrote:

(snip)
I am wondering how I can apply what you suggested to my current query?
Could you please show me the query?


Hi rola,

No, I can't.

The only things you've shown here thus far are snippets of your current
solution. I could try to apply some band-aids to the spots where it
hurts most, but I'd much rather know the real problem and try to cure
that. It's possible that your current solution is indeed the best for
your particular problem - but it's also possible that there are better
ways to get the job done.

Can you post more information about your problem? I'd like to know the
structure of your tables (posted as CREATE TABLE statements; irrelevant
columns may be omitted, but please include all constraints and
properties), some rows of sample data to illustrate what your data
typically looks like (posted as INSERT statements) and a description of
the actual business problem that you're trying to solve.

Check out www.aspfaq.com/5006 for more details on the best way to ask
for help in these groups.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Oct 22 '05 #9

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

Similar topics

1
2636
by: Randy | last post by:
Access= 2002 I'm NOT a Programmer, but I have used VB in the past to do some things ( Spaghetti Code King) so I have some understanding of Coding I need to replace a text field (teacher) in Table (attendance) with a lookup fileld. the lookup field will point to a newly created table (teachers) the original DB was never intended to do the job that it is doing, but
6
6162
by: Marcus Kwok | last post by:
I am designing a GUI (my question is not about GUIs) and I have named my variables using a regular naming scheme. However, in order to simplify the code using these variables, I have created an array of non-owning pointers to these variables. I am trying to write a macro to generate these variable names for me, but I am not sure if what I want to do is possible. The code below demonstrates what I want to do (except it is generating...
27
3793
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
8
3481
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 , etc. I want to go to the first row, do a WHERE statement, return the
2
6909
by: Tom_F | last post by:
To comp.databases.ms-access: In the VBA code behind a form, I would like to create a string variable, composed of the names of the field(s) in the primary key of the table which is the "RecordSource" of the given form. For example, if my string variable is called "PriKeyFields", and the primary key of the RecordSource table is (Field01, Field02, Field03), then I would like PriKeyFields to be set to the string "Field01 Field02 Field03"....
4
12441
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
23
2373
by: mike3 | last post by:
Hi. (posted to both newsgroups since I was not sure of which would be appropriate for this question or how specific to the given language it is. If one of them is inappropriate, just don't send replies to it.) I'm making a bignum package for use in a program I've got (this is something different from the pi program you may have heard about). The package is going to support manipulating long floating point numbers.
3
2047
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
6
26329
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
9432
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10008
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
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
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.