我只学过C#,听说存储过程很好用,于是决心学高手。才知道做个高手太难了。
第一关就过不了,新建一个存储过程,提示错误:
“必须声明变量@type”
“必须声明变量@type”
真是烦恼得不得了了。
下面就是了。请各位老大看看下面的语句问题出在哪里。CREATE PROCEDURE [dbo].[search] 
@text varchar(200)=null,
@type varchar(50),
@part int(1)=0
AS
if(@text<>null)
                  if(@part=0)
select * from @type where contains(title,@text)          else if(@part=1)
select * from @type where contains(*,@text)
GO

解决方案 »

  1.   

    可以有个替代的方法,就是拼成一个字符串,然后执行这个字符串:
    declare @sql varchar(2000);set @sql = ' select * from ' + @type ;exec (@sql)
      

  2.   

    Sql Server帮助中的例子CREATE PROCEDURE InsertSales @PrmOrderID INT, @PrmCustomerID INT,
                     @PrmOrderDate DATETIME, @PrmDeliveryDate DATETIME
    AS
    DECLARE @InsertString NVARCHAR(500)
    DECLARE @OrderMonth INT-- Build the INSERT statement.
    SET @InsertString = 'INSERT INTO ' +
           /* Build the name of the table. */
           SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
           CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
           'Sales' +
           /* Build a VALUES clause. */
           ' VALUES (@InsOrderID, @InsCustID, @InsOrdDate,' +
           ' @InsOrdMonth, @InsDelDate)'/* Set the value to use for the order month because
       functions are not allowed in the sp_executesql parameter
       list. */
    SET @OrderMonth = DATEPART(mm, @PrmOrderDate)EXEC sp_executesql @InsertString,
         N'@InsOrderID INT, @InsCustID INT, @InsOrdDate DATETIME,
           @InsOrdMonth INT, @InsDelDate DATETIME',
         @PrmOrderID, @PrmCustomerID, @PrmOrderDate,
         @OrderMonth, @PrmDeliveryDateGO
      

  3.   

    感谢仗义相助!请教minajo21(大眼睛)
    declare语法
    回复人: minajo21(大眼睛) ( ) 信誉:100  2005-04-25 12:19:00  得分: 0  
     
     
       可以有个替代的方法,就是拼成一个字符串,然后执行这个字符串:
    declare @sql varchar(2000);set @sql = ' select * from ' + @type ;exec (@sql)
      

  4.   

    可否在程序中构造好SQL语句之后,直接传递到存储过程中执行呢?如何实现,给个实例。
      

  5.   

    SqlConnection cn=new SqlConnection("连接");
                               cn.Open();
                               SqlDataReader dr;
    SqlCommand cmd = new SqlCommand(procName, cn);
    cmd.CommandType = CommandType.StoredProcedure;
                                string[] fileds={"strSql"};//存储过程中定义的参数名
                                string[] texts={"select * from topic"};//对应的参数值
    for (int i=0;i<fileds.Length;i++)
    {
    SqlParameter parameter=new SqlParameter("@"+fileds[i],texts[i]);
    cmd.Parameters.Add(parameter);
    }
    dr=cmd.ExecuteReader();
      

  6.   

    精彩!感谢!
    为实现程序目的,如何写对应的存储过程?(yadier(YADI理想年代))
      

  7.   

    送佛送到西天!
    为实现程序目的,如何写对应的存储过程?(yadier(YADI理想