MS SQL 'de Binary Verinin Dosya Olarak Çıkarılması
c:\> bcp.exe "SELECT BINARY_COLUMN_NAME FROM INSTANCE_NAME.dbo.TABLE_NAME" queryout "C:\FILE_NAME" -T -n';
SQL Sorgusundan;
DECLARE @bcp_cmd1 VARCHAR(4000);
SET @bcp_cmd1 ='bcp.exe "SELECT BINARY_COLUMN_NAME FROM INSTANCE_NAME.dbo.TABLE_NAME" queryout "C:\FILE_NAME" -T -n';
PRINT @bcp_cmd1;
exec xp_cmdshell @bcp_cmd1;
GO
Dosya İsmini Kolon İsminden Almak için Örnek;
DECLARE @y VARCHAR(500)
DECLARE @id VARCHAR(100)
DECLARE @x VARCHAR(100)
DECLARE @Command NVARCHAR(4000)
DECLARE @fileName VARCHAR(4000)
DECLARE cAttachments CURSOR LOCAL FOR select a.id,x,y from TABLE_NAME
OPEN cAttachments
FETCH NEXT FROM cAttachments INTO @id, @x,@y
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @id+'_'+@x+@y
SET @Command = 'bcp.exe "select dokuman from INSTANCE_NAME.dbo.TABLE_NAME a, INSTANCE_NAME.dbo.SECOND_TABLE_NAME c where a.id=c.id and a.id='''+@id+''' and x='+@x+'" queryout "C:\DATA\'+@fileName +'" -T -n'
PRINT @Command
EXEC xp_cmdshell @Command
FETCH NEXT FROM cAttachments INTO @id, @x,@y
END
CLOSE cAttachments
DEALLOCATE cAttachment
Dikkat edilmesi gerekenler;
1. Dizine yazma yetkisi olduğundan emin olun yoksa "Unable to open BCP host data-file" hatası alırsınız.
2. bcp.exe nin PATH değişkeninde tanımlı olduğundan emin olun.