从SQL Server中的几个select语句返回特定值

时间:2018-12-10 11:48:47

标签: sql sql-server database subquery job-scheduling

选择中出现选择问题,以获得特定结果,但是我设法解决了这个问题,只是被另一个问题阻止了。

我有一个类似这样的代码:

IF EXISTS (SELECT * 
           FROM   bo 
           WHERE  nmdos LIKE '%preço%' 
                  AND datafinal = Dateadd(day, -6, CONVERT(DATE, Getdate()))) 
  EXEC msdb.dbo.Sp_send_dbmail 
    @profile_name = 'SQL Server - 2017', 
    @recipients = 'mail@mail.com', 
    @query = 'select * from bo     where nmdos like %preço%     and datafinal = DATEADD(day, -6, convert(date, GETDATE()))', 
    @subject = 'Encerramento Dossiert', 
    @attach_query_result_as_file = 1; 
ELSE 
  PRINT '0' 

我得到的错误是这样的:

  

信息22050,第16级,状态1,第0行   无法初始化错误编号为-2147467259的sqlcmd库。

如何使它正常工作,哪里出问题了?

1 个答案:

答案 0 :(得分:0)

您错过了@query变量中的引号

@query = 'select * from bo where nmdos like %preço% and datafinal = DATEADD(day, -6,convert(date, GETDATE()))'

您需要这个

@query = 'select * from bo where nmdos like'+' ''%preço%'' ' +'and datafinal = DATEADD(day, -6, convert(date, GETDATE()))' 

您可以看到此查询的不同之处:

 DECLARE @query NVARCHAR(MAX)
SET @query = 'select * from bo where nmdos like'+' ''%preço%'' ' +'and datafinal = DATEADD(day, -6, convert(date, GETDATE()))' 

select @query