Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problem with scripting objects through query


Problem with scripting objects through query

Author
Message
Divya Agrawal
Divya Agrawal
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 604
Comments posted to this topic are about the item Problem with scripting objects through query

--Divya
vitaly.maslevskiy
vitaly.maslevskiy
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 6
I would change query a little


select 'DROP PROCEDURE ['+ si.name + '].[' + so.name + ']'+ CHAR(13)+CHAR(10)+'GO'+CHAR(13)+CHAR(10)+
OBJECT_DEFINITION(object_id)+ CHAR(13)+CHAR(10)+'GO'+CHAR(13)+CHAR(10)

from sys.objects so inner join sys.schemas si ON so.schema_id = si.schema_id
where so.type='P'




1. Add brackets to procedure name if proc name is "non standard"
2. Add join with sys.schemas for procedures not in dbo/current user schema
Scott Coleman
Scott Coleman
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3088 Visits: 1442
You don't need carriage return for a valid end-of-line sequence, only line feed. Drop the CHAR(13) and just use CHAR(10) and it works fine.



Chris Harshman
Chris Harshman
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3138 Visits: 3503
I typically just use a bunch of UNION ALL statements in my script building scripts
Scott Coleman
Scott Coleman
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3088 Visits: 1442
The line breaks can be included in the literal strings, so you don't need to worry about which characters are used. You do have to put " ' + ' " in front of any GO however.

SELECT 'DROP PROCEDURE [' + SCHEMA_NAME([schema_id]) + '].[' + [name] + ']
' + 'GO
' + OBJECT_DEFINITION([object_id])+ '
' + 'GO'
FROM sys.objects
WHERE [name] = 'Test SP'





ankitwaitshere
ankitwaitshere
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 46
.. All fine... but Why do I need OBJECT_DEFINITION(1234567) there...?? Is this to create the stored proc back?

If so, it may not be possible as the function OBJECT_DEFINITION(1234567) returns only first few hundred chars I think...

Thoughts??
AUSteve
AUSteve
Old Hand
Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)Old Hand (344 reputation)

Group: General Forum Members
Points: 344 Visits: 178
ankitwaitshere (10/30/2009)
.. All fine... but Why do I need OBJECT_DEFINITION(1234567) there...?? Is this to create the stored proc back?

If so, it may not be possible as the function OBJECT_DEFINITION(1234567) returns only first few hundred chars I think...

Thoughts??


According to BOL, OBJECT_DEFINITION returns nvarchar(max) in both SQL2005 and SQL2008 so it should be fine.
Linson.Daniel
Linson.Daniel
Old Hand
Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)Old Hand (321 reputation)

Group: General Forum Members
Points: 321 Visits: 391
Then again....would someone tell me why would I go about generating scripts like the way that was shown here when SQL server itself provides all the necessary options to script your db objects?
ankitwaitshere
ankitwaitshere
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 46
Select datalength(object_definition(669622570))


returns only 28700 - might be for this object with above ID.

But when I display the value of
Select object_definition(669622570)


It just gives me the first few lines from the stored proc. Thoughts?
ankitwaitshere
ankitwaitshere
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 46
Linson.Daniel (11/1/2009)
Then again....would someone tell me why would I go about generating scripts like the way that was shown when SQL server itself provides all the necessary options to create your db objects?


May be if someone wants scripts for many objects all in one shot... not sure if SQL-Server does that for many objects at a time. Does it?
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