473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Stored Procdures MS SQL

49
Can anyone help, i'm just starting out with MS SQL on a new web page and i'm having problems writing a stored procdure, its for a simple forum on a web page.
I have a table with the main forum information, i then have a stored procdure

"SELECT Count(forum.ID) AS CountOfID, forum.partID
FROM forum
GROUP BY forum.partID
HAVING (((forum.partID)<>0));"

this is to work out which is the main posts and not the replies, i then have the main table with all the records.

i then am trying to write another stored procdure that looks at the main forum table and combines that with the stored procdure, however i can seem to get the last stored procdure to work, can a stored prcdure use a SELECT statment for a stored procdure and a table, or do i need to prefix the stored procedure with some thing??

SELECT forum.ForumID, forum.ID, forum.partID, CountofUsers.CountOfID, forum.subject, forum.author, forum.message, forum.responder, forum.dtDate, forum.email, forum.forumIP, forum.PicID
FROM forum LEFT JOIN CountofUsers ON forum.ID = CountofUsers.partID;

Forum is the table
CountofUsers is the stored procdure
May 22 '07 #1
8 2105
almaz
168 Expert 100+
...Forum is the table
CountofUsers is the stored procdure
You cannot use stored procedures in the FROM clause. But if you'll rewrite your stored procedure as FUNCTION, then you'll be able to use it in the FROM clause.
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.CountofUsers
  2. RETURNS TABLE
  3. AS RETURN
  4.   SELECT Count(forum.ID) AS CountOfID, forum.partID
  5.   FROM forum
  6.   GROUP BY forum.partID
  7.   HAVING (((forum.partID)<>0));
May 23 '07 #2
Milkstr
49
thanks for that, i'm getting an error, it just says
Incorrect syntax near 'RETURNS'.

do i then write a function call in my store procdure to call this???

FROM (forum LEFT JOIN Countofusers ON forum.ID = Countofusers.PartID) INNER JOIN NumberPosts ON forum.author = NumberPosts.author;

this is the line i need to use it with
May 23 '07 #3
almaz
168 Expert 100+
thanks for that, i'm getting an error, it just says
Incorrect syntax near 'RETURNS'.
Ooops, forgot to add parentheses. Should be:

CREATE FUNCTION dbo.CountofUsers()
RETURNS TABLE
AS RETURN
SELECT Count(*) AS CountOfID, forum.partID
FROM forum
GROUP BY forum.partID
WHERE forum.partID<>0

Usage:

FROM (forum LEFT JOIN Countofusers() as CountOfUsers ON forum.ID = CountOfUsers.PartID) INNER JOIN NumberPosts ON forum.author = NumberPosts.author
May 23 '07 #4
Milkstr
49
Sorry to be a pain, but i'm still getting an error

Error -2147217900
Incorrect syntax near the keyword 'WHERE'.

I'm using 1and1 as my provider and its MS SQL 2000 server, when i go to the user defined functions is prompts me with this

CREATE FUNCTION [Function Name]
RETURNS (return_type_spec) AS
BEGIN
(FUNCTION BODY)
END

Can You Help Please!
May 23 '07 #5
Milkstr
49
Can anyone help??? Please
May 24 '07 #6
almaz
168 Expert 100+
CREATE FUNCTION dbo.CountofUsers()
RETURNS TABLE
AS RETURN
SELECT Count(*) AS CountOfID, forum.partID
FROM forum
WHERE forum.partID<>0
GROUP BY forum.partID
May 24 '07 #7
Milkstr
49
Thanks, the functions works, but still struggling to get the stored procdure to work

CREATE PROCEDURE Forum2 AS
SELECT forum.ForumID, forum.ID, forum.partID, Countofusers.CountOfID,forum.subject, forum.author, forum.message, forum.responder, forum.dtdate, forum.ipaddress, forum.PicID
FROM (forum LEFT JOIN Countofusers() as countOfUsers ON forum.ID = Countofusers.PartID)



i get "Invalid object name 'forum'.

forum is a table that exists

Can you Help?? (yet again!)
May 24 '07 #8
almaz
168 Expert 100+
...i get "Invalid object name 'forum'.

forum is a table that exists

Can you Help?? (yet again!)
Please describe what actions did you take to find out the problem, because it looks like as soon as you encounter an error you post it here instead trying to understand it. Previous one was just because of incorrect order of clauses in the SELECT statement, and it can be easily figured out from Books Online.

Concerning your current problem: there is nothing wrong with your query (except if you have case-sensitive collation), you may just have changed the active database context to another database. Please post the DDL of the [forum] table, if you'll have more questions.
May 24 '07 #9

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

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Együd Csaba | last post by:
Hi, I've a problem with some of my stored procs. My config is: RH7.1, Postgres 7.3.2 I have converted a few fields of a few tables from one type to another. After this I made all the necessary...
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
5
by: Rhino | last post by:
This question relates to DB2 Version 6 on OS/390. Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a sequential file? I am trying to debug a stored procedure. As far...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
1
by: Milkstr | last post by:
is it possible in MS SQL to write a stored procdure that uses the FROM statment to get imformation from an existing stored procdure, at the moment i get invaild object as it seem to only like the...
0
by: mirandacascade | last post by:
Questions toward the bottom of the post. Situation is this: 1) Access 97 2) SQL Server 2000 3) The Access app: a) sets up pass-thru query b) .SQL property of querydef is a string, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.