InterBase için Stored Procedure Kütüphanesi

Yazdığınız makaleleri ve üyelerimizin işine yarayacağını düşündüğünüz kodlarınızı gönderebilirsiniz. Bu foruma soru sormayın!
Cevapla
oguzozturk74
Kıdemli Üye
Mesajlar: 574
Kayıt: 01 Şub 2004 12:29
Konum: Erdemli - MERSİN

InterBase için Stored Procedure Kütüphanesi

Mesaj gönderen oguzozturk74 »

Stored Procedure DOW permits the calculation of the day of week for any date
Herhangi bir tarih için haftanın hangi günü olduğunu bulan S.P.

Kod: Tümünü seç

CREATE PROCEDURE DOW (THEDATE DATE)
RETURNS (DOW  INTEGER ) 
AS
/* WHAT : */
/* Returns the week day of THEDATE.*/
/* 1 = Monday, ... , 7 = Sunday*/
DECLARE VARIABLE REF_DATE DATE;
DECLARE VARIABLE DATE_COPY DATE;
DECLARE VARIABLE DIFF1 INTEGER;
DECLARE VARIABLE DIFF2 INTEGER; 
DECLARE VARIABLE DAYSDIFF INTEGER;
BEGIN
   /*Initialize*/
   REF_DATE = "15.DEC.96";        /*A Sunday*/
   DATE_COPY = :THEDATE;
   DIFF1 = 1;
   DIFF2 = 2;
   /*Calc*/
   WHILE (:DIFF1 <> :DIFF2) DO
      BEGIN
         DIFF1 = THEDATE - REF_DATE;
         DAYSDIFF = :DIFF1 / 7;
         DIFF2 = :DAYSDIFF * 7;
         IF (:DIFF1 <> :DIFF2) THEN
            THEDATE = :THEDATE + 1;
      END
  DOW = 7-(:THEDATE-:DATE_COPY);
  SUSPEND;
END

Invoice Number Creator - 1998/000001 şeklinde Fatura numarası oluşturan S.P.
The procedure in this file makes an invoice number from the data defined in the parameters. The invoice number contains prefix, and number part with leading characters.
----------------
This script file contains the following procedures:

FillChar - Returns a string filled with a given character.
InvNum - Returns an Invoice number as described below.
----------------

About Invoice numbers
---------------
Sometimes invoice numbers must be handled as strings but the string order differs from the integer order.

The procedure in this file makes an invoice number from the data defined in the parameters. The invoice number contains prefix, and number part with leading characters as described below.

Prefix - Contains the fix length prefix that is used at the beginning of every invoice number.
Number - The number to be generated.
MaxLen - The maximum length of the number excluded prefix.
LeadingChar - The number that appears before the number.
Inv_Num - The returning value.

example:
Prefix = "1998/" "1998/"
Number = 1 333
MaxLen = 6 6
LeadingChar = "0" "0"
Inv_Num = "1998/000001" "1998/000333"
---------------------------

Kod: Tümünü seç

*/   
SET TERM # ;

CREATE PROCEDURE FillChar(Ch CHAR, Len INTEGER)
RETURNS (Str      VARCHAR(255))
AS
DECLARE VARIABLE  I  INTEGER;
BEGIN
  Str = "";
  I = 0;
  WHILE(I < Len) DO
  BEGIN
    Str = Str || Ch;
    I = I +1;
  END
END
#

Kod: Tümünü seç

CREATE PROCEDURE Invoice_Number(
                 APrefix      VARCHAR(20), 
                 ALeadingChar CHAR,
                 AMaxLen      INTEGER, 
                 ANumber      INTEGER)
RETURNS(Inv_Num      VARCHAR(100))
AS
DECLARE VARIABLE I              INTEGER;
DECLARE VARIABLE Dummy          VARCHAR(100);
DECLARE VARIABLE WorkInvNum     CHAR(100);
BEGIN
  /* Set the length of the leading characters */
  I = 100-AMaxLen;
  /* Create the string containing the leading characters with the specified size */
  EXECUTE PROCEDURE FillChar("0", I) RETURNING_VALUES Dummy; 
  /* Make the invoice number without leading characters */
  Inv_Num = ANumber;
  /* Create the longest possible invoice number */
  I = 1;
  WHILE(I < 100) DO
  BEGIN
    /* Try to create an invoice number with this length *)
    WorkInvNum = CAST(Dummy || Inv_Num AS CHAR(99));
    /* Add a new leading character at the beginning of the invoice number */
    Inv_Num = ALeadingChar || Inv_Num;
    I = I +1;
    /* In case of error the length is exceeded so the last invoice number was the right one */
    WHEN ANY DO
    BEGIN
      Inv_Num = APrefix || Inv_Num;
      EXIT;
    END
  END
END
#

SET TERM ; #
İlk N sayıda ki veya son N sayıdaki kayıdı döndüren S.P.
Not: Küçük tablolarda daha iyi çalıştığı belirtiliyor.
This is an example of how you could provide FIRST N or LAST N record functionality using a stored proc. It's not the most efficient method -- so best for smaller tables -- but it works.

Kod: Tümünü seç

CREATE PROCEDURE GET_LAST (iHOW_MANY INTEGER)  
RETURNS (vUSER_ID VARCHAR(3)) 
AS 
  DECLARE VARIABLE iREC_COUNT INTEGER;
  DECLARE VARIABLE iCOUNT INTEGER;
  DECLARE VARIABLE iSTART_FROM INTEGER;
BEGIN
  /* How many records are there... */
  SELECT COUNT(*) 
    FROM users
    INTO :iREC_COUNT;
  /* At what record "number" do we start returning values... */
  IF (iHOW_MANY >= iREC_COUNT) THEN
    iSTART_FROM = 1;
  ELSE
    iSTART_FROM = iREC_COUNT - iHOW_MANY + 1;
  /* Initialize the "counter" */
  iCOUNT = 0;
  /* Do the query */
  FOR SELECT USER_ID
    FROM users
    ORDER BY USER_ID
    INTO :vUSER_ID
  DO
    BEGIN
      iCOUNT = iCOUNT + 1;
      IF (iCOUNT >= iSTART_FROM) THEN
        BEGIN
          SUSPEND;
        END
    END

END
Başka bir örnek ise:

İlk N sayıdaki kayıdı getiren S.P.
Retrieving the first n records from a result set

There needs to be a way to retrieve a fixed number of records from a result set using SQL.

Solution:
One way to do this is to create a stored procedure that performs the query and keeps track of how many records it has fetched. After the limit has been reached, the procedure should end.

Here's a procedure that returns the employee number, first name, and last name from the records in the employee table. The parameter rows specifies how many records the procedure returns, starting with those who have last names at the beginning of the alphabet, or the whole result set, whichever is smaller.

Kod: Tümünü seç

 create procedure getemp ( rows integer )
 returns (emp_no smallint, firstname varchar(15), lastname varchar(20))
 as
 begin
    if (rows < 1) then 
      exit;
    for select emp_no, first_name, last_name from employee
       order by Last_name 
       into :emp_no, :firstname, :lastname
    do
    begin
       suspend;
       rows = rows - 1;
       if (rows < 1) then
         exit;
    end
 end
Saygılar,
Oğuz ÖZTÜRK
Kullanıcı avatarı
husonet
Admin
Mesajlar: 2962
Kayıt: 25 Haz 2003 02:14
Konum: İstanbul
İletişim:

Mesaj gönderen husonet »

Oğuzcum bir de şunları Türkçe ye çevirsen sözlükle uğraşmak zorunda kalmayacağım. :wink:

Ama yine de saol Eline Sağlık. :lol:

Gazete manşetleri
* DİKKAT :Lütfen forum kurallarını okuyalım ve uyalım...!
* Warez,crack vs. paylaşımı kesinlikle yasaktır.
onaydin

Mesaj gönderen onaydin »

Charindex fonksiyonu;
Karakterin metnin içinde kaçıncı sırada olduğunu gösteriyor

pos(aranan_karakter,metin)

Kod: Tümünü seç

CREATE PROCEDURE POS (
  SUBSTR VarChar(100),
  STR VarChar(100))
 returns (
  DONEN Integer)
AS
DECLARE VARIABLE SubStr2 VARCHAR(201); /* 1 + SubStr-lenght + Str-length */
DECLARE VARIABLE Tmp VARCHAR(100);
BEGIN
  IF (SubStr IS NULL OR Str IS NULL)
  THEN BEGIN DONEN= NULL; EXIT; END

  SubStr2 = SubStr || '%';
  Tmp = '';
  DONEN = 1;
  WHILE (Str NOT LIKE SubStr2 AND Str NOT LIKE Tmp) DO BEGIN
    SubStr2 = '_' || SubStr2;
    Tmp = Tmp || '_';
    DONEN = DONEN + 1;
  END

  IF (Str LIKE Tmp) THEN DONEN = 0;
  SUSPEND;
END
oguzozturk74
Kıdemli Üye
Mesajlar: 574
Kayıt: 01 Şub 2004 12:29
Konum: Erdemli - MERSİN

Mesaj gönderen oguzozturk74 »

Tamam Hüseyin abi, kusura bakma . :oops:

2. örnekten itibaren;

The procedure in this file makes an invoice number from the data defined in the parameters. The invoice number contains prefix, and number part with leading characters. ( Bu prosedür bir parametre ile verisi belirtiliyor ve ilgili fatura numarasını veriyor. Bu fatura no; bir ön ek ve belli sayıda basamaklı karakterden meydana geliyor. )
----------------
This script file contains the following procedures:
( Script dosyası şu procedürü ihtiva ediyor:)

FillChar - Returns a string filled with a given character.
( FillChar- verilen karakterde string ifade döndürüyor.)
InvNum - Returns an Invoice number as described below.
( InvNum - Aşağıda belirtilen Fatura No yu döndürür.)
----------------

About Invoice numbers ( Fatura No hakkında)
---------------
Sometimes invoice numbers must be handled as strings but the string order differs from the integer order.
(Fatura No lar stringlerle ifade edilirler ama bazı zamanlarda string sırası sayısal sıradan farklı olabilir. )

The procedure in this file makes an invoice number from the data defined in the parameters. The invoice number contains prefix, and number part with leading characters as described below.
( Bu prosedür bir parametre ile verisi belirtiliyor ve ilgili fatura numarasını veriyor. Bu fatura no; bir ön ek ve belli sayıda basamaklı karakterden meydana geliyor)

Prefix - Contains the fix length prefix that is used at the beginning of every invoice number.
(Prefix- ÖnEk , belirlenmiş uzunlukta ve her Fatura No nun başında bulunur)
Number - The number to be generated.
(Numara- Oluşturulmak istenen No )
MaxLen - The maximum length of the number excluded prefix.
(En büyük uzunluk - ÖnEk hariç olması gereken en büyük uzunluk.)
LeadingChar - The number that appears before the number.
(Sabit karakter- Fatura Nodan önce ki sabit karakter , örnekte bu /sıfır/ verilmiş )
Inv_Num - The returning value.
(Geriye dönecek değer)

Son örnek ise:
There needs to be a way to retrieve a fixed number of records from a result set using SQL.
(SQL yardımı ile bir tablodaki belli sayıda ki kayıtları döndürecek yöntem )

Solution: (çözüm)
One way to do this is to create a stored procedure that performs the query and keeps track of how many records it has fetched. After the limit has been reached, the procedure should end.
(Bunu yapmanın bir yolu, bir S.P. oluşturmak, bu s.p. kaç tane kayıt getirileceğini takip etsin ve verilen limite ulaşıldığında sona ersin)

Here's a procedure that returns the employee number, first name, and last name from the records in the employee table. The parameter rows specifies how many records the procedure returns, starting with those who have last names at the beginning of the alphabet, or the whole result set, whichever is smaller.
(Aşağıda employee number,first name ve last name i employee tablosundan getiren s.p var. Parametre satırı, kaç tane kayıt getirileceğini söylüyor (Last Name e göre sıralı olarak veya en küçük olan bütün sonuçlar ). )

:lol:
Kullanıcı avatarı
husonet
Admin
Mesajlar: 2962
Kayıt: 25 Haz 2003 02:14
Konum: İstanbul
İletişim:

Mesaj gönderen husonet »

Oğuzcum Eline Sağlık şimdi çok daha güzel oldu.

Teşekkür Ederim.

Gazete manşetleri
* DİKKAT :Lütfen forum kurallarını okuyalım ve uyalım...!
* Warez,crack vs. paylaşımı kesinlikle yasaktır.
Cevapla