当前位置: 代码迷 >> Sql Server >> 帮忙看下这个存储过程,解决方法
  详细解决方案

帮忙看下这个存储过程,解决方法

热度:100   发布时间:2016-04-27 19:27:47.0
帮忙看下这个存储过程,急!!
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'Pr_GetList ' AND type = 'P ') 
  DROP PROCEDURE Pr_GetList 
GO 
CREATE PROCEDURE Pr_GetList 

@iMode char(10), 
@TbName nvarchar(15), 
@iFiled nvarchar(20), 
@sWhere nvarchar(20) 

AS 
begin 
declare @sql varchar(100) 
if @iMode= '1 ' 
if @sWhere!= ' ' 
@sql= 'Select DISTINCT ' + @iFiled + ' from '+ @TbName + 'where ' [email protected]+ ' order by [email protected] 
else 
@sql= 'Select DISTINCT ' + @iFiled + ' from ' + @TbName 
else 
if @sWhere!= ' ' 
@sql= 'Select * from ' + TableName + 'where ' [email protected]+ ' order by [email protected] 
else 
@sql= 'Select * from ' + TableName 
execute @sql 

GO 
怎么老是说: 
服务器: 消息 170,级别 15,状态 1,过程 Pr_GetList,行 13 
第 13 行: [email protected] ' 附近有语法错误。 
服务器: 消息 170,级别 15,状态 1,过程 Pr_GetList,行 18 
第 18 行: [email protected] ' 附近有语法错误。

------解决方案--------------------
if @sWhere!= ' '
set @sql= 'Select DISTINCT ' + @iFiled + ' from '+ @TbName + 'where ' [email protected]+ ' order by [email protected]

------解决方案--------------------
SQL code
IF EXISTS (SELECT name FROM sysobjects WHERE name =   'Pr_GetList  ' AND type =   'P  ')    DROP PROCEDURE Pr_GetList  GO  CREATE PROCEDURE Pr_GetList  (  @iMode char(10),  @TbName nvarchar(15),  @iFiled nvarchar(20),  @sWhere nvarchar(20)  )  AS  begin    declare @sql varchar(100)    if @iMode=  '1  '   begin     if @sWhere!=  '  '        set @sql=  'Select DISTINCT   ' + @iFiled +   ' from   '+ @TbName +  'where   ' [email protected]+  ' order by   [email protected]      else        set @sql=  'Select DISTINCT   ' + @iFiled +   ' from   ' + @TbName    end  else  begin      if @sWhere!=  '  '        set @sql=  'Select * from   ' + TableName +  'where   ' [email protected]+  ' order by   [email protected]      else        set @sql=  'Select * from   ' + TableName    end  exec(@sql)end
------解决方案--------------------
SQL code
IF EXISTS (SELECT name FROM sysobjects WHERE name =   'Pr_GetList  ' AND type =   'P  ')     DROP PROCEDURE Pr_GetList  GO  CREATE PROCEDURE Pr_GetList  (  @iMode char(10),  @TbName nvarchar(15),  @iFiled nvarchar(20),  @sWhere nvarchar(20)  )  AS  begin  declare @sql varchar(100)  if @iMode=  '1  '  if @sWhere!=  '  '  [color=#FF0000]set [/color]@sql=  'Select DISTINCT   ' + @iFiled +   ' from   '+ @TbName +  [color=#FF0000]' where   '[/color] [email protected]+  ' order by   [email protected]  else  [color=#FF0000]set [/color]@sql=  'Select DISTINCT   ' + @iFiled +   ' from   ' + @TbName  else  if @sWhere!=  '  '  [color=#FF0000]set [/color]@sql=  'Select * from   ' + TableName +  [color=#FF0000]' where   [/color]' [email protected]+  ' order by   [email protected]  else  [color=#FF0000]set [/color]@sql=  'Select * from   ' + TableName  execute @sql  对照红色部分改下再试试
  相关解决方案