2015-10-05 50 views
0

我需要附上對每個結果文件中查詢,可能是一個結果可能是10.使用Microsoft SQL Server 2012如何建立Sp_send_dbmail連接字符串從查詢結果

例如:select ItemNumber from table where column_x=22:

結果:

125487,

Declare @path nvarchar(255); 
Set @path = 'C:\Users\Public\Documents\' 

Declare @attachment nvarchar(255); 
    Set @attachment = @path + result1 + '.txt' + ',' + @path + result2 + '.txt' 

回答

-1
CREATE Procedure [dbo].[GetItems] 
@TransNumber INT, 
@attachment nvarchar(MAX) OUTPUT 
AS 

Begin 

DECLARE @path nvarchar(255); 
SET @path = 'C:\Users\Public\Documents\WineCards\'; 
set @attachment = '' 


SELECT @attachment += CONCAT(';', @path, 'wn', ItemLookupcode, '.pdf') 
FROM TransactionEntry 
Left Join Item on TransactionEntry.ItemID = Item.ID 
WHERE [email protected] and   dbo.fn_FileExists(CONCAT(@path, 'wn', ItemLookupcode, '.pdf'))=1 
Group by Itemlookupcode 

;

select @attachment = STUFF(@attachment,1,1,''); - 刪除最初的「;」

結束