Skip to main content

Export SPROC query to text file in SQL Server

sometime i have need to documented my Stored Procedured query

often i'm using sp_helptext SPROC_NAME then copy paste the result in notepad and save it. It consume alot of time if u using alot SPROC

found another way around using sqlcmd command, something like
DECLARE @sql AS NVARCHAR(1000)
SET @sql = 'sqlcmd -S (local) -d "DB_NAME" -q "sp_helptext SPROC_NAME" -o 'OUTPUT_FILE'

exec master..xp_cmdshell @sql

after some tweaking :D, i decided to wrap in SPROC
/* ===========================================================
Author: eRiCk WiDyA
Description: Export SPROC syntax to text file
Date: 10 Feb 2010

Usage: exec ExportSPROCToTextFile 'C:\', '[DB NAME]', 'sp_some%'
Param:
@FilePath : must exists first before run
@DatabaseName : use []
@SPROCCriteria : SPROC name that contains those words
===========================================================
*/

CREATE PROC ExportSPROCToTextFile

@FilePath AS NVARCHAR(50),
@DatabaseName AS NVARCHAR(50),
@SPROCCriteria AS NVARCHAR(50)

AS
SET NOCOUNT ON

DECLARE @sql AS NVARCHAR(1000)

SET @sql =
'USE ' + @DatabaseName + '

DECLARE @temp TABLE
(AutoID INT IDENTITY(1,1) PRIMARY KEY,
SPROCName NVARCHAR(50))

DECLARE @Init AS INT,
@Count AS INT,
@SPROCName AS NVARCHAR(50),
@Sql AS NVARCHAR (1000),
@DbName AS NVARCHAR (50)

SET @DbName = ''' + @DatabaseName + '''
SELECT @DbName = REPLACE(@DbName, ''['', '''')
SELECT @DbName = REPLACE(@DbName, '']'', '''')

SET @Init = 1
SET @Count = 0

INSERT INTO @temp (SPROCName)
SELECT ROUTINE_NAME
FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = ''PROCEDURE'' AND
PATINDEX(''' + @SPROCCriteria + ''', ROUTINE_NAME) <> 0

SELECT @Count = SUM(1) FROM @Temp

WHILE @Init <= @Count
BEGIN
SELECT @SPROCName = SPROCNAME
FROM @temp
WHERE AutoID = @Init

SET @Sql = ''sqlcmd -S (local) -d "'' + @DbName + ''" -q "sp_helptext '' + @SPROCName + ''" -o ' + @FilePath + ''' + @SPROCNAME + ''.sql''

EXEC xp_cmdshell @Sql

SET @Init = @Init + 1
END'

EXEC (@sql)

GO

PS: i think it for SQL SERVER 2005 and above

see u

Comments

Popular posts from this blog

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then

Could not load file or assembly error in VS 2010 and CR

when u received this kind of error "Could not load file or assembly 'file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified." try to add this to your .config file (usually app.config) PS: don't forget to remove extra space after " see u

SBO - Internal Error (-5002) Occured in Inventory Transfer

this is something that not well documented in SBO, it took me several hours to look at Google and can't find anything about it. Thank God friend of mine got answer for this one.So the Sympton is something like this, u need to make Inventory Transfer for Batch Item and Serial Item in one single Transaction so the Transaction is the combination of it. ex in detail lines: 1. Serial Item 2. Batch Item 3. Batch Item 4. Batch Item 5. Serial Item 6. Serial Item . When u reach this code oStockTransfer.Add it will return an error "Internal Error (-5002) Occured". This is because ur details is not in order, meaning u need to order the line with combination of Batch first then Serial or Serial first then Batch so be careful with this thing :D see u