为什么这个 SQL 服务器存储过程被认为具有不正确的语法(错误 102)?
Why is this SQL Server stored procedure considered to have incorrect syntax (Error 102)?
尝试在 LINQPad 4 中创建存储过程,我得到
Error 102: Incorrect syntax near 'CHOPHOUSE'
我从之前的存储过程中更改了这一行:
where up.Unit = @Unit
...为此:
where up.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
这是怎么回事?几乎是一样的:
WHERE UNIT IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI 2 DAYS', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
...在 LINQPad 的查询中运行良好。可能是什么问题?
更新
好的,你们自找的;整个存储过程是:
CREATE Procedure [dbo].[sp_DontSleepInTheSubwayDarlin]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where up.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where mp.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
这些行:
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and
[ShortName]=''' + @ShortName +'''')
其中 '''
个数不足。
另外这个程序是'ROCK BOTTOM'。
编辑应为:
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
[ShortName]=''' + @ShortName +'''')
我不相信我的编辑会修复 SQL 的所有错误。
此外,我可能会寻找尽可能多地删除字符串到 SQL 执行的方法。如果不询问您的信用卡号码,我无法在此处执行此操作,因为这太麻烦了。
尝试在 LINQPad 4 中创建存储过程,我得到
Error 102: Incorrect syntax near 'CHOPHOUSE'
我从之前的存储过程中更改了这一行:
where up.Unit = @Unit
...为此:
where up.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
这是怎么回事?几乎是一样的:
WHERE UNIT IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI 2 DAYS', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
...在 LINQPad 的查询中运行良好。可能是什么问题?
更新
好的,你们自找的;整个存储过程是:
CREATE Procedure [dbo].[sp_DontSleepInTheSubwayDarlin]
@BegDate datetime,
@EndDate datetime,
@SortBy varchar(20)
AS
DECLARE
@SQLstring varchar(max),
@Statement varchar(8000),
@ShortName varchar(50),
@ItemCode varchar(25),
@PriceWeek varchar(30),
@LastPriceWeek varchar(30),
@Week int,
@WherePriceWeek varchar(2000),
@Price varchar(25),
@Contractprice int,
@CalendarBegDate datetime,
@CalendarEndDate datetime
create table #Temp
(
Unit varchar(50),
ShortName varchar(25),
ItemCode varchar(50),
Description varchar(250),
regionorder int,
Contractprice varchar(50),
Price varchar(25),
Variance varchar(25),
PriceWeek varchar(50),
Week varchar(10)
)
-- create temp table
Select up.Unit, mm.ShortName,
up.ItemCode, Description=(Select Description from MasterProducts where ItemCode=up.itemcode),
mm.regionorder, up.Contractprice
into #TempContract From UnitProducts up
Inner Join Unitmembers um on up.Unit=um.Unit and abs(um.pricesheet) = 1
Inner Join Members mm on um.memberno = mm.memberno
where up.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM')
Select @CalendarBegDate = C.BeginDate From Calendar C where @BegDate between C.BeginDate and C.EndDate
Select @CalendarEndDate = C.EndDate From Calendar C where @EndDate between C.BeginDate and C.EndDate
-- get weeks and where clause
SET @WherePriceWeek = ' Where '
Declare GetPriceWeek Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
Order By 2
Open GetPriceWeek
fetch next from GetPriceWeek into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
Select @Statement = ('Alter Table #TempContract Add [' + @PriceWeek + '] numeric(8,2) ')
exec (@Statement)
IF(@WherePriceWeek<>' Where ')
Begin
SET @WherePriceWeek = @WherePriceWeek + 'or '
End
SET @WherePriceWeek = @WherePriceWeek + 'IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
fetch next from GetPriceWeek into @PriceWeek, @BegDate
end
Close GetPriceWeek
Deallocate GetPriceWeek
-- build member data by weeks
Declare GetMemberColumns Cursor For
SELECT distinct ShortName,ItemCode
FROM #TempContract
Order by ShortName,ItemCode
Open GetMemberColumns
fetch next from GetMemberColumns into @ShortName,@ItemCode
while @@fetch_status = 0
begin
Declare GetMemberPrice Cursor For
Select [PriceWeek] = C.Description, convert(varchar(20),
cast(IsNull(mp.Price,0) as numeric(8,2))) as Price,
up.Contractprice
From MemberPrice mp
Inner Join UnitProducts up on mp.unit=up.unit and mp.itemcode=up.itemcode
Inner Join Unitmembers um on mp.memberno=um.memberno and mp.unit=um.unit and abs
(um.pricesheet) = 1
Inner Join Members mm on mp.memberno = mm.memberno
Inner Join Calendar C on mp.CYear=C.CYear and mp.Cweek=C.CWeek
where mp.Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and C.BeginDate <= @CalendarEndDate and C.EndDate >= @CalendarBegDate
and mm.ShortName = @ShortName and Mp.ItemCode = @ItemCode
Open GetMemberPrice
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
while @@fetch_status = 0
begin
Print(@Price)
Select @Statement = ('Update #TempContract Set [' + @PriceWeek + ']=''' +
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and
[ShortName]=''' + @ShortName +'''')
exec (@Statement)
fetch next from GetMemberPrice into @PriceWeek,@Price,@Contractprice
end
Close GetMemberPrice
Deallocate GetMemberPrice
fetch next from GetMemberColumns into @ShortName,@ItemCode
end
Close GetMemberColumns
Deallocate GetMemberColumns
--Select * From #TempContract
-- final select statement
SET @Week = 0
SET @LastPriceWeek = ''
SET @SQLstring = ''
Declare SetPriceWeekSQL Cursor For
Select [PriceWeek] = C.Description, C.BeginDate
From Calendar C
where C.BeginDate between @CalendarBegDate and @CalendarEndDate
Order By 2
Open SetPriceWeekSQL
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
while @@fetch_status = 0
begin
SET @Week = @Week + 1
IF(@SQLstring='')
Begin
SET @SQLstring = @SQLstring + 'Insert Into #Temp Select Unit, ShortName, ItemCode,
Description, regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, (convert(decimal(10,3),''-0.001'')) as
Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''-0.001'' as Variance, '''' as PriceWeek,
''WK'+convert(varchar(2),@Week)+''' as Week From #TempContract'+@WherePriceWeek
End
End
ELSE
Begin
SET @SQLstring = @SQLstring + ' UNION '
SET @SQLstring = @SQLstring + 'Select Unit, ShortName, ItemCode, Description,
regionorder, Contractprice, IsNull('+
'['+@PriceWeek+'],''0.00'') as Price, IsNull(convert(decimal(10,2),['+@PriceWeek
+'])-convert(decimal(10,2),['+@LastPriceWeek+']),''0.00'') as Variance,
'''+@PriceWeek+''' as PriceWeek, ''WK'+convert(varchar(2),@Week)+''' as Week From
#TempContract'+@WherePriceWeek
IF(@SortBy='Members')
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, ShortName, '''',
''zzzz'', '''', '''', ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert(varchar
(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99'' or
IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
Else
Begin
SET @SQLstring = @SQLstring + ' UNION Select Unit, '''', ItemCode,
Description, ''1000'', Contractprice, ''0'' as Price, ''0'' as Variance, '''' as PriceWeek, ''WK'+convert
(varchar(2),@Week)+''' as Week From #TempContract Where IsNull(['+@LastPriceWeek+'],''0.00'') <> ''999.99''
or IsNull(['+@PriceWeek+'],''0.00'') <> ''999.99'' '
End
End
SET @LastPriceWeek = @PriceWeek
fetch next from SetPriceWeekSQL into @PriceWeek, @BegDate
end
Close SetPriceWeekSQL
Deallocate SetPriceWeekSQL
Print(@SQLstring)
Execute(@SQLstring)
Drop Table #TempContract
IF(@SortBy='Members')
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ShortName=T.ShortName and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ShortName=T.ShortName and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,ShortName,Description
End
ELSE
Begin
Select
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
convert(varchar(20),convert(decimal(10,2),Price)) as Price,
sum(convert(money,Variance)) as Variance,
VarianceAverage = convert(varchar(25),convert(decimal(10,2),(Select sum(convert
(money,Variance)) From #Temp Where ItemCode=T.ItemCode and Week=T.Week) / Replace(((Select count
(regionorder) From #Temp Where ItemCode=T.ItemCode and Week=T.Week)-count(Variance)),'0','1'))),
PriceWeek,Week
From #Temp T
Group By
Unit,
ShortName,
ItemCode,
Description,
regionorder,
Contractprice,
Price,
PriceWeek,Week
Order By Week,Description,regionorder
End
Drop Table #Temp
这些行:
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN ('CHOPHOUSE', 'CRAFTWORKS SC', 'GORDON BIERSCH', 'OLD CHI FRANCHISE', 'OLD CHICAGO', 'ROCK BOTTOM') and
[ShortName]=''' + @ShortName +'''')
其中 '''
个数不足。
另外这个程序是'ROCK BOTTOM'。
编辑应为:
IsNull(@Price,'0.00') + ''' where ItemCode=''' + @ItemCode + ''' and Unit IN (''CHOPHOUSE'', ''CRAFTWORKS SC'', ''GORDON BIERSCH'', ''OLD CHI FRANCHISE'', ''OLD CHICAGO'', ''ROCK BOTTOM'') and
[ShortName]=''' + @ShortName +'''')
我不相信我的编辑会修复 SQL 的所有错误。
此外,我可能会寻找尽可能多地删除字符串到 SQL 执行的方法。如果不询问您的信用卡号码,我无法在此处执行此操作,因为这太麻烦了。