Export images from a SQL Server using Stored procedures

后端 未结 1 951
孤独总比滥情好
孤独总比滥情好 2021-01-21 11:41

I\'m using Microsoft Sql server 2012 as DBMS, there I have my database which is containing informations about football players. Each player has a photography, a

1条回答
  •  野的像风
    2021-01-21 11:58

    try the following query:

    EXEC sp_configure 'show advanced options', 1 
    
    GO 
    RECONFIGURE 
    GO 
    EXEC sp_configure 'xp_cmdshell', 1 
    GO 
    RECONFIGURE 
    GO
    
    EXEC master..xp_cmdshell 'mkdir C:\ImagesFromSql'
    
    
    DECLARE @ID as int
    DECLARE @SQL as varchar(4000)
    
    DECLARE  csr  CURSOR FOR SELECT ID FROM [FootballTeam].[dbo].[Players]
    
    OPEN csr
    
    FETCH NEXT FROM csr INTO @ID
    
    WHILE @@FETCH_STATUS = 0 
    BEGIN
    
    
    SET @SQL = 'BCP "SELECT Photo FROM [FootballTeam].[dbo].[Players] WHERE ID = "' + CAST(@ID as varchar(10)) + ' queryout "C:\ImagesFromSql\' + CAST(@ID as varchar(10)) + '.jpg" -T -N'
    
    EXEC master..xp_cmdshell  @SQL
    
    FETCH NEXT FROM csr INTO @ID
    
    END
    
    
    CLOSE csr
    DEALLOCATE csr
    

    0 讨论(0)
提交回复
热议问题