Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Comment créer un document sur le serveur en exécutant une procédure stockée existante ou l'instruction sql de cette procédure sur un serveur sql R2008

     EXEC sp_configure 'show advanced options', 1
     GO
     -- To update the currently configured value for advanced options.
     RECONFIGURE
     GO
     -- To enable the feature.
     EXEC sp_configure 'xp_cmdshell', 1
     GO
     -- To update the currently configured value for this feature.
     RECONFIGURE   

    create procedure [dbo].[exportSubmissionsReport] 
    @login varchar(50) ='x',
    @password varchar(10) ='y$',
    @Server varchar(50) = 'my/instance', 
    @Database varchar(50) = 'database_DEMO', 
    @Statement varchar(5000),
    @fileName varchar(70) = 'TESD'

    as
    declare @query varchar(8000);
    set @query = 'sqlps.exe -command "invoke-sqlcmd ';
    set @query = @query [email protected]+ ' ';
    set @query = @query + '  -ServerInstance '[email protected]+' -Username ';
    set @query = @query + @login+' -Password '[email protected]+' -Database                     '[email protected]+'         |Export-CSV E:\NMCMS_DEMO\UploadedMedia\Reports\'[email protected]+'.csv"';
    execute xp_cmdshell @query