MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 27th, 2004, 09:00 AM
tOeJaM tOeJaM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: United States
Posts: 46 tOeJaM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 55 sec
Reputation Power: 6
Dynamic CREATE TABLE or SELECT INTO statement

In SQL Server you can do a SELECT INTO to create a new table, much like CREAT TABLE AS in Oracle. I'm putting together a dynamic script that will create a table with the number of columns being the dynamic part of my script. Got any suggestions that come to mind?

Example:

I need to count the number of weeks between two dates, my columns in the table need to be at least one for every week returned in my query.

I'm thinking of getting a count of the number of weeks then building my column string comma separated then do my CREATE TABLE statement rather then the SELECT INTO... But I'm not sure I'll be able to do that using a variable that holds the string of column names. I'm guess the only way I can do this is via either VBScript or VB rather then from within the database.

BTW - this would be a stored procedure...

Any suggestions would be greatly appreciated.
__________________
_________________________
Dual Intel P3 1ghz CPU's
ABIT VP6 dual FC-PGA
720,896mb ECC PC100 RAM
128meg ATI Radeon 9500 Pro
Antec Performance II
White Box Linux r3.0
kernel 2.4.21-9.0.3ELsmp

Reply With Quote
  #2  
Old August 2nd, 2004, 03:00 AM
pickledshark pickledshark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Blackpool
Posts: 22 pickledshark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 32 sec
Reputation Power: 0
Send a message via ICQ to pickledshark Send a message via MSN to pickledshark
You can use the EXEC command to run dynamic SQL. For example:

Quote:
DECLARE @sql VarChar(2000)
DECLARE @table VarChar(100)

SET @table = 'Monkeys'

SET @sql = 'CREATE TABLE '+@table+'
(
MonkeyID Int
Bananas Int
)'

With a bit of imagination i reckon you would be able to build the table you want. Try using a cursor.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Dynamic CREATE TABLE or SELECT INTO statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway