Solved

How do I add columns into a Select Into table ?

Posted on 2008-10-16
14
193 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now