DECLARE CustObject_Cursor CURSOR
KEYSET FORselect name,rtrim(LTRIM(Type)) from sysobjects where xtype<>'S'
DECLARE @ObjectName nvarchar(max) DECLARE @Type nvarchar(50) OPEN CustObject_Cursor FETCH NEXT FROM CustObject_Cursor INTO @ObjectName ,@TypeWHILE (@@fetch_status <> -1) BEGIN IF (@@fetch_status <> -2) BEGIN select top 1 text from syscomments where id=OBJECT_ID(@ObjectName)declare @cmd varchar(8000)
declare @path nvarchar(500)
set @path = 'G:\me_doc\'+DB_NAME()+'\'+@Type+'\'declare @cmdCreateDir varchar(500)
set @cmdCreateDir='if not exist '+@path+' md '+@path+''set @cmd = 'bcp "select top 1 text from '+DB_NAME()+'.sys.syscomments where id='+cast(OBJECT_ID(@ObjectName)as nvarchar)+' " queryout '+@path+@ObjectName+'.sql -c -T'
print @cmdexec master.dbo.xp_cmdShell @cmdCreateDir
exec master.dbo.xp_cmdShell @cmd END FETCH NEXT FROM CustObject_Cursor INTO @ObjectName ,@TypeEND CLOSE CustObject_Cursor DEALLOCATE CustObject_Cursor