I have the following stored procedure, how could I modify it to export multiple images at once? A TicketID can contain several images, could the IN statement work here? Appreciate all answers!
CREATE PROCEDURE dbo.ExportImage (
#ImgName NVARCHAR (100)
,#ImageFolderPath NVARCHAR(1000)
,#Filename NVARCHAR(1000)
)
AS
BEGIN
DECLARE #ImageData VARBINARY (max);
DECLARE #Path2OutFile NVARCHAR (2000);
DECLARE #Obj INT
SET NOCOUNT ON
SELECT #ImageData = (
SELECT convert (VARBINARY (max), ImgData, 1)
FROM Images
WHERE TicketID = #ImgName
);
SET #Path2OutFile = CONCAT (
#ImageFolderPath
,'\'
, #Filename
);
BEGIN TRY
EXEC sp_OACreate 'ADODB.Stream' ,#Obj OUTPUT;
EXEC sp_OASetProperty #Obj ,'Type',1;
EXEC sp_OAMethod #Obj,'Open';
EXEC sp_OAMethod #Obj,'Write', NULL, #ImageData;
EXEC sp_OAMethod #Obj,'SaveToFile', NULL, #Path2OutFile, 2;
EXEC sp_OAMethod #Obj,'Close';
EXEC sp_OADestroy #Obj;
END TRY
BEGIN CATCH
EXEC sp_OADestroy #Obj;
END CATCH
SET NOCOUNT OFF
END
This is the query I execute:
exec dbo.ExportImage '2042','C:\ExpImg','2042.jpg'
Errormsg:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression.
Something like that:
CREATE PROCEDURE dbo.ExportImage (
#ImgName NVARCHAR (100)
,#ImageFolderPath NVARCHAR(1000)
,#Filename NVARCHAR(1000)
)
AS
BEGIN
DECLARE #ImageData VARBINARY (max);
DECLARE #Path2OutFile NVARCHAR (2000);
DECLARE #Obj INT
SET NOCOUNT ON
DECLARE #cursor CURSOR
SET #cursor = CURSOR FOR
SELECT convert (VARBINARY (max), ImgData, 1)
FROM Images
WHERE TicketID = #ImgName
OPEN #cursor
FETCH NEXT
FROM #cursor INTO #ImageData
WHILE ##FETCH_STATUS = 0
BEGIN
SET #Path2OutFile = CONCAT (
#ImageFolderPath
,'\'
, #Filename
);
BEGIN TRY
EXEC sp_OACreate 'ADODB.Stream' ,#Obj OUTPUT;
EXEC sp_OASetProperty #Obj ,'Type',1;
EXEC sp_OAMethod #Obj,'Open';
EXEC sp_OAMethod #Obj,'Write', NULL, #ImageData;
EXEC sp_OAMethod #Obj,'SaveToFile', NULL, #Path2OutFile, 2;
EXEC sp_OAMethod #Obj,'Close';
EXEC sp_OADestroy #Obj;
END TRY
BEGIN CATCH
EXEC sp_OADestroy #Obj;
END CATCH
FETCH NEXT
FROM #cursor INTO #ImageData
END
CLOSE #cursor
DEALLOCATE #cursor
END
Related
I tried following query for export images to selective folder but it export only one file in one time but I want all image export at same time.
USE [DanfeyPhoto]
GO
/****** Object: StoredProcedure [dbo].[usp_ExportImage] Script Date: 7/31/2021 7:36:40 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
----- first ma database lai sql 2014 ma restore gareko xa vane matra yo query le kam garxa.
ALTER PROCEDURE [dbo].[usp_ExportImage] (
#PicName NVARCHAR (100)
,#ImageFolderPath NVARCHAR(1000)
,#Filename NVARCHAR(1000)
)
AS
BEGIN
DECLARE #ImageData VARBINARY (max);
DECLARE #Path2OutFile NVARCHAR (2000);
DECLARE #Obj INT
set #ImageFolderPath='D:\PhotoExtract\'
SET NOCOUNT ON
SELECT #ImageData = (
SELECT convert (VARBINARY (max), photoim, 1)
FROM MemberDetail
WHERE MemberNo = #PicName
);
SET #Path2OutFile = CONCAT (
#ImageFolderPath
,'\'
, #Filename
);
BEGIN TRY
EXEC sp_OACreate 'ADODB.Stream' ,#Obj OUTPUT;
EXEC sp_OASetProperty #Obj ,'Type',1;
EXEC sp_OAMethod #Obj,'Open';
EXEC sp_OAMethod #Obj,'Write', NULL, #ImageData;
EXEC sp_OAMethod #Obj,'SaveToFile', NULL, #Path2OutFile, 2;
EXEC sp_OAMethod #Obj,'Close';
EXEC sp_OADestroy #Obj;
END TRY
BEGIN CATCH
EXEC sp_OADestroy #Obj;
END CATCH
SET NOCOUNT OFF
END
-- EXEC [dbo].[usp_ExportImage] '00001', 'D:\PhotoExtract', '00001.jpg'
-- select * from MemberDetail
I am trying to export data from SQL Server to a file. The data is a .jpg. I found a script that would do so however whenever i run it I get a not any error message. and file not create in folder Please help me
DECLARE xArgsEntryImg CURSOR FOR
SELECT TOP 1
CONVERT(varbinary(max), E.ENTRY_IMAGE, 1) AS img1,
CONVERT(varchar(50), E.TKT_NO) + 'IMG1.jpg' AS img,
T.TicketNo
FROM
ITNLTCS_IMAGE.dbo.ICS_ENTRY_IMAGE E
INNER JOIN
fms25.dbo.tblTagTransaction T ON E.TKT_NO = T.TicketNo
INNER JOIN
FMS25.dbo.ReqPayViolationMessage m ON (T.TagTransactionPk = m.TagTransactionPk)
WHERE
NOT EXISTS (SELECT TOP 1 1
FROM tblImgUploadLog I
WHERE ImgType = 1
AND T.TicketNo = I.TicketNo)
DECLARE #PathForEntryImg nvarchar(max);
DECLARE #ImageDataForEntryImg varbinary(max);
DECLARE #FilenameForEntryImg NVARCHAR(max);
DECLARE #TicketNoForEntryImg bigint;
OPEN xArgsEntryImg
FETCH xArgsEntryImg INTO #ImageDataForEntryImg, #FilenameForEntryImg, #TicketNoForEntryImg
WHILE ##FETCH_STATUS = 0
BEGIN
BEGIN TRY
DECLARE #FullPathToOutputFileforEntryImg NVARCHAR(max);
DECLARE #FileName varchar(4000) = 'D:\FMS\IMAGES\CURRENT\a.jpg'
DECLARE #ObjectTokenEntryImg INT
EXEC sp_OACreate 'ADODB.Stream', #ObjectTokenEntryImg OUTPUT; -- Create Object
EXEC sp_OASetProperty #ObjectTokenEntryImg, 'Type', 1;
EXEC sp_OAMethod #ObjectTokenEntryImg, 'Open';
EXEC sp_OAMethod #ObjectTokenEntryImg, 'Write', NULL, #ImageDataForEntryImg;
EXEC sp_OAMethod #ObjectTokenEntryImg, 'SaveToFile', NULL, #FileName, 2;
EXEC sp_OAMethod #ObjectTokenEntryImg, 'Close';
EXEC sp_OADestroy #ObjectTokenEntryImg;
END TRY
BEGIN CATCH
END CATCH
FETCH NEXT From xArgsEntryImg INTO #ImageDataForEntryImg, #FilenameForEntryImg, #TicketNoForEntryImg
END
CLOSE xArgsEntryImg
DEALLOCATE xArgsEntryImg
I'm following the example on this page
https://www.sqlservercentral.com/forums/topic/using-msxml2-serverxmlhttp-within-stored-procedure-to-grab-source-of-html-page-and-save-to-table
I need to add a variable to the url. I have tried id='+ #PMIDLIST ' and also id='& #PMIDLIST ' but both cause errors. I have also tried
id=pmidList but that just does not return a valid response. I have also tried double quotes but that did not work.
These are the errors I got:
Incorrect syntax near '&'.
Incorrect syntax near '+'.
Code:
DECLARE #Object AS INT;
DECLARE #hr INT
DECLARE #json AS TABLE (Json_Table NVARCHAR(MAX))
DECLARE #pmidList NVARCHAR(MAX)
SET #PMIDLIST = '17784783,19505939,30166592'
EXEC #hr = sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', #Object OUT;
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr = sp_OAMethod #Object, 'open', NULL, 'get', 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?
db=pubmed&retmode=json&id='+ #PMIDLIST ' ;out;',
'false'
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr = sp_OAMethod #Object, 'send'
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr=sp_OAMethod #Object, 'responseText', #json OUTPUT
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
INSERT INTO #json (Json_Table)
EXEC sp_OAGetProperty #Object, 'responseText'
-- select the JSON string
SELECT * FROM #json
I figured out the answer I needed
Declare #Object as Int;
DECLARE #hr int
Declare #json as table(Json_Table nvarchar(max))
DECLARE #pmidList NVARCHAR(max)
SET #PMIDLIST = '17784783,19505939,30166592'
declare #url NVARCHAR(max)
SET #url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&id='+ #pmidList
Exec #hr=sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', #Object OUT;
IF #hr <> 0 EXEC sp_OAGetErrorInfo #Object
Exec #hr=sp_OAMethod #Object, 'open', NULL, 'get',
#url OUT,
'false'
Can't seem to find much information about how to attach a file that's been stored as a BLOB as part of an email.
I know you can attach files from the file system (C:\temp...) to email that are being setup in DBMAIL or custom stored procedures. However, I haven't seen any references to attaching something such as a PDF that's been stored as a binary object in a table.
I see where you can attach a query as a file, but I don't think that's what I'm looking for.
We would do this programatically via the application, but we need to be able to kick this SP via triggers or the application's server side code.
Is this possible or should i be asking other questions, since usually a binary object also needs to have a content type associated with it for browsers or mail objects to know how to handle?
The solution can not attach binary object stored in db field, you may change your schema a little bit store the path to the binary file. if you could enable .net clr in your database server, you will have more options.
use master
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_send_cdosysmail]')
and objectproperty(id, N'isprocedure') = 1)
drop procedure [dbo].[usp_send_cdosysmail]
go
create procedure usp_send_cdosysmail
#from varchar(500) ,
#to varchar(500) ,
#subject varchar(500),
#body nvarchar(max) ,
#smtpserver varchar(25),
#bodytype varchar(10) ,
#attachment varchar(100)= ' '
as
declare #imsg int
declare #hr int
declare #source varchar(255)
declare #description varchar(500)
declare #output varchar(1000)
exec #hr = sp_oacreate 'cdo.message', #imsg out
exec #hr = sp_oasetproperty #imsg,
'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").value','2'
exec #hr = sp_oasetproperty #imsg, 'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', #smtpserver
exec #hr = sp_oamethod #imsg, 'configuration.fields.update', null
exec #hr = sp_oasetproperty #imsg, 'to', #to
exec #hr = sp_oasetproperty #imsg, 'from', #from
exec #hr = sp_oasetproperty #imsg, 'subject', #subject
-- if you are using html e-mail, use 'htmlbody' instead of 'textbody'.
exec #hr = sp_oasetproperty #imsg, #bodytype, #body
-- Attachments...
IF #attachment IS NOT NULL AND LEN(#attachment) > 0 BEGIN
Declare #files table(fileid int identity(1,1),[file] varchar(255))
Declare #file varchar(255)
Declare #filecount int ; set #filecount=0
Declare #counter int ; set #counter = 1
DECLARE #outVar INT
SET #outVar = NULL
INSERT #files SELECT cValue FROM master..fn_split(#attachment,',')
SELECT #filecount=##ROWCOUNT
WHILE #counter<(#filecount+1)
BEGIN
SELECT #file = [file]
FROM #files
WHERE fileid=#counter
EXEC #hr = sp_OAMethod #imsg, 'AddAttachment',#outVar OUT, #file
SET #counter=#counter+1
END
END
exec #hr = sp_oamethod #imsg, 'send', null
-- sample error handling.
if #hr <>0
select #hr
begin
exec #hr = sp_oageterrorinfo null, #source out, #description out
if #hr = 0
begin
select #output = ' source: ' + #source
print #output
select #output = ' description: ' + #description
print #output
end
else
begin
print ' sp_oageterrorinfo failed.'
return
end
end
exec #hr = sp_oadestroy #imsg
go
set quoted_identifier off
go
set ansi_nulls on
go
sp_configure 'Ole Automation Procedures', 1
RECONFIGURE
GO
I previously was using SQL-DMO to automatically generate scripts from the database.
Now I upgraded to SQL Server 2008 and I don’t want to use this feature anymore since Microsoft will be dropping this feature off.
Is there any other alternative I can use to connect to a server and generate scripts automatically from a database?
Any answer is welcome. Thanks in advance.
This is the procedure i was previously using:
CREATE PROC GenerateSP (
#server varchar(30) = null,
#uname varchar(30) = null,
#pwd varchar(30) = null,
#dbname varchar(30) = null,
#filename varchar(200) = 'c:\script.sql'
)
AS
DECLARE #object int
DECLARE #hr int
DECLARE #return varchar(200)
DECLARE #exec_str varchar(2000)
DECLARE #spname sysname
SET NOCOUNT ON
-- Sets the server to the local server
IF #server is NULL
SELECT #server = ##servername
-- Sets the database to the current database
IF #dbname is NULL
SELECT #dbname = db_name()
-- Sets the username to the current user name
IF #uname is NULL
SELECT #uname = SYSTEM_USER
-- Create an object that points to the SQL Server
EXEC #hr = sp_OACreate 'SQLDMO.SQLServer', #object OUT
IF #hr <> 0
BEGIN
PRINT 'error create SQLOLE.SQLServer'
RETURN
END
-- Connect to the SQL Server
IF #pwd is NULL
BEGIN
EXEC #hr = sp_OAMethod #object, 'Connect', NULL, #server, #uname
IF #hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
ELSE
BEGIN
EXEC #hr = sp_OAMethod #object, 'Connect', NULL, #server, #uname, #pwd
IF #hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
--Verify the connection
EXEC #hr = sp_OAMethod #object, 'VerifyConnection', #return OUT
IF #hr <> 0
BEGIN
PRINT 'error VerifyConnection'
RETURN
END
SET #exec_str = 'DECLARE script_cursor CURSOR FOR SELECT name FROM ' + #dbname + '..sysobjects WHERE type = ''P'' ORDER BY Name'
EXEC (#exec_str)
OPEN script_cursor
FETCH NEXT FROM script_cursor INTO #spname
WHILE (##fetch_status <> -1)
BEGIN
SET #exec_str = 'Databases("'+ #dbname +'").StoredProcedures("'+RTRIM(UPPER(#spname))+'").Script(74077,"'+ #filename +'")'
EXEC #hr = sp_OAMethod #object, #exec_str, #return OUT
IF #hr <> 0
BEGIN
PRINT 'error Script'
RETURN
END
FETCH NEXT FROM script_cursor INTO #spname
END
CLOSE script_cursor
DEALLOCATE script_cursor
-- Destroy the object
EXEC #hr = sp_OADestroy #object
IF #hr <> 0
BEGIN
PRINT 'error destroy object'
RETURN
END
GO
Download the SQLServer2005_BC.msi from
http://www.microsoft.com/en-us/download/details.aspx?id=24793
then install DMO, also works with 2008/R2!
Are you aware that DMO has been replaced with SMO? Microsoft has no plans to discontinue this, that I'm aware of.
I would suggest you try SQL CRL Stored Procedure. See http://www.sqlteam.com/article/writing-clr-stored-procedures-in-charp-introduction-to-charp-part-1