Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 5th, 2005, 09:12 AM
Fredyprogram Fredyprogram is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 8 Fredyprogram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 37 sec
Reputation Power: 0
Retrieve data using Stored Procedure

Can anyone post me an example of a Stored Procedure that returns me all the data from a table using a "Select". I have a table called "COMPANY" that has the fields "CODE, NAME, LOCAL, UF". How would my Stored Procedure be to retrieve all of the Companies and show in my DataGrid ?? Thanx

Reply With Quote
  #2  
Old August 5th, 2005, 11:44 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,907 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 9 h 21 m 6 sec
Reputation Power: 279
An example right from the docs (www.ibphoenix.com
Code:
SET TERM !! ;

CREATE PROCEDURE SUB_TOT_BUDGET (HEAD_DEPT CHAR(3))

RETURNS (tot_budget DECIMAL(12, 2), avg_budget DECIMAL(12, 2),
min_budget DECIMAL(12, 2), max_budget DECIMAL(12, 2))

AS

BEGIN
SELECT SUM(BUDGET), AVG(BUDGET), MIN(BUDGET), MAX(BUDGET)
FROM DEPARTMENT
WHERE HEAD_DEPT = :head_dept
INTO :tot_budget, :avg_budget, :min_budget, :max_budget;
EXIT;

END !!

SET TERM ; !!

Reply With Quote
  #3  
Old August 8th, 2005, 08:04 AM
Fredyprogram Fredyprogram is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 8 Fredyprogram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 37 sec
Reputation Power: 0
Select in a Stored Prcedure

But the Stored Procedure that I need will need to retrieve more than one register. It would be like doing:

SELECT CODE, NAME, UF FROM COMPANY


And that sql will return me more than one register. How would my stored procedure be ???

Reply With Quote
  #4  
Old August 9th, 2005, 03:46 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 11 sec
Reputation Power: 8
I have found small error in pabloj's example ;-)

Correct is something like:

CREATE PROCEDURE proc_name
RETURNS (
ID INTEGER,
SDATA INTEGER,
TXT VARCHAR(10))

AS
BEGIN
FOR SELECT ID,
SDATA,
TXT
FROM Table_name
INTO :ID,
:SDATA,
:TXT
DO
BEGIN
SUSPEND;
END
END

I am here for more questions ;-)

--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer
* Firebird Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org
Comments on this post
pabloj agrees!

Reply With Quote
  #5  
Old August 10th, 2005, 12:39 PM
Fredyprogram Fredyprogram is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 8 Fredyprogram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 37 sec
Reputation Power: 0
It still doesn´t do what I need. Let me try this way. How do I put this query as a Stored Procedure ? "SELECT CODE, NAME, STATE FROM COMPANY"

In which will return me more than one rows.

Reply With Quote
  #6  
Old August 11th, 2005, 01:51 AM
badukist badukist is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 62 badukist User rank is Corporal (100 - 500 Reputation Level)badukist User rank is Corporal (100 - 500 Reputation Level)badukist User rank is Corporal (100 - 500 Reputation Level)badukist User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 3 m 18 sec
Reputation Power: 7
Send a message via Yahoo to badukist
Quote:
Originally Posted by Fredyprogram
It still doesn´t do what I need. Let me try this way. How do I put this query as a Stored Procedure ? "SELECT CODE, NAME, STATE FROM COMPANY"

In which will return me more than one rows.


Define code, name, state as output parameters and

...

FOR SELECT CODE, NAME, STATE FROM COMPANY INTO :CODE, :NAME, :STATE DO
SUSPEND;

...

Reply With Quote
  #7  
Old August 11th, 2005, 03:32 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 11 sec
Reputation Power: 8
Quote:
Originally Posted by Fredyprogram
It still doesn´t do what I need. Let me try this way. How do I put this query as a Stored Procedure ? "SELECT CODE, NAME, STATE FROM COMPANY"

In which will return me more than one rows.


CREATE PROCEDURE return_company_data
RETURNS (
CODE INTEGER,
NAME VARCHAR(10),
STATE VARCHAR(10))

AS
BEGIN
FOR
SELECT CODE, NAME, STATE
FROM COMPANY
INTO :CODE, :NAME, :STATE
DO
SUSPEND;
END

Just set correct data types here.


--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer

* Firebird Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org

Reply With Quote
  #8  
Old August 15th, 2005, 08:12 AM
Fredyprogram Fredyprogram is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 8 Fredyprogram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 37 sec
Reputation Power: 0
Thanks...
Just two more question. If I want to use filters in the select. Can I do something declare a parameter "prmCODE" and so something like:

If (prmCODE <> '') then
where CODE = prmCODE

I want to use this Stored Procedure to retreive Data from my table and show it in my DataGrid. Would this increase the performance of my ASP .NET and Windows Application ??? Instead of passing pure queries ?? Thanks

Reply With Quote
  #9  
Old August 16th, 2005, 03:08 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 11 sec
Reputation Power: 8
Quote:
Originally Posted by Fredyprogram
Thanks...
Just two more question. If I want to use filters in the select. Can I do something declare a parameter "prmCODE" and so something like:

If (prmCODE <> '') then
where CODE = prmCODE


Yes, it can be done.
But it should be something like:
If (prmCODE <> '') then
where CODE = :"prmCODE"

Quote:
Originally Posted by Fredyprogram
I want to use this Stored Procedure to retreive Data from my table and show it in my DataGrid. Would this increase the performance of my ASP .NET and Windows Application ??? Instead of passing pure queries ?? Thanks


Probably it will increase performance. But it all depends...


--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer

* Firebird Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org

Reply With Quote
  #10  
Old August 17th, 2005, 04:37 AM
fikret fikret is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Sarajevo, Bosnia
Posts: 111 fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level)fikret User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 32 m 11 sec
Reputation Power: 8
Why I see that Freedyprogram is the person last who posted here?


--
Best regards,
Fikret Hasovic http://fikret.fbtalk.net
USAID TAMP Senior Programmer

* Firebird Foundation member.
- Join today at http://www.firebirdsql.org/ff/foundation
* JEDI VCS contributor
http://jedivcs.sourceforge.net/
* Firebird and Fyracle news
http://www.fyracle.org

Reply With Quote
  #11  
Old March 22nd, 2006, 07:43 AM
flashang flashang is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 2 flashang New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 19 m 20 sec
Reputation Power: 0
how about

Code:
CREATE PROCEDURE GETDATA (
    DATA1 VARCHAR(10))
RETURNS (
    RESULT VARCHAR(10))
AS
begin

    if (data1 = '' ) then
        for
        SELECT * FROM AA into :result
        do suspend;
    else
        for
        SELECT * FROM AA WHERE TEST like :DATA1 into :result
        do suspend;
end


as you can
EXECUTE PROCEDURE GETDATA ( '%TEXT%' );
EXECUTE PROCEDURE GETDATA ( '' );

Reply With Quote
  #12  
Old March 23rd, 2006, 08:18 AM
upscene upscene is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 223 upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 7 h 14 m 50 sec
Reputation Power: 8
Flashang: if you use EXECUTE, you won't get multiple rows, but only 1.

Use SELECT instead.
Comments on this post
pabloj agrees!
__________________
Martijn Tonies
Database Workbench: developer IDE for Firebird, MySQL, InterBase, MSSQL Server and Oracle
Upscene Productions
http://www.upscene.com

Reply With Quote
  #13  
Old March 23rd, 2006, 03:26 PM
samh5792 samh5792 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 8 samh5792 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 52 sec
Reputation Power: 0
Error: multiple rows in singleton select

When I call the select procedure, SelProc_All_Constituents with
“select * from SelProc_All_Constituents “
then use a TADOQuery.locate from my app, I receive the following error:
“Error: multiple rows in singleton select”

SelProc_All_Constituents Source

set term ^;
create procedure SelProc_All_Constituents
RETURNS (
constituentID integer,CT_Code integer,Inactive_YN varchar(3))
as
begin
select constituentID,CT_Code,Inactive_YN from constituents
into :constituentID,:CT_Code,:Inactive_YN;
suspend;
end^

This is my first FB procedure ever.
Any ideas?
Thx
Sam Hunt

Reply With Quote
  #14  
Old March 24th, 2006, 01:49 AM
upscene upscene is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 223 upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level)upscene User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 7 h 14 m 50 sec
Reputation Power: 8
You have a singleton select -> the SELECT with INTO and only do the SUSPEND once.

If you want to return a multi-row result from this procedure, make it a cursor select:

FOR SELECT ... INTO :variables ... DO SUSPEND;

Reply With Quote
  #15  
Old March 29th, 2006, 10:48 AM
samh5792 samh5792 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 8 samh5792 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 52 sec
Reputation Power: 0
I'm still missing something - simple, I am sure.

Here's my latest version of my select procedure that still returns just one row when I do a "select * from selproc_all_constituents9".
============
set term ^;
create procedure SelProc_All_Constituents9
RETURNS (
constituentID integer,CT_Code integer,Inactive_YN varchar(3),Deceased_YN varchar(3),
CoupleID integer,Primary_PartnerID integer,Secondary_PartnerID integer,Display_Name varchar(30),
Business_Name varchar(30),Last_Name varchar(20),First_Name varchar(20),MI char(1),
Couples_Title varchar(30),EmployerID integer,DonorID varchar(3),Title varchar(20),Suffix varchar(30),
Mail_YN varchar(3),Sort_Name varchar(30),Soundex varchar(30),Formal_Address_Name varchar(50),
Informal_Address_Name varchar(50),Formal_Salutation varchar(50),Informal_Salutation varchar(50),
Referred_By varchar(50),Job_Title varchar(30),Business_Primary_Contact varchar(50),
Use_Employer_Address varchar(3))
as
begin
for select constituentID,CT_Code,Inactive_YN,Deceased_YN,
CoupleID,Primary_PartnerID,Secondary_PartnerID,Display_Name,
Business_Name,Last_Name,First_Name,MI,
Couples_Title,EmployerID,DonorID,Title,Suffix,
Mail_YN,Sort_Name,Soundex,Formal_Address_Name,
Informal_Address_Name,Formal_Salutation,Informal_Salutation,
Referred_By,Job_Title,Business_Primary_Contact,
Use_Employer_Address from constituents
into :constituentID,:CT_Code,:Inactive_YN,eceased_YN,:CoupleID,:Primary_PartnerID,
:Secondary_PartnerID,isplay_Name,:Business_Name,:Last_Name,:First_Name,:MI,:Couples_Title,
:EmployerID,onorID,:Title,:Suffix,:Mail_YN,:Sort_Name,:Soundex,:Formal_Address_Name,
:Informal_Address_Name,:Formal_Salutation,:Informal_Salutation,:Referred_By,:Job_Title,
:Business_Primary_Contact,:Use_Employer_Address
do suspend;
end^
============
why does this only return one row?
btw, the green smiley faces are actually the colon and uppercase D.
Thx
Sam Hunt

Last edited by samh5792 : March 29th, 2006 at 10:54 AM. Reason: ,:D displays as an icon.

Reply With Quote