我得到了scalar variable not define @cmdSQL
在尝试打印我的查询时,看看什么是sql查询正在运行时生成
print @cmdSQL = @cmdSQL + ' from server_des where title Like ''%'+@title+'%'' AND Description Like ''%'+@Description+'%'' AND Keywords Like ''%'+@Keywords+'%'' AND category Like ''%'+@Category+'%'' AND Location Like ''%'+@Location+'%''AND source Like ''%'+@Source+'%'' AND Date Like ''%'+@Date+'%'
exec All_Searchnew1 'man','thief','ma','ma','ma','ma','12/12/2009',1,1,1,0,0,0,0,0
procedure Is written below...
Alter PROCEDURE [dbo].[All_Searchnew1]
@title nvarchar(max),
@Description nvarchar(max),
@Keywords nvarchar(max),
@Category nvarchar(max),
@Location nvarchar(max),
@Source nvarchar(max),
@Date nvarchar(50),
@RD_btn_AND_OR AS BIT,
@Chk_title AS BIT ,
@Chk_Description AS Bit ,
@Chk_Keywords AS BIT,
@Chk_Category AS BIT,
@Chk_Location AS BIT,
@Chk_Source AS BIT,
@Chk_Date AS BIT
AS
DECLARE @cmdSQL AS VARCHAR(2000)
SET @cmdSQL = 'Select '
DECLARE @cmdCondition AS VARCHAR(2000)
SET @cmdCondition = ''
if @RD_btn_AND_OR = 1
Begin
if (@Chk_title = 1)
begin
SET @cmdSQL = @cmdSQL + ' title,'
SET @cmdCondition ='title Like ''%' + @title + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as title'
SET @cmdCondition ='title Like ''%' + @title + '%'''
end
if @Chk_Description <> 1
begin
SET @cmdSQL = @cmdSQL + ' Description,'
SET @cmdCondition ='Description Like ''%' + @Description + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Description'
SET @cmdCondition ='Description Like ''%' + @Description + '%'''
end
if @Chk_Keywords <> 1
begin
SET @cmdSQL = @cmdSQL + ' Keywords,'
SET @cmdCondition ='Keywords Like ''%' + @Keywords + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Keywords'
SET @cmdCondition ='Keywords Like ''%' + @Keywords + '%'''
end
if @Chk_Category <> 1
begin
SET @cmdSQL = @cmdSQL + ' Category,'
SET @cmdCondition ='Category Like ''%' + @Category + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Category'
SET @cmdCondition ='Category Like ''%' + @Category + '%'''
end
if @Chk_Location <> 1
begin
SET @cmdSQL = @cmdSQL + ' Location,'
SET @cmdCondition ='Location Like ''%' + @Location + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Location'
SET @cmdCondition ='Location Like ''%' + @Location + '%'''
end
if @Chk_Source <> 1
begin
SET @cmdSQL = @cmdSQL + ' Source,'
SET @cmdCondition ='Source Like ''%' + @Source + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Source'
SET @cmdCondition ='Source Like ''%' + @Source + '%'''
end
if @Chk_Date <> 1
begin
SET @cmdSQL = @cmdSQL + ' Date'
SET @cmdCondition ='Date Like ''%' + @Date + '%'''
end
else
begin
SET @cmdSQL = @cmdSQL +'null as Date'
SET @cmdCondition ='Date Like ''%' + @Date + '%'''
end
End
--SET @cmdSQL = @cmdSQL + ' from server_des where title like ''%''' + @title + '''%'' AND Description Like '''%''+@Description+''%''' AND Keywords Like '''%''+@Keywords+''%''' AND category Like '''%''+@Category+''%''' AND Location Like '''%''+@Location+''%'''AND source Like '''%''+@Source+''%''' AND Date Like '''%''+@Date+''%''
--SET @cmdSQL = @cmdSQL + ' from server_des where title Like ''%' + @title + '%''or description like ''%'+@Description+'%''or keywords like ''%'+@Keywords+'%''or category like ''%'+@Category+'%'''
SET @cmdCondition = @cmdCondition
SET @cmdSQL = @cmdSQL + ' from server_des where ' + @cmdCondition
EXEC(@cmdSQL)
go
答案 0 :(得分:1)
从表面上看,你正在运行
print @cmdSQL = @cmdSQL + ...
在声明存储过程中只 之前
答案 1 :(得分:0)
你也错过了这样的表达式中的逗号:
SET @cmdSQL = @cmdSQL +'null as title'
它应该是SET @cmdSQL = @cmdSQL +'null as title,'
从多个部分组合动态SQL时,缺少某些内容是常见问题。这项工作应该非常认真。