0

I was wondering if I could get some help. I have the following function in Postgresql which is designed to give me a "solrid" value without having to store it each time in the database, and basically works by putting an "A" at the start of the value.

-- Setup Solrid Function
CREATE OR REPLACE FUNCTION solrid(IN local_id INTEGER, OUT result TEXT) AS $$
DECLARE
    database_id TEXT := 'A';
BEGIN
    result := database_id || local_id::TEXT;
END;
 $$ LANGUAGE PLPGSQL;

I now have to move my database to MySql and I was wondering if anyone could tell me how I can convert this function to work with MySQL.

8
  • Can you provide sample input and expected result? Commented Mar 7, 2013 at 11:05
  • 5
    Just FYI, that doesn't need to be written in PL/PgSQL, the simple SQL function would be CREATE OR REPLACE FUNCTION solrid(integer) RETURNS text AS $$ SELECT 'A'||$1; $$ LANGUAGE SQL . This would be a heck of a lot faster and could often be inlined away. Commented Mar 7, 2013 at 11:18
  • It is a way of getting my job table "id" into a solr_id without having to store the solr_id. The job "id" might be 123 and the solr_id would be A123. Commented Mar 7, 2013 at 11:22
  • Craig can you add this as an answer please? Commented Mar 7, 2013 at 11:32
  • @CraigRinger Also, can you explain where it references the initial ID? I,e where is the table jobs "id"? Commented Mar 7, 2013 at 14:15

1 Answer 1

1

Here is the same function in MySQL:

DELIMITER //

DROP FUNCTION IF EXISTS solrid //

CREATE FUNCTION solrid(local_id INTEGER) RETURNS TEXT
BEGIN
  DECLARE database_id TEXT;
  SET database_id = 'A';
  RETURN CONCAT(database_id, CAST(local_id AS CHAR));
END //

DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.