我正在尝试创建一个变量来保存一个值,然后我将使用该变量值从表中进行选择。这在第一次完美运行,但每当我尝试更改参数以更新变量时,它都会返回空白。我可以使用具有不同参数的相同变量吗?还是我的代码有问题?
就像我说的,这个方法用了 1 次,然后如果我Print是变量,那么它返回空白。
这是我的变量按预期工作的示例。
DECLARE @ReportTitle as VARCHAR(100) SELECT @ReportTitle = ti.Title FROM dbo.tblSales ti WHERE ti.Title = (SELECT REPLACE(T1.Title, 'Sale', '') FROM dbo.tblSales t1 WHERE t1.IdCountry = 34 AND t1.IdReport = 3375 AND t1.IdLanguage= 1 and t1.btInUse = 1 ) PRINT @ReportTitle SELECT i.IdCountry as Country, i.Title as ReportTitle, i.Report as IndustryID FROM dbo.tblSales i WHERE i.IdCountry = 49 AND i.IdLanguag = 1 and i.btInUse = 1 AND i.Title LIKE '%' + @ReportTitle +'%'
现在,如果我运行完全相同的查询但更改AND t1.IdReport = 3375为AND t1.IdReport = 3595打印@ReportTitle返回空并且我编写的查询不返回任何行。
AND t1.IdReport = 3375为AND t1.IdReport = 3595
@ReportTitle
这是一个不起作用的例子:
我的预期结果是根据IdReportI pass使用变量来保存标题。
IdReportI pass
例如,第一次围绕变量 printsRetail这是我所期望的,然后我搜索包含%Retail%
printsRetail
%Retail%
第二次我使用IdReport= 3595运行查询,它返回 title Appointments。如果我打印@ReportTitle,当我期望它等于时它不等于Appointments
IdReport= 3595
title Appointments
Appointments
能够弄清楚。这不是数据问题,所以有点棘手。轮到我可能有一个不需要的额外选择。
这是我的工作代码的代码示例:
DECLARE @ReportTitle as VARCHAR(100) (SELECT @ReportTitle = REPLACE(T1.Title, 'Sale', '') FROM dbo.tblSales ti WHERE t1.IdCountry = 34 AND t1.IdReport = 214 AND t1.IdLanguage= 1 and t1.btInUse = 1 ) SELECT i.IdCountry as Country, i.Title as ReportTitle, i.Report as IndustryID FROM dbo.tblSales i WHERE i.IdCountry = 21 AND i.IdLanguag = 1 and i.btInUse = 1 AND i.Title LIKE '%' + @ReportTitle +'%'