用SQL语句生成某个库下所有存储过程脚本,并每个存为一个文件

DECLARE @s VARCHAR(4000),@n INT,@i INT,@s1 VARCHAR(100)
SELECT IDENTITY(INT) id,text INTO ## 
    FROM syscomments
SELECT @n=@@ROWCOUNT,@i=0
WHILE @i<@n
    BEGIN
        SELECT @i=@i+1,@s=''
        SELECT @s1=REPLACE(REPLACE(RTRIM(LTRIM(STUFF(STUFF(text,CHARINDEX('AS',text),40000,''),1,CHARINDEX('PROC',STUFF(text,CHARINDEX('AS',text),40000,''))+4,''))),CHAR(10),''),CHAR(13),'')
            FROM ## WHERE ID=RTRIM(@i)
            --SELECT @s1,ASCII(SUBSTRING(@s1,3,1))
            --SELECT LEN(REPLACE(REPLACE(@s1,CHAR(13),''),CHAR(10),''))
        SELECT @s='SELECT text FROM tempdb.dbo.## WHERE ID=' + RTRIM(@i)
        EXEC('EXEC master..xp_cmdshell ''bcp "' + @s + ' "  queryout "e:ProcTXT/' + @s1 + '.txt" -S"pcnameSQLEXPRESS" -c -U"sa" -P"xxxx"''')
    END
DROP TABLE ##

你可能感兴趣的:(sql,sql,server,dba,select,所有存储过程脚本)