Solved

How do I add columns into a Select Into table ?

Posted on 2008-10-16
14
194 Views
Last Modified: 2010-03-20
1.  In my query I Select Into a table that doesn't exist in my DB.  Will this table get created automatically?
2.  After the Select statement, how can I add 4 additional columns to it (code exmaple please).  
3.  Also, At some point in my query I also need to delete 2 columns in this table that was part of the Select Into statement's result.

thank you.
0
Comment
Question by:lapucca
  • 9
  • 5
14 Comments
 
LVL 18

Accepted Solution

by:
jmoss111 earned 500 total points
ID: 22736935
Hello lapucca,

The SELECT INTO creates the table if the table doesn't exist.
Do ALTER TABLE MyTable ADD MyCol VARCHAR(10)
Do ALTER TABLE MyTable DROP COLUMN MyCol


Regards,

Jim
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22736940
If the table exists, you'll get an error. If this process is recurring do DROP TABLE MyTable before SELECT INTO



Jim
0
 

Author Comment

by:lapucca
ID: 22736966
This sounds stupid, but is Do part of the code?
also, how do I check if the table exist then drop it at the begining of my query?  Thank you.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:lapucca
ID: 22736973
Also, after adding or deleting columns, do I need some kind of commit statement to save the changes to the table?  Thanks.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22736980
Nope nowherer I used it; no question is stupid except the unasked ones.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22736982
As soon as the SELECT INTO executes you're there
0
 

Author Comment

by:lapucca
ID: 22736998
So no commit statement if needed.
Thank you.
0
 

Author Comment

by:lapucca
ID: 22737004
Still I'm not sure here.  No commit statemetn is need for
ALTER TABLE ???
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22737025
Nope. If that was part of a TRANSACTION then it would require a BEGIN and COMMIT.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22737031
Missed one:

IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = 'MyTable')
   DROP TABLE MyTable
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22737035
Is this exercise inside a TRANSACTION?
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22737045
I'll be away for about 15 minutes. Don't think I've left this thread...

Jim
0
 

Author Comment

by:lapucca
ID: 22737055
No, it's not a transaction.  It's either a query or a stored procedure.  Thank you very much for your thourough help.  Really appreciate it.
0
 
LVL 18

Expert Comment

by:jmoss111
ID: 22737123
I'm glad that I have been able to help out a little.

Jim
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help - SELECT Statement 6 52
Please help for the below sql query. 1 24
Need help on t-sql 2012 10 54
T-SQL: Do I need CLUSTERED here? 13 42
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question