473,396 Members | 1,760 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.

Inserting to table with dashes in fieldnames


Hello,

I have had a request, one of our tables is used as a report and I have been
asked that all fieldnames for months have dashes in them,
like Jan-05 instead of Jan05 and so on...

Now what we have is a CURSOR which loops through all values in another table
generating these fieldnames, like 'Jan-05', Feb-05' etc..
Then the table definition is modified so these months display as fieldnames.

That is fine,
Except later it calls another stored procedure which inserts values from
another table into this one.
INSERT INTO TableName
SELECT *
FROM Tablename

Obviously this is failing because the newly amended table now has dashes in
the fieldnames now.

Now as the fieldnames are created dynamically I cannot simply do INSERT
Tablename ([Jan-05], [feb-5]) etc, as one would normally do,

How could I do this bearing in mind its not always going to be 12 months we
use,

Basically I am asking how can I modify this INSERT statement so it takes
into account the fieldnames with dashes in them
Bearing in mind I don't know what the fieldnames are called as they are
created dynamically by a CURSOR in a different routine.
And its not always 12 months.

have you any ideas?
and the dashes are required

Jayne
Jul 23 '05 #1
4 1997
Ray
Dynamically built field names is a bit unusual. But you will most likely
have to dynamically build the insert command and then execute it. Use the
INFORMATION_SCHEMA.COLUMNS view to define a cursor of column names you need.

Ray

"Little PussyCat" <SP******@NOSPAM.com> wrote in message
news:5s************@tiger.sphynx...

Hello,

I have had a request, one of our tables is used as a report and I have
been
asked that all fieldnames for months have dashes in them,
like Jan-05 instead of Jan05 and so on...

Now what we have is a CURSOR which loops through all values in another
table
generating these fieldnames, like 'Jan-05', Feb-05' etc..
Then the table definition is modified so these months display as
fieldnames.

That is fine,
Except later it calls another stored procedure which inserts values from
another table into this one.
INSERT INTO TableName
SELECT *
FROM Tablename

Obviously this is failing because the newly amended table now has dashes
in
the fieldnames now.

Now as the fieldnames are created dynamically I cannot simply do INSERT
Tablename ([Jan-05], [feb-5]) etc, as one would normally do,

How could I do this bearing in mind its not always going to be 12 months
we
use,

Basically I am asking how can I modify this INSERT statement so it takes
into account the fieldnames with dashes in them
Bearing in mind I don't know what the fieldnames are called as they are
created dynamically by a CURSOR in a different routine.
And its not always 12 months.

have you any ideas?
and the dashes are required

Jayne

Jul 23 '05 #2
Little PussyCat (SP******@NOSPAM.com) writes:
I have had a request, one of our tables is used as a report and I have
been asked that all fieldnames for months have dashes in them, like
Jan-05 instead of Jan05 and so on...

Now what we have is a CURSOR which loops through all values in another
table generating these fieldnames, like 'Jan-05', Feb-05' etc.. Then the
table definition is modified so these months display as fieldnames.
This is a design that I would strongly discourage use of. Month data
is best stored as rows in a subtable. Then if someone want to looks
at this with the rows as column, you define a view for him, but let
the underlying tables be.
That is fine,
Except later it calls another stored procedure which inserts values from
another table into this one.
INSERT INTO TableName
SELECT *
FROM Tablename

Obviously this is failing because the newly amended table now has dashes
in the fieldnames now.


There are two more cases of violations of good practice here:

1) Always list columns explicitly with INSERT:
2) Don't use SELECT * in production code.

Anyway, if you let this poor table be and define that view instead, the
problem will go away.

This may not be the answer you were looking for, but creating columns
dynamically is definitely not what relational databases are designed for,
and if you go there, it becomes very painful.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
This is a design that I would strongly discourage use of. Month data
is best stored as rows in a subtable. Then if someone want to looks
at this with the rows as column, you define a view for him, but let
the underlying tables be.


I have recently taken over from someone else in a job so for the time being
I am having to work with how the original code is written, plus I am
learning how all the systems hook together.
Yes, in my opinion I would have done it in a View but until I feel 100% sure
of how all the systems work I would rather work this way.

Oh, I solved my problem, the stored procedure was failing at the ALTER TABLE
Column command in that CURSOR,
It was failing because of the dashes in the fieldname, so all I have to do
is surround the fieldnames with square brackets when its creating the
column, like this

Exec ('ALTER TABLE TableName ADD' + '[' + @ColName + ']' + 'Money NULL')

And yes I know having dashes in fieldnames is a big No No, and normally I
wouldn't do it, later on when I feel more comfortable in my job I will
change this to a View then I can simply create Aliases for the Month Names

Thanks for your help anyway :-)

Regards,

Jayne
Jul 23 '05 #4
Little PussyCat (SP******@NOSPAM.com) writes:
I have recently taken over from someone else in a job so for the time
being I am having to work with how the original code is written, plus I
am learning how all the systems hook together. Yes, in my opinion I
would have done it in a View but until I feel 100% sure of how all the
systems work I would rather work this way.
I think that is a fallacy. You only run the risk to be let out on
lonely winding roads that you have no idea where they lead to or
even is what behind the next curve.
And yes I know having dashes in fieldnames is a big No No,


I wouldn't say so. After all, SQL is designed to accommodate this
possibility. I don't use it myself, as all those brackets or quotes
makes the code more bulky. I'd say the dashes is the smallest of your
problems.


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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

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

Similar topics

7
by: | last post by:
How do I get to the fieldnames of a table in a recordset? I have a recordset which I output into HTML. And on data from certian columns I need to perform different actions. I would like to...
8
by: Stephen Poley | last post by:
One disadvantage of using a simple text editor to produce HTML is that it is relatively time-consuming to put in the proper typographical quotation marks and dashes. A round tuit having arrived...
0
by: Ben | last post by:
PostgreSQL mavens, can I ask the database, in a normal database query command, or in other words, essentially using the same environment where I'd say... SELECT count(*) FROM mytable ; ....to...
6
by: Miles Keaton | last post by:
Is there a simple way to list fieldnames in a table, from PHP? When on the command-line, I just do \d tablename But how to get the fieldnames from PHP commands? ...
5
by: EiEiO | last post by:
Hi All, I am trying to create an Import Form to "Map Fields" for importing many different tables into 1. The form I created has 2 columns of comboboxes ( A - AA) thru (J - JJ). The...
13
by: gau.tai | last post by:
I have an Access 2003 database, protected by a workgroup, that I am trying to view through python. Currently, I'm attempting dao with the win32 package, and I'm able to view all of the table...
5
by: hmiller | last post by:
Hey there folks: I have been trying to get this work for about a week now. I'm new to VBA... I am trying to transfer a populated table in Access to an existing, but blank, Excel worksheet. I...
6
by: Gerrit | last post by:
Hello, I try to display the fieldnames and the primary key of a table, with this code: using System; using System.Collections.Generic; using System.Text; using System.Data; using...
5
by: maryanncanor | last post by:
Hi, I have an input excel file that needs to be imported to Table1. However, the fieldnames or header in the excel file doesnt match the fieldnames in the database. The fieldnames in the excel file...
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
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
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
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...
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.