USE AdventureWorks2008;
GO
DECLARE
@Command
VARCHAR
(4000),
@PhotoID
INT
,
@ImageFileName
VARCHAR
(128)
DECLARE
curPhotoImage
CURSOR
FOR
SELECT
ProductPhotoID,
LargePhotoFileName
FROM
Production.ProductPhoto
WHERE
LargePhotoFileName !=
'no_image_available_large.gif'
OPEN
curPhotoImage
FETCH
NEXT
FROM
curPhotoImage
INTO
@PhotoID,
@ImageFileName
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET
@Command =
'bcp "SELECT LargePhoto FROM
AdventureWorks2008.Production.ProductPhoto WHERE ProductPhotoID = '
+
convert
(
VARCHAR
,@PhotoID) +
'" queryout "K:\data\images\productphoto\' +
@ImageFileName + '
" -T -n -SHPESTAR'
PRINT @Command – debugging
/* bcp
"SELECT LargePhoto FROM AdventureWorks2008.Production.ProductPhoto
WHERE ProductPhotoID = 69"
queryout
"K:\data\images\productphoto\racer02_black_f_large.gif"
-T -n -SHPESTAR
*/
EXEC
xp_cmdshell @Command
FETCH
NEXT
FROM
curPhotoImage
INTO
@PhotoID,
@ImageFileName
END
CLOSE
curPhotoImage
DEALLOCATE
curPhotoImage
/*
output
NULL
Starting copy...
NULL
1
rows
copied.
Network packet
size
(bytes): 4096
Clock
Time
(ms.) Total : 16 Average : (62.50
rows
per sec.)
NULL
.....
*/