日期:2014-05-17  浏览次数:20801 次

存储过程如何写
这是一个关于sql server 中的存储过程的问题。

我有一个表
两个列:
friendid,my
我的存储过程是这样写的。

SQL code

create  procedure [dbo].[getFriendList]
    @my INT OUTPUT,
    @friendid INT output,

AS
     select @friendid=my
     from FriendInfo
     where MyID=@MyQQID


现在想获取friendid的列表,但是我执行这个,只能获得一个,不能获得多个。

事先,我传入了@my这个参数的值,希望返回表中所有有@my这个值的行。

是多行的,能弄不?


------解决方案--------------------
存储过程不可以输出表类型的。

create procedure [dbo].[getFriendList]
@my INT 
AS
select my
from FriendInfo
where MyID=@My

过程你可以这么写.你写的有误.

然后前台你直接返回DataSet就行了。



------解决方案--------------------
C# code


/* 准备演示过程 getFriendList,
         * CREATE PROCEDURE [dbo].[getFriendList]  
            AS
              select 1
              UNION ALL
              SELECT 2
              UNION ALL
              SELECT 3
         */
        //....
        public DataTable Execute()
        {
            DataTable dt = new DataTable();

            SqlConnection conn=new SqlConnection();
            SqlCommand cmd = new SqlCommand("getFriendList", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            dt.Load(cmd.ExecuteReader());
            conn.Close();
            return dt;
        }