Solved

Create dynamic 'Create Table' statement

Posted on 2012-04-13
6
237 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
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 39

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

746 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

12 Experts available now in Live!

Get 1:1 Help Now