我试图创建一个变量来保存一个值,然后我将使用该变量值从表中进行选择。这是第一次完美运行,但每当我试图更改参数以更新变量时,它就会返回空白。我能使用不同参数的相同变量吗?还是我的代码有问题?: b6 T/ U1 y+ k2 ^
正如我所说,这种方法使用 1 次,如果我Print如果是变量,它会回到空白。 ! D( K1 ~ {- m7 }5 U" y! k4 Q+ Q这是我的变量按预期工作的例子。 R/ V3 X3 P6 |; W" x6 BDECLARE @ReportTitle as VARCHAR(100)SELECT @ReportTitle = ti.Title FROM dbo.tblSales ti WHERE ti.Title = (SELECT REPLACE(T1.Title,'Sale FROM dbo.tblSales t WHERE t1.IdCountry = 34 AND t1.IdReport = 3375 AND t1.IdLanguage= and t1.btInUse = PRINT @ReportTitleSELECT i.IdCountry as Country, i.Title as ReportTitle, i.Report as IndustryIDFROM dbo.tblSales iWHERE i.IdCountry = AND i.IdLanguag = and i.btInUse = AND i.Title LIKE '%' @ReportTitle '%'现在,如果我运行完全相同的查询但更改AND t1.IdReport = 3375为AND t1.IdReport = 3595打印@ReportTitle返回空,我写的查询不返回任何行。$ q; e* W% H7 y6 P, m, ?$ ]! x
这是一个无效的例子: " v+ s: ^- {/ u. N; Z4 G# `DECLARE @ReportTitle as VARCHAR(100)SELECT @ReportTitle = ti.Title FROM dbo.tblSales ti WHERE ti.Title = (SELECT REPLACE(T1.Title,'Sale FROM dbo.tblSales t WHERE t1.IdCountry = 34 AND t1.IdReport = 3375 AND t1.IdLanguage= and t1.btInUse = PRINT @ReportTitleSELECT i.IdCountry as Country, i.Title as ReportTitle, i.Report as IndustryIDFROM dbo.tblSales iWHERE i.IdCountry = AND i.IdLanguag = and i.btInUse = AND i.Title LIKE @ReportTitle '%'根据我的预期结果IdReportI pass使用变量来保存标题。 . r. e/ M" @! D- I% q6 @例如,第一次围绕变量 printsRetail这是我所期待的,然后我搜索包含%Retail%/ h! L3 T4 f4 y
第二次用IdReport= 3595运行查询,返回 title Appointments。如果我打印@ReportTitle,当我期望它等于时,它并不等于Appointments" {5 z, u: Y8 t