Solved

How do I add columns into a Select Into table ?

Posted on 2008-10-16
14
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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