添加新的 Join 语句后,MS-SQL 存储过程抛出错误。消息 102,级别 15,状态 1,第 279 行 'WHERE' 附近的语法不正确
MS-SQL stored procedure throwing error after adding a new Join statement. Msg 102, Level 15, State 1, Line 279 Incorrect syntax near 'WHERE'
错误:
Msg 102, Level 15, State 1, Line 279
Incorrect syntax near 'WHERE'.
ALTER PROCEDURE [dbo].[Country_usp_GetLocationsDetailsTemp]
(@RiskID nvarchar(50)
,@LocationID nvarchar(50)
,@SortColumn nvarchar(50)
,@SortOrder nvarchar(5)
,@PageIndex nvarchar(5)
,@MaxRows nvarchar(5))
AS
declare @sql1 nvarchar(max)
declare @sql2 nvarchar(max)
BEGIN
SET NOCOUNT ON;
if( @SortColumn = 'Description') begin set @SortColumn = 'PL.Description' end
set @sql1 = '
;with LocationResults as
(
SELECT
ROW_NUMBER() OVER (ORDER BY ' + @SortColumn + ' ' + @SortOrder + ') AS RowNumber
,PL.RiskID as RiskID
, [LocationID]
,HMLC.Description as ''Country''
set @sql2 = ' ,EarthQuake as EarthQuakeID
,HRIF.ProgramID
FROM [Country_Locations] PL
LEFT OUTER JOIN Country_Master_Common HMLC on HMLC.ColumnName = ''Country'' and HMLC.Code = Country and HMLC.active = 1
LEFT OUTER JOIN Country_RiskInformation HRIF ON hrif.active = 1 and hrif.riskid = ''' + @RiskID + '''
WHERE
(''' + @RiskID + '''= '''' or PL.RiskID =''' + @RiskID + ''')
AND ('''+@LocationID+''' = '''' or LocationID = '''+@LocationID+''')
AND PL.Active = 1
)
SELECT * FROM LocationResults
WHERE
RowNumber BETWEEN('+@PageIndex+' -1) * '+@MaxRows + '+ 1 AND((('+@PageIndex+' -1) * '+@MaxRows+' + 1) + '+@MaxRows+') - 1'
exec (@sql1 + @sql2)
print @sql1
print @sql2
这是计算分页显示结果的公式。
这个公式在添加新的 Join 语句后失效。
但据我所知,我的加入没有问题,因为如果我尝试使用公式中的实际值执行打印的 SQL,例如:
SELECT * FROM LocationResults WHERE RowNumber BETWEEN 1 and 5
它工作正常。
注意:我在 SP 中使用的所有联接都是左外部联接。
我想知道我哪里做错了。
由于代码过长,查询被截断。
添加一个新变量并将 sp 的最后一个查询分配给新变量解决了这个问题。
sql3= 'SELECT * FROM LocationResults WHERE RowNumber BETWEEN 1 and 5
'
错误:
Msg 102, Level 15, State 1, Line 279
Incorrect syntax near 'WHERE'.
ALTER PROCEDURE [dbo].[Country_usp_GetLocationsDetailsTemp]
(@RiskID nvarchar(50)
,@LocationID nvarchar(50)
,@SortColumn nvarchar(50)
,@SortOrder nvarchar(5)
,@PageIndex nvarchar(5)
,@MaxRows nvarchar(5))
AS
declare @sql1 nvarchar(max)
declare @sql2 nvarchar(max)
BEGIN
SET NOCOUNT ON;
if( @SortColumn = 'Description') begin set @SortColumn = 'PL.Description' end
set @sql1 = '
;with LocationResults as
(
SELECT
ROW_NUMBER() OVER (ORDER BY ' + @SortColumn + ' ' + @SortOrder + ') AS RowNumber
,PL.RiskID as RiskID
, [LocationID]
,HMLC.Description as ''Country''
set @sql2 = ' ,EarthQuake as EarthQuakeID
,HRIF.ProgramID
FROM [Country_Locations] PL
LEFT OUTER JOIN Country_Master_Common HMLC on HMLC.ColumnName = ''Country'' and HMLC.Code = Country and HMLC.active = 1
LEFT OUTER JOIN Country_RiskInformation HRIF ON hrif.active = 1 and hrif.riskid = ''' + @RiskID + '''
WHERE
(''' + @RiskID + '''= '''' or PL.RiskID =''' + @RiskID + ''')
AND ('''+@LocationID+''' = '''' or LocationID = '''+@LocationID+''')
AND PL.Active = 1
)
SELECT * FROM LocationResults
WHERE
RowNumber BETWEEN('+@PageIndex+' -1) * '+@MaxRows + '+ 1 AND((('+@PageIndex+' -1) * '+@MaxRows+' + 1) + '+@MaxRows+') - 1'
exec (@sql1 + @sql2)
print @sql1
print @sql2
这是计算分页显示结果的公式。
这个公式在添加新的 Join 语句后失效。
但据我所知,我的加入没有问题,因为如果我尝试使用公式中的实际值执行打印的 SQL,例如:
SELECT * FROM LocationResults WHERE RowNumber BETWEEN 1 and 5
它工作正常。
注意:我在 SP 中使用的所有联接都是左外部联接。
我想知道我哪里做错了。
由于代码过长,查询被截断。 添加一个新变量并将 sp 的最后一个查询分配给新变量解决了这个问题。
sql3= 'SELECT * FROM LocationResults WHERE RowNumber BETWEEN 1 and 5 '