当前位置: 代码迷 >> Sql Server >> 存储过程int连接单引号有关问题
  详细解决方案

存储过程int连接单引号有关问题

热度:36   发布时间:2016-04-27 20:28:27.0
存储过程int连接单引号问题,
存储过程如下:
Create   PROC   GetModelList
@CategoryID   int,
@ModelID   int,
@Model   varchar(50)

As
Declare   @Sql   varchar(200)
Set   @Sql   =   'Select   ModelID,Model,Models.CategoryID   As   CateID,CategoryName_CH,Pdf,InstallPicPath, '
+ 'ProPicPath,AssmParaPicPath,ModelDesc_CN,ModelDesc_EN,ModelDesc   From   Models,Categories   '
+ 'Where   Models.CategoryID=Categories.CategoryID '

if(@CategoryId   <>   0)
@[email protected]   +   '   And   Models.CategoryID= ' "[email protected]+ " "
這出錯,Incorrect   syntax   near   '@Sql '.
Cannot   use   empty   object   or   column   names.   Use   a   single   space   if   necessary.

if(@ModelID <>   0)
@[email protected]+   '   And   ModelID= ' "[email protected]+ " "

if(@Model   <> ' ')
@[email protected]+ '   And   Model= ' ' '[email protected]+ ' ' ' '
這出錯,Incorrect   syntax   near   '@Sql '.
@[email protected]+ '   Order   By   Model   Desc '

Exec(@Sql)
GO

數字要怎么連接啊?

------解决方案--------------------
@[email protected] + ' And Models.CategoryID= ' + cast(@CategoryID as varchar(2))

------解决方案--------------------
先要转换成varchar
------解决方案--------------------
@[email protected] + ' And Models.CategoryID= ' + CONVERT(varchar(20),@CategoryID )


------解决方案--------------------
create PROC GetModelList
@CategoryID int,
@ModelID int,
@Model varchar(50)
as

set quoted_identifier off

Declare @Sql varchar(500)
Set @Sql = 'Select ModelID,Model,Models.CategoryID As CateID,CategoryName_CH,Pdf,InstallPicPath, '
+ 'ProPicPath,AssmParaPicPath,ModelDesc_CN,ModelDesc_EN,ModelDesc From Models,Categories '
+ 'Where Models.CategoryID=Categories.CategoryID '

if(@CategoryId <> 0)
set @[email protected] + ' And Models.CategoryID= '+ltrim(@CategoryID)


if(@ModelID <> 0)
set @[email protected]+ ' And ModelID= '+ltrim(@ModelID)

if(@Model <> ' ')
set @[email protected]+ " And Modelid= ' "[email protected]+ " ' "

set @[email protected]+ ' Order By Model Desc '

Exec(@Sql)


set quoted_identifier on
GO
------解决方案--------------------
Create PROC GetModelList
@CategoryID int,
@ModelID int,
@Model varchar(50)

As
Declare @Sql varchar(200)
Set @Sql = 'Select ModelID,Model,Models.CategoryID As CateID,CategoryName_CH,Pdf,InstallPicPath, '
+ 'ProPicPath,AssmParaPicPath,ModelDesc_CN,ModelDesc_EN,ModelDesc From Models,Categories '
+ 'Where Models.CategoryID=Categories.CategoryID '

if(@CategoryId <> 0)
set @[email protected] + ' And Models.CategoryID= ' "+rtrim(cast(@CategoryID as varchar))+ " "

if(@ModelID <> 0)
set @[email protected]+ ' And ModelID= ' "+rtrim(cast(@ModelID as varchar))+ " "

if(@Model <> ' ')
set @[email protected]+ ' And Model= ' ' '[email protected]+ ' ' ' '
  相关解决方案