SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic Stored Proc to generate Create Table Script ,taking table_name as parameter


Dynamic Stored Proc to generate Create Table Script ,taking table_name as parameter

Author
Message
Alkesh Khedle
Alkesh Khedle
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 61
Hi I am looking to make Dynamic Stored Proc to generate Create Table Script ,taking table_name as parameter.Is any system stored proc availabel in SQL Server2008 to do the same ?
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36414 Visits: 9518
There is a SQL Server 2008 forum where you might get a better answer for that part of your question.

As for SQL2005, although it is easy to do from SMO, Tables are one of the few common SQL objects that you cannot generate a script for with just SQL, right out of the box. There are some custom stored procedures around that might be sufficient for you, I'll let those more familiar with them comment on that.

The other way to go, would be to write a CLR UDF that just called SMO and returned the script. I have thought about writing such a Function, and might in the near future.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Alkesh Khedle
Alkesh Khedle
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 61
Hi
I Tried to use CLR UDF to achive the same but I didnt get the result .
Can I get the proper name and the procedure to use call the functions .
We need to activate some procedures through Surface Area configuration also to achive the same .Please send the proper doc .
Thansk in advance.
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36414 Visits: 9518
Well, I have to confess, I have not written a CLR UDF yet (I am hoping to write my first this week).

However, I have written a lot of SMO. Here is a utility function that should be easily adaptable to a CLR UDF:

Imports Microsoft.SqlServer
Imports Microsoft.SqlServer.Management
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Server
Imports system.Data
Module Util

Public Function ScriptTable(ByVal Table As Smo.Table, ByVal Options As Smo.ScriptingOptions) As String
' This function generates the script for an SMO.Table object based on
'the options specified and then returns is as a single concatenated
'string with CR/LF's between the lines.
'
' RBarryYoung@Gmail.com, 17-Mar-2008

'First, get the script
Dim scrLines As New Collections.Specialized.StringCollection
scrLines = Table.Script(Options)

'Now, turn the Script=String() into 1 String with CrLf's
Dim strScript As String = "", strLines() As String
If scrLines.Count > 0 Then
ReDim strLines(scrLines.Count - 1)
scrLines.CopyTo(strLines, 0)
strScript = Join(strLines, vbCrLf)
End If

Return strScript

End Function
End Module



-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Alkesh Khedle
Alkesh Khedle
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 61
Hi Barry thanks for the solution .
Can u plz tell me how to call these function in sql server. I never called SMO's in SQL Server yet.Is that required any assamblly to call or sm thing ??

Thanks
Alkesh K.
SQL Server DB Developer
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36414 Visits: 9518
Sorry, I haven't done SMO as CLR yet, just as a client call from a regular .net program.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
GrahamH
GrahamH
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 345
Hi,

SMO is currently not supported in CLR extended stored procedures. If you try to construct an extended stored proc to call SMO and install it into SQL 2005, you will get the error:


Assembly 'xxxxxxxx' references assembly 'microsoft.sqlserver.batchparser, version=9.0.242.0, culture=neutral, publickeytoken=89845dcd8080cc91.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(The system cannot find the file specified.)). Please load the referenced assembly into the current database and retry your request.


For which there is no workaround. Please refer to the Microsoft issue below, and add your vote to have Microsoft add this feature.

http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=126386
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36414 Visits: 9518
Yep, I did discover this about a month after I wrote this.

For an alternative solution, check this script out: http://www.sqlservercentral.com/scripts/Miscellaneous/30730/

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
yoosufshanij
yoosufshanij
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 11
http://yoosufshanij.blogspot.com/2011/08/how-to-dynamically-create-table-using.html
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search