Solved

Create dynamic 'Create Table' statement

Posted on 2012-04-13
6
245 Views
Last Modified: 2012-04-13
Hello,
I have this logic below where it gets the columns from a table - 'Proc_Layouts' and there is a while loop that puts these columns together, and at the end, uses these columns to create a table.
I had it working one time, where it did select the columns and create the string - 'col1 varchar(10), col2 varchar(20), col3(varchar2), .....)
But when I added the last statement - 'Create table ' +
It doesn't even print it or retrieve the columns.  Am I out of scope somewhere or is there a better way to write this.
The 'Print @pSQL' statement doesn't seem to work and the result or message page just shows:
'(42 row(s) affected)'
Not showing the DDL statement that is supposed to be shown.
Thanks,
Steve

  Declare @pSQL    varchar(8000)  
, @pTypeKey   int  
, @pEXTColumns    varchar(8000)  
, @FieldOrder   int  
, @ColumnName   varchar(80)  
, @CurrentVariable  Varchar(10)  
, @FieldLength   varchar(4)
,@ud_ext_temp_table varchar(40)

  Set @pTypeKey = 1711
  Set @ud_ext_temp_table = 'My_EXT_Table'
 
 Declare @columns table  
   (  
    FieldOrder int,  
    columnname varchar(80),  
    fieldlength varchar(5)  
   )  
 
   Insert Into @columns (FieldOrder, ColumnName, FieldLength)  
   Select FieldOrder, fieldname, fieldlength  
   From proc_Layouts  
   Where  LayoutKey = @pTypeKey  
 
   Set @FieldOrder = (Select Min(FieldOrder) From @columns)  
 
   While @FieldOrder Is Not Null  
      Begin  
      Select      @ColumnName  = IsNull(columnname,'colxxx'),
            @FieldLength = IsNull(FieldLength, '80')
      From      @columns  
      Where      FieldOrder = @FieldOrder  
             
      Set @pEXTColumns = @pEXTColumns + ', '+@ColumnName+ ' VarChar(' + @FieldLength + ') '  
             
      Set @FieldOrder = (Select Min(fieldorder) From @columns Where fieldorder > @fieldorder)  

      End  
Set @pEXTColumns = right(@pEXTColumns, len(@pEXTColumns)) -- remove the first character (comma) of this string..  

set @pSQL = 'Create Table ' + @ud_ext_temp_table + ' (' + @pEXTColumns + ')'

print @pSQL
0
Comment
Question by:Machinegunner
[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
6 Comments
 
LVL 14

Expert Comment

by:nishant joshi
ID: 37844133
might you are having an end less while loop try to print in while loop and check.

Regards,
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37844157
can you select @pSQL . .. does that return anything?
0
 

Author Comment

by:Machinegunner
ID: 37844427
Thanks for the replies, the @pSQL is coming back as NULL, when I do a 'Select @pSQL' and looks like some kind of endless loop somewhere?
Should I reinitialize @FieldOrder somewhere or another variable, to kick it out of the endless loop?
Thanks,
Steve
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
vasto earned 500 total points
ID: 37844461
Your @pEXTColumns variable has value null and since there is no place where this value is initialized
Set @pEXTColumns = @pEXTColumns + ', '+@ColumnName+ ' VarChar(' + @FieldLength + ') '  
will always set the value to null

add
set @pEXTColumns=''
somewhere at the beginning of the script ( for example after Set @pTypeKey = 1711)
0
 
LVL 18

Expert Comment

by:vasto
ID: 37844495
also you don't need cursor and temp table . try this code intead:

Declare @pSQL varchar(8000), @pTypeKey int, @ud_ext_temp_table varchar(40)
SELECT @pTypeKey = 1711, @ud_ext_temp_table = 'My_EXT_Table'

SELECT @pSQL = isnull(@pSQL + ',' ,'') + '[' +fieldname + '] VarChar(' + fieldlength  + ') '
FROM proc_Layouts  
WHERE  LayoutKey = @pTypeKey  
ORDER BY fieldorder

SET @pSQL = 'Create Table ' + @ud_ext_temp_table + ' (' + @pSQL + ')'
print @pSQL
0
 

Author Closing Comment

by:Machinegunner
ID: 37845177
Thanks for the solution as the setting of the @pEXTColumns = '' in the beginning, worked and created the DDL statement just fine.
Thanks also for the other logic, for creating without a cursor.
Much appreciated!
Thanks,
Steve
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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