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  ' +@sWhere+ ' order by  '+@iFiled 
else 
@sql= 'Select DISTINCT  ' + @iFiled +  ' from  ' + @TbName 
else 
if @sWhere!= ' ' 
@sql= 'Select * from  ' + TableName + 'where  ' +@sWhere+ ' order by  '+@iFiled 
else 
@sql= 'Select * from  ' + TableName 
execute @sql GO 
怎么老是说: 
服务器: 消息 170,级别 15,状态 1,过程 Pr_GetList,行 13 
第 13 行:  '@sql ' 附近有语法错误。 
服务器: 消息 170,级别 15,状态 1,过程 Pr_GetList,行 18 
第 18 行:  '@sql ' 附近有语法错误。

解决方案 »

  1.   

    if @sWhere!=  '  '  
    set @sql=  'Select DISTINCT   ' + @iFiled +   ' from   '+ @TbName +  'where   ' +@sWhere+  ' order by   '+@iFiled  
      

  2.   

    本帖最后由 libin_ftsafe 于 2007-10-16 15:27:28 编辑
      

  3.   

    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!=  '  '  
    set @sql=  'Select DISTINCT   ' + @iFiled +   ' from   '+ @TbName +  ' where   ' +@sWhere+  ' order by   '+@iFiled  
    else  
    set @sql=  'Select DISTINCT   ' + @iFiled +   ' from   ' + @TbName  
    else  
    if @sWhere!=  '  '  
    set @sql=  'Select * from   ' + TableName +  ' where   ' +@sWhere+  ' order by   '+@iFiled  
    else  
    set @sql=  'Select * from   ' + TableName  
    execute @sql  对照红色部分改下再试试
      

  4.   

    倒。怎么给SQL直接显示出来了`
    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!=  '  '  
    set @sql=  'Select DISTINCT   ' + @iFiled +   '  from   '+ @TbName +  ' where   ' +@sWhere+  ' order by   '+@iFiled  
    else  
    set @sql=  'Select DISTINCT   ' + @iFiled +   '  from   ' + @TbName  
    else  
    if @sWhere!=  '  '  
    set @sql=  'Select * from   ' + TableName +  ' where   ' +@sWhere+  ' order by   '+@iFiled  
    else  
    set @sql=  'Select * from   ' + TableName  
    execute @sql  
      

  5.   

    if @sWhere!=   '   '   
    set @sql=   'Select * from    ' + TableName +   ' where    ' +@sWhere+   ' order by    '+@iFiled   
    这里的where前面也要加空格。这个编译器真不好用。 
      

  6.   

    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 ' +@sWhere+' order by '+@iFiled
    else
    set @sql='Select DISTINCT ' + @iFiled + ' from ' + @TbName
      end
    else
      begin
    if @sWhere!=''
    set @sql='Select * from ' + @TbName +'where ' +@sWhere+' order by '+@iFiled
    else
    set @sql='Select * from ' + @TbName
      end
    execute @sqlGO刚才发急了,现在改了一下,还是报错
    服务器: 消息 170,级别 15,状态 1,过程 Pr_GetList,行 25
    第 25 行: '@sql' 附近有语法错误。
      

  7.   

    已经解决,谢谢。是Begin 和 end 的问题