Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:46:02+00:00 2026-06-09T18:46:02+00:00

I have a function with a SELECT using a SIMILAR TO expression with a

  • 0

I have a function with a SELECT using a SIMILAR TO expression with a variable and I don’t know how to do it:

DECLARE pckg_data cl_data;
DECLARE contacts contacts_reg%ROWTYPE;
DECLARE sim_name varchar;
 BEGIN
SELECT client_reg._name, 
       client_reg.last_name, 
       client_reg.id_card, 
       client_reg.address
INTO pckg_data
FROM client_reg WHERE(client_reg._name = (cl_name ||' '|| cl_lastname));
RETURN NEXT pckg_data;

SELECT ('%'||cl_name || ' ' || cl_lastname ||'%') INTO sim_name;

FOR contacts IN SELECT contacts_reg.id 
FROM contacts_reg, contactscli_asc, client_reg
WHERE(contacts_reg._name SIMILAR TO sim_name) LOOP
    SELECT client_reg._name, client_reg.last_name, client_reg.id_card,
                   client_reg.address, client_reg.id
    INTO pckg_data
    FROM client_reg, contactscli_asc WHERE(contactscli_asc.contact = contacts.id 
    AND client_reg.id = contactscli_asc.client);
END LOOP;

END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:46:03+00:00Added an answer on June 9, 2026 at 6:46 pm

    Your query that feeds the loop has CROSS JOIN over three (!) tables. I removed the last two on the notion that they are not needed. One of them is repeated in the body of the loop. Also consider @kgrittn’s note on CROSS JOIN.

    In the body of the loop you select data into a variable repeatedly, which does nothing. I assume you want to return those rows – that’s what my edited version does, anyway.

    I rewrote the LOOP construct with a simple SELECT with RETURN QUERY, because that’s much faster and simpler.

    Actually, I rewrote everything in a way that would make sense. What you presented is still incomplete (missing function header) and syntactically and logically a mess.

    This is an educated guess, no more:

    CREATE FUNCTION very_secret_function_name(cl_name varchar, cl_lastname varchar)
      RETURNS TABLE (name varchar, last_name varchar,
                     id_card int, address varchar, id int)
      LANGUAGE plpgsql AS
    $func$
    DECLARE
       _sim_name varchar := (cl_name ||' '|| cl_lastname);
    BEGIN
       RETURN QUERY
       SELECT c._name, c.last_name, c.id_card, c.address, NULL::int
               -- added NULL for an id to match the second call
       FROM   client_reg c
       WHERE  c._name = _sim_name;
    
       RETURN QUERY
       SELECT c._name, c.last_name, c.id_card, c.address, r.id
       FROM   client_reg      c
       JOIN   contactscli_asc a ON a.client = c.id
       JOIN   contacts_reg    r ON r.id = a.contact
       WHERE  r._name LIKE ('%' || _sim_name || '%');
    END
    $func$;
    

    Else, consider the features used.

    Some advise:

    • You can assign a variable at declaration time.

    • The keyword DECLARE is only needed once.

    • Use table aliases to make your code easier to read.

    • You don’t have to enclose the WHERE clause in parenthesis.

    • Most likely you don’t need SIMILAR TO and LIKE does the job faster. I never use SIMILAR TO. LIKE or regular expressions (~) do a better job:

      • Pattern matching with LIKE, SIMILAR TO or regular expressions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function called GetUserByOpenId I don't want to be using this function
I have the following function for SQLite select statement public Boolean itemFound(String cartId,Long ItemId){
suppose i have a .on() function wherein i select multiple ids $(#i, #am, #your,
I have a function of activation code function active($activationcode) { $sql = SELECT *
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
Does DISTINCT in a simple query with an aggregate function have any effect? select
I have this jQuery script that works fine: $(select).change(function () { var finalPrice =
I have the following code: function isValidAuthor($authorID){ $query = SELECT * FROM jos_users WHERE
I have an MS SQL function that is called with the following syntax: SELECT
I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.