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

use column names and not positions when inserting from one table into another

/*Code below raises following errors:
Server: Msg 245, Level 16, State 1, Line 6
Syntax error converting the varchar value 'a' to a column of data type
int.
*/

create table #x (i integer, c char(1))
create table #y (c char(1), i integer)

insert into #x VALUES (1, 'a')

insert into #y SELECT * from #x
drop table #x
drop table #y
--CODE COMPLETE

Is there a way to use the column names as the basis for the insert as
opposed to column position? The key here is that I DO NOT want to
SPECIFY COLUMN NAMES (i.e. 'insert into #y (i,c) SELECT * from #x')
Regards,

Ty
Jul 20 '05 #1
3 3466
You could build the INSERT statement programmatically as a string and then
execute it in an EXEC statement but I wouldn't really recommend it. I
suggest you *always* specify the column names in an INSERT statement and
don't use SELECT * in production code - in the long run it makes your code
much easier to maintain.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
>> Is there a way to use the column names as the basis for the insert
as opposed to column position? <<

You did not bother to look at the syntax of an INSERT INTO statement.
You just put a list of columns after the table name. Those columns
which are not listed will be given their default values. This is the
way you should have been writing production code all along.
Jul 20 '05 #3
Tyler Hudson (ty****@allpax.com) writes:
/*Code below raises following errors:
Server: Msg 245, Level 16, State 1, Line 6
Syntax error converting the varchar value 'a' to a column of data type
int.
*/

create table #x (i integer, c char(1))
create table #y (c char(1), i integer)

insert into #x VALUES (1, 'a')

insert into #y SELECT * from #x
drop table #x
drop table #y
--CODE COMPLETE

Is there a way to use the column names as the basis for the insert as
opposed to column position? The key here is that I DO NOT want to
SPECIFY COLUMN NAMES (i.e. 'insert into #y (i,c) SELECT * from #x')


No.

And in my opinion it is bad coding practice to use SELECT * and INSERT
without a column list. It's OK to do if the table in question is a
temp table created in the same stored procedure, but for a permanent
table it is a no-no.

The reason is that if the DBA makes what appears to be a harmless change
sucb as adding a nullable column, he will break your code if you say
things like:

INSERT tbl
SELECT a, b, c FROM othertbl

If you specify the column you are inserting into, your code is safe.

Likewise, using SELECT * can cause nasty surprises.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

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

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

Similar topics

14
by: Sanjay Minni | last post by:
What is the datatype to be used for Primary Key columns for most optimised access given that - There will be a single column primary key only - The values will only be integers (but as...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
2
by: Piotr Kurpiel | last post by:
Hello, I am using MS Access 2003 with VBScript. I have quite strange column names which are: 1,2,3,4,etc... Data Types of the columns are Yes/No and formats True/False. The first thing I want to...
2
by: David Veeneman | last post by:
How can I set a bound DataGridView control to use a dataset table's column captions, instead of column names? I'm working with a DataGridView control, which I have bound to a table in a dataset....
1
by: Brett Magill | last post by:
Hello all, Looking for some help here. BTW, to e-mail me directly, should you prefer, take the *nomail*. out the reply address. I uploaded data from another program ( GNU R-2.2.0 @...
1
by: krk77 | last post by:
Hi, I need to write a select statement where I select the columns by their positions rather than by their names. Let me know if this is possible and how? Eg. Select 1,2,3 from table (where 1...
2
by: lildiapaz | last post by:
Can someone please explain to me how to select a specific column in an excel spreadsheet and import the information in that column to an sql table in a c# windows application. Let's say my excel...
6
by: Henry J. | last post by:
I have a composite index on two columns in a table. However, the index is not used in a query that restricts the 2nd column to a constant. If both columns are linked with columns in other join...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...

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.