简体   繁体   中英

How to return multiple records from multiple tables bu a single stored procedure having parameters?

Suppose my stored procedure is:

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_QB_descriptionwise_select')
    BEGIN
        DROP  Procedure  sp_QB_descriptionwise_select
    END
GO
CREATE Procedure sp_QB_descriptionwise_select
    (
        @description nvarchar(max)
    )
AS

declare @type int
SELECT     Question.questionid, Question.question, Question.answer, Question.typeOfQuestion, QuestionBank.description
FROM         QuestionBank INNER JOIN
                      Question ON QuestionBank.questionid = Question.questionid
WHERE     (QuestionBank.description = @description)

SELECT     @type = Question.typeOfQuestion
FROM         QuestionBank INNER JOIN
                      Question ON QuestionBank.questionid = Question.questionid
WHERE     (QuestionBank.description = @description)

if @type=1
begin
SELECT     OptionQuestion.option1, OptionQuestion.option2, OptionQuestion.option3, OptionQuestion.option4
FROM         OptionQuestion INNER JOIN
                      Question ON OptionQuestion.questionid = Question.questionid
end
GO

How can i store the records n in what way???

Using IDataReader / ExecuteReader , you should be able to use:

    using (IDataReader reader = cmd.ExecuteReader())
    {
        do
        {
            while (reader.Read())
            {
                /* row */
            }
        } while (reader.NextResult()); /* grid */
    }

Call your stored procedure with the DataSetAdapter.Fill() method. It will populate your dataset with all resulting tables. If you use a typed dataset make sure to set up table mappings (populate the DataSetAdapter.TableMappings collection). Details to be found on MSDN

DataSetCommand.selectCommand接受参数作为sqlcommand.commandText,sqlConnction在这种情况下如何传递参数?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM