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


Automate "Generate Scripts" in SQL Job


Automate "Generate Scripts" in SQL Job

Author
Message
tschmitt300
tschmitt300
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 53
I would like to create a SQL job that automatically runs the Generate Scripts process for certain Views, Functions and Stored Procedures.

A little background...we have a canned software package that uses an SQL 2005 database. I have created several views, functions and stored procedures in that database to accommodate some of our needs, reporting, etc. The problem is...whenever we apply an update from the software provider, all of my custom views etc get erased as part of their "upgrade" process. I then run my script from the Generate Scripts process to re-create everything.

As time has gone on, every time I make a change to something or add something new, I'm manually running the Generate Scripts option, then selecting the views, functions and stored procedures I want by hand. Now that we're up in the mid 80s on quantity of custom objects I've made, this is a bit of a pain in the butt to do every time. More of an annoyance than anything.

What I would like to do is create an SQL Server job that runs this for me daily. I know what objects I want to back up. Running a simple "SELECT * FROM dbo.sysobjects WHERE name LIKE 'KLL%' " shows me all of the objects I would want backed up, as all of my customizations I have prefixed with 'KLL'. I can also see that the xtype field in that table tells me what are views, functions, and procedures.

Is there some way I can create a script to run in a job that will run the Generate Scripts procedure, and output that result to a text file?

Your help is much appreciated.

Tony
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27912 Visits: 39921
well, here's a simple procedure that will return a table with the definitions of just the view/proc/function objects that begin with "KLL" in the correct dependancy order.

you could then create a scheduled job which calls bcp to output this to a single file.

if you have TABLEs that begin with KLL, i have a different version for that, but it's basicalyl the same.



CREATE PROCEDURE sp_export_KLLschema
AS
BEGIN
SET NOCOUNT ON
CREATE TABLE #MyObjectHierarchy
(
HID int identity(1,1) not null primary key,
ObjectId int,
TYPE int,OBJECTTYPE AS CASE
WHEN TYPE = 1 THEN 'FUNCTION'
WHEN TYPE = 4 THEN 'VIEW'
WHEN TYPE = 8 THEN 'TABLE'
WHEN TYPE = 16 THEN 'PROCEDURE'
WHEN TYPE =128 THEN 'RULE'
ELSE ''
END,
ONAME varchar(255),
OOWNER varchar(255),
SEQ int
)
--our results table
CREATE TABLE #Results(ResultsID int identity(1,1) not null,ResultsText varchar(max) )
--our list of objects in dependancy order
INSERT #MyObjectHierarchy (TYPE,ONAME,OOWNER,SEQ)
EXEC sp_msdependencies @intrans = 1

Update #MyObjectHierarchy SET ObjectId = object_id(OOWNER + '.' + ONAME)
--synonyns are object type 1 Function?!?!...gotta remove them
DELETE FROM #MyObjectHierarchy WHERE objectid in(
SELECT [object_id] FROM sys.synonyms UNION ALL
SELECT [object_id] FROM master.sys.synonyms)
--custom requirement: only objects starting with KLL
DELETE FROM #MyObjectHierarchy WHERE LEFT(ONAME,3) <> 'KLL'
DECLARE
@schemaname varchar(255),
@objname varchar(255),
@objecttype varchar(20),
@FullObjectName varchar(510)

DECLARE cur1 CURSOR FOR
SELECT OOWNER,ONAME,OBJECTTYPE FROM #MyObjectHierarchy ORDER BY HID
OPEN cur1
FETCH NEXT FROM cur1 INTO @schemaname,@objname,@objecttype
WHILE @@fetch_status <> -1
BEGIN
SET @FullObjectName = QUOTENAME(@schemaname) + '.' + QUOTENAME(@objname)
PRINT @FullObjectName
IF @objecttype IN( 'VIEW','FUNCTION','PROCEDURE')
BEGIN
INSERT INTO #Results(ResultsText)
EXEC sp_helptext @FullObjectName
--we need a batch seperator:
INSERT INTO #Results(ResultsText)
SELECT 'GO'

END
FETCH NEXT FROM cur1 INTO @schemaname,@objname,@objecttype
END
CLOSE cur1
DEALLOCATE cur1
SELECT ResultsText FROM #Results ORDER BY ResultsID
END
GO






Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

tschmitt300
tschmitt300
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 53
Hi Lowell...thanks for your reply. I had to do some research on the BCP part but got it to work. Thanks!

One thing...it does output the specs, but it's not exactly the same as running it manually. It looks like it puts everything together without an sp_executesql statement. For example:

This is the code generated from one particular function that is generated out of the Generate Scripts Function:

/****** Object: UserDefinedFunction [dbo].[KLLfn_Get_Week_Start_Date] Script Date: 11/22/2010 16:26:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[KLLfn_Get_Week_Start_Date]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'-- =============================================
-- Author: Tony Schmitt
-- Create date: 03/15/2010
-- Description: Return the starting day of a given week.
-- =============================================
CREATE FUNCTION [dbo].[KLLfn_Get_Week_Start_Date]
(
-- Add the parameters for the function here
@ParmDate DateTime
)
RETURNS DateTime
AS
BEGIN
-- Declare the return variable here
DECLARE @WkStartDate DateTime

-- Add the T-SQL statements to compute the return value here
SET @WkStartDate = (SELECT DATEADD(wk, DATEDIFF(wk, 6, @ParmDate), 6))

-- Return the result of the function
RETURN @WkStartDate

END
'
END
GO


This is from the process you provided me with:

-- =============================================

-- Author: Tony Schmitt

-- Create date: 03/15/2010

-- Description: Return the starting day of a given week.

-- =============================================

CREATE FUNCTION [dbo].[KLLfn_Get_Week_Start_Date]

(

-- Add the parameters for the function here

@ParmDate DateTime

)

RETURNS DateTime

AS

BEGIN

-- Declare the return variable here

DECLARE @WkStartDate DateTime



-- Add the T-SQL statements to compute the return value here

SET @WkStartDate = (SELECT DATEADD(wk, DATEDIFF(wk, 6, @ParmDate), 6))



-- Return the result of the function

RETURN @WkStartDate



END


It then goes to the next Create Function statement.

Basically, it looks like it's missing the step checking if it exists, then the execute statement. When I try running the new version, I get loads and loads of errors about variables etc not being defined.

Any ideas?
bjhogan
bjhogan
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 382
Have you tried doing this through Powershell? You can easily access the the SQL Server SMO to script out the objects to a file. I hadn't used powershell at all until this past Monday and within a few hours had a script that would dump the schema to a file. It's really really easy. And the code to do this is much more compact than using stored procedures.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84553 Visits: 41064
bjhogan (11/26/2010)
Have you tried doing this through Powershell? You can easily access the the SQL Server SMO to script out the objects to a file. I hadn't used powershell at all until this past Monday and within a few hours had a script that would dump the schema to a file. It's really really easy. And the code to do this is much more compact than using stored procedures.


Can you post the script, please?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Leo.Miller
Leo.Miller
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1018 Visits: 1525
In general knowing that most vendors don't like client messing with their DBs, I've used a different approach to this problem. I would rather add my own database to the environment and have my code in this database, just referencing the other database.

This way you don't run the risk of breaking support contracts, or of lossing your code.

Leo
WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9717 Visits: 10568
Jeff Moden (11/26/2010)
bjhogan (11/26/2010)
Have you tried doing this through Powershell? You can easily access the the SQL Server SMO to script out the objects to a file. I hadn't used powershell at all until this past Monday and within a few hours had a script that would dump the schema to a file. It's really really easy. And the code to do this is much more compact than using stored procedures.


Can you post the script, please?


Yes, this would be very useful. Perhaps even worthy of an article here on SSC!

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

bjhogan
bjhogan
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 382
I have attached the script. It is extremely basic, but you can build on it very easily. Actually, if you are familiar with Informix, they have a built-in tool called dbschema...that's where i got the name from. I'm going to try to mimic the tool as much possible as it's been one of the best command line tools I have ever used for dumping schemas.

You can see the full usage here: http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.mig.doc/mig170.htm
Attachments
dbschema.txt (211 views, 2.00 KB)
homebrew01
homebrew01
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4765 Visits: 9108
I use this to script all the objects to files. As I recall, the .exe is not in 2005, so I copied it, and maybe a couple others from my 2000 server: scptxfr.rll, scriptin.exe, sqlresld.dll.

I build a temp table with the databases I want to script, then loop through each database name:

--- Script out Database objects
set @code = '"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\scptxfr.exe" /s MyServerName ' + ' /d ' + @DatabaseName + ' /I /F '
+ '\\UNCServerToWriteTo\SQLBACKUP\MyServerName\Object_Scripts\' + @DatabaseName + '_structure_'+ convert(varchar(8),getdate(),112) + '' + ' /q /A /r'
--select @code
EXEC master..xp_cmdshell @code



Looks like it scripts pretty much all the database objects. I started using it as an extra layer of backup before we had change controls in place, and as a way to recover code without having to restore a 1Tb database.



Randy Doub
Randy Doub
Old Hand
Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)Old Hand (380 reputation)

Group: General Forum Members
Points: 380 Visits: 778
Why not create an SSIS package to create your objects in another DB?
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