473,756 Members | 5,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stored Procedure with multiple select statements

62 New Member
Is it possible to use to select statements in a stored procedure?

I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings.

The code needs to get a count of the ratings, then the sum of them to get the percentage.


I am getting a error
Exception Details: System.IndexOut OfRangeExceptio n: columncount

it seems my stored procedure returns the first select, but the value for the second select is non existent.

Is this possible? I've never tried a Stored Procedure with multiple selects?
and if so, what am I doing wrong?




Stored Procedure -----------------------------------------------------------------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFI ER ON
GO
ALTER PROCEDURE [dbo].[GetMovieRating] ( @MoviesID int )
AS

SELECT Sum(Rating) As RatingSum FROM MovieRatings WHERE (MoviesID = @MoviesID)

SELECT columncount = COUNT(*) FROM MovieRatings WHERE(MoviesID = @MoviesID)





---------------------------------------------------------------------------------------------------




Vb.net code ---------------------------------------------------------------------------------------


Public Function GetMovieRating( ByVal MoviesID As Integer) As Integer

Dim Conn As New SqlConnection(C onnectionString )
Dim CmdSelect As New SqlCommand("Get MovieRating", Conn)
CmdSelect.Comma ndType = CommandType.Sto redProcedure
CmdSelect.Param eters.Add(New SqlParameter("@ MoviesID", MoviesID))
' Try
Conn.Open()
Dim Dr As SqlDataReader = CmdSelect.Execu teReader()

While Dr.Read()

Dim Rating As Integer = Dr("RatingSum" ) / Dr("columncount ")

Return Rating

End While

' Catch ex As Exception
' Throw New ApplicationExce ption("Data Error")
' Finally
Conn.Close()
' End Try

End Function


---------------------------------------------------------------------------------------------------
Aug 1 '07 #1
4 6939
prabunewindia
199 New Member
hi friend,
try to use SqlDataAdapter instead of datareader
and get the output into DataSet
now ur dataset will have two tables
1.DatasetName.T ables[0]
2.DatasetName.T ables[1]

code

Dim Conn As New SqlConnection(C onnectionString )
Dim CmdSelect As New SqlCommand("Get MovieRating", Conn)
CmdSelect.Comma ndType = CommandType.Sto redProcedure
CmdSelect.Param eters.Add(New SqlParameter("@ MoviesID", MoviesID))
SqlDataAdapter da=new SqlDataAdapter( (CmdSelect);
Dataset ds=new Dataset();
da.Fill(ds);

use this dataset for ur process
Prabu



Is it possible to use to select statements in a stored procedure?

I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings.

The code needs to get a count of the ratings, then the sum of them to get the percentage.


I am getting a error
Exception Details: System.IndexOut OfRangeExceptio n: columncount

it seems my stored procedure returns the first select, but the value for the second select is non existent.

Is this possible? I've never tried a Stored Procedure with multiple selects?
and if so, what am I doing wrong?




Stored Procedure -----------------------------------------------------------------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFI ER ON
GO
ALTER PROCEDURE [dbo].[GetMovieRating] ( @MoviesID int )
AS

SELECT Sum(Rating) As RatingSum FROM MovieRatings WHERE (MoviesID = @MoviesID)

SELECT columncount = COUNT(*) FROM MovieRatings WHERE(MoviesID = @MoviesID)





---------------------------------------------------------------------------------------------------




Vb.net code ---------------------------------------------------------------------------------------


Public Function GetMovieRating( ByVal MoviesID As Integer) As Integer

Dim Conn As New SqlConnection(C onnectionString )
Dim CmdSelect As New SqlCommand("Get MovieRating", Conn)
CmdSelect.Comma ndType = CommandType.Sto redProcedure
CmdSelect.Param eters.Add(New SqlParameter("@ MoviesID", MoviesID))
' Try
Conn.Open()
Dim Dr As SqlDataReader = CmdSelect.Execu teReader()

While Dr.Read()

Dim Rating As Integer = Dr("RatingSum" ) / Dr("columncount ")

Return Rating

End While

' Catch ex As Exception
' Throw New ApplicationExce ption("Data Error")
' Finally
Conn.Close()
' End Try

End Function


---------------------------------------------------------------------------------------------------
Aug 1 '07 #2
gagandeepgupta16
56 New Member
Is it possible to use to select statements in a stored procedure?

I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings.

The code needs to get a count of the ratings, then the sum of them to get the percentage.


I am getting a error
Exception Details: System.IndexOut OfRangeExceptio n: columncount

it seems my stored procedure returns the first select, but the value for the second select is non existent.

Is this possible? I've never tried a Stored Procedure with multiple selects?
and if so, what am I doing wrong?




Stored Procedure -----------------------------------------------------------------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFI ER ON
GO
ALTER PROCEDURE [dbo].[GetMovieRating] ( @MoviesID int )
AS

SELECT Sum(Rating) As RatingSum FROM MovieRatings WHERE (MoviesID = @MoviesID)

SELECT columncount = COUNT(*) FROM MovieRatings WHERE(MoviesID = @MoviesID)





---------------------------------------------------------------------------------------------------




Vb.net code ---------------------------------------------------------------------------------------


Public Function GetMovieRating( ByVal MoviesID As Integer) As Integer

Dim Conn As New SqlConnection(C onnectionString )
Dim CmdSelect As New SqlCommand("Get MovieRating", Conn)
CmdSelect.Comma ndType = CommandType.Sto redProcedure
CmdSelect.Param eters.Add(New SqlParameter("@ MoviesID", MoviesID))
' Try
Conn.Open()
Dim Dr As SqlDataReader = CmdSelect.Execu teReader()

While Dr.Read()

Dim Rating As Integer = Dr("RatingSum" ) / Dr("columncount ")

Return Rating

End While

' Catch ex As Exception
' Throw New ApplicationExce ption("Data Error")
' Finally
Conn.Close()
' End Try

End Function


---------------------------------------------------------------------------------------------------

hi
there can be multiple select query, the thing is how you return the output.
having two output parameters or returning to datareader.

Your Stored Procedure -----------------------------------------------------------------------------------


set ANSI_NULLS ON
set QUOTED_IDENTIFI ER ON
GO
ALTER PROCEDURE [dbo].[GetMovieRating] ( @MoviesID int )
AS

SELECT Sum(Rating) As RatingSum FROM MovieRatings WHERE (MoviesID = @MoviesID)

SELECT COUNT(*) as columncount FROM MovieRatings WHERE(MoviesID = @MoviesID)


hope this solves your prob
Aug 1 '07 #3
vertigo262
62 New Member
I guess my question is, how do I retreive both those column values.

the reason I am trying to use a datareader is so I can grab both values, and dived them. then put them into a variable and return the function.

I don't know if you can grab values out of a dataset like that. I've never used a dataset in that way. I've just used them to fill controls with a dataset.

any help would be appreciated.
Aug 1 '07 #4
vertigo262
62 New Member
Ok, it's working.
I took prabunewindia's advice.

I created a dataset and pulled the 2 variables.

here is my code ----------------------------------------------------------------

Dim Conn As New SqlConnection(C onnectionString )
Dim CmdSelect As New SqlCommand("Get MovieRating", Conn)
CmdSelect.Comma ndType = CommandType.Sto redProcedure
CmdSelect.Param eters.AddWithVa lue("@MoviesID" , MoviesID)

Dim Da As New SqlDataAdapter( CmdSelect)
Dim Ds As New DataSet

'Fill the Dataset
'Try
Da.Fill(Ds)

Dim RatingSum As Integer = Ds.Tables(0).Ro ws(0)("RatingSu m").ToString ()
Dim columncount As Integer = Ds.Tables(1).Ro ws(0)("columnco unt").ToString( )



Dim Rating As Integer = RatingSum / columncount

Return Rating



'Catch ex As Exception
Throw New ApplicationExce ption("Data Error")
'Finally
Conn.Close()
'End Try

-----------------------------------------------------------------


My only concern is that this code is going to query the database for every movie listed. I hope this method of counting and suming of a database table for every movie in a recordset isn't too taxing on the database as it gets large and there are a lot of users using the system.

Thanks for all your help
Aug 2 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1940
by: Dieter Gasser | last post by:
I have two SQL Server stored procedures, PROC1 and PROC2. PROC1 has about 50 input parameters. PROC2 is the main procedure that does some data modifications and afterwards calls PROC1 using an EXECUTE statement. The input parameter values for PROC1 are stored in a table in my database. What I like to do is passing those values to PROC1 using a SELECT statement. Currently, all 50 parameters are read and stored in a variable, and...
0
2115
by: jamiemcc | last post by:
Hi, I would like to have 1 stored procedure call another stored procedure (which contains multiple select statements) and then be able to access the 3 result sets. Example Create Procedure . exec Multiple --manipulate result set A
1
4272
by: Harry V | last post by:
I'm wondering if there is a limit of a single select, delete and insert statement (command) to each dataadapter? On a form, I have a dataconnection that is shared by 3 dataadapters, one for each of three tables in an Access database. Each was created in the form designer and each has a datatable that was generated in the designer. I have no problem filling the datatables and inserting the rows into the Access tables. I DO need to select the...
3
2169
by: Joe via DotNetMonster.com | last post by:
Hi, I'm trying to use several select statements so that I don't need to call the function several times. The next Result set always seems to read the first select statement. I have the following: Dim queryString As String = "SELECT TOP 1 (.), . FROM WHERE (. = @LessonID) AND .='Motivate'; SELECT TOP 1 (.), .
4
9138
by: Chris | last post by:
This might be a stupid question.... I have a stored procedures, which uses two selects. When I run the SP I get two resultsets, one very big and the other much smaller, which is the one I want in the dataset. How do a ensure only the last one goes in the dataset.
2
5095
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have coded multiple select statements in a single stored procedure, and when I execute this procedure on SQL Server Management Express, I correctly get multiple result sets. But, if I try to add a new Data Source to to my VB 2005 project, and point to this stored procedure, the data source wizard only sees the 'first' select statement. Is there a way to load multiple tables in a DataSet from a single stored procedure with multiple...
7
2232
by: golffor1 | last post by:
Hello I was wondering if you could help me out with doing multiple select statements. I have this objCommand.CommandText = "Select * from table" objCommand.CommandText1 = "Select * from table where number like" & Val(txtSearchList.Text) objCommand.CommandTimeout = 30 objCommand.CommandType = adCmdText Set objRS = objCommand.Execute Set objCommand = Nothing
0
2143
by: Richard Silvers | last post by:
Hi, I'm trying to find the most efficient way to access DB2 data on z/OS DB2 V8. Any suggestions would be welcomed . . . Situation: 1. Converting a VSAM file to DB2 on z/OS 2. VSAM file will be populated into 4 DB2 tables, 1 parent and 3 child tables
3
43187
by: els12 | last post by:
How do I write 2 distinct select queries in one sql statement, and then also divide the results of one by the other? The first query will give me a subset of all the records in the table...the second query will give me a second (larger subset)...and then I want to use those two results to come up with a percentage. Any thoughts?
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3352
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.