如何在sql游标中传递多个参数

时间:2013-08-19 18:24:22

标签: sql-server-2008

declare @USERID varchar(50)
Declare @FileName Varchar(500)
declare @file_ext nvarchar(30)
Declare @exeVariable nvarchar(4000)
declare @file_type nvarchar(50)
declare @messageBody nvarchar(MAX)
declare @attach nvarchar(MAX)
declare @p_recipients varchar(MAX)
declare @EMAIL_ID  varchar(MAX)
declare @count float
set @file_type='PDF'
SET @file_ext='.pdf'

begin



Declare @USERID_Cursor Cursor
Set @USERID_Cursor = Cursor for 
SELECT DISTINCT empcode,email FROM EmailTable

Open @USERID_Cursor
Fetch NEXT from @USERID_Cursor into @USERID,@EMAIL_ID

While @@FETCH_STATUS = 0
BEGIN
SET @exeVariable = '[FilePath=C:\inetpub\wwwroot\WebServer\ProgramGuide\Report1.Stat]'
SET @exeVariable = @exeVariable +'[ConnStr=Provider=SQLOLEDB.1;Password=*abcdef;Persist          Security Info=True;User ID=abcd;Initial Catalog=OneKeyAgile;DataSource= ]'
SET @exeVariable = @exeVariable + '[NC_CountS=1][NC_ParaS=mcode][mcode='+@USERID+']     [NC_CountN=0][NC_ParaN=][NC_CountD=0][NC_ParaD=]'                                  
SET @exeVariable = @exeVariable + '[FormatTyp=PDF][ExportPath=F:\abcd\Report 1-'+@USERID+'.PDF] [StatWebServer=http://192.168.5.25/WebServer/Service.asmx]'
SET @exevariable = '"C:\"Program Files"\"ABCD"\Reporter.exe ' + @exevariable  +'"'

PRINT @exevariable
exec xp_cmdshell @exeVariable


SET @p_recipients = @EMAIL_ID
SET @attach = 'F:\BUDGET\Report1' + @USERID + @file_ext+''
SET @messageBody = ' '

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'ABCD', 
@recipients = @p_recipients,
@body = @messageBody,
@file_attachments = @attach
Fetch NEXT from @USERID_Cursor into @USERID,@EMAIL_ID
END
Close @USERID_Cursor
Deallocate @USERID_Cursor


end

好的,这是我的光标。我使用它将empcode从email_table传递给光标,然后光标将此参数传递给我的报告工具。这很好。

让我们说,我现在想要包含另一个来自email_table,bcode的变量,它也需要传递给游标。有什么帮助吗?

0 个答案:

没有答案