|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
You can use the EXEC command to run dynamic SQL. For example:
Quote:
With a bit of imagination i reckon you would be able to build the table you want. Try using a cursor. |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Dynamic CREATE TABLE or SELECT INTO statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|