1

I tried using DB::unprepared() in migration files after reading old questions. but it only gives me "A non-numeric value encountered" error and nothing changes in database. However, when I enter sql statement directly into phpmyadmin by adding delimiter, it works. please suggest me what I am missing.

DB::unprepared('
    CREATE FUNCTION getNextCustomSeq
    (
        sSeqName VARCHAR(50),
        sSeqGroup VARCHAR(10)
    ) RETURNS VARCHAR(20)
    BEGIN
        DECLARE nLast_val INT;

        SET nLast_val =  (SELECT seq_val
                              FROM _sequence
                              WHERE seq_name = sSeqName
                                    AND seq_group = sSeqGroup);
        IF nLast_val IS NULL THEN
            SET nLast_val = 1;
            INSERT INTO _sequence (seq_name,seq_group,seq_val)
            VALUES (sSeqName,sSeqGroup,nLast_Val);
        ELSE
            SET nLast_val = nLast_val + 1;
            UPDATE _sequence SET seq_val = nLast_val
            WHERE seq_name = sSeqName AND seq_group = sSeqGroup;
        END IF;

        SET @ret = (SELECT concat(sSeqGroup,'-',lpad(nLast_val,6,0)));
        RETURN @ret;
    END//
    ');
Aung
  • 65
  • 1
  • 13
  • The warning is returned from PHP 7.1 [when invalid strings are coerced using operators expecting numbers](http://php.net/manual/en/migration71.other-changes.php). Could you tell us which line is it offending? – Lionel Chan Jan 31 '18 at 04:26
  • from this line SET @ret = (SELECT concat(sSeqGroup,'-',lpad(nLast_val,6,0))); – Aung Jan 31 '18 at 04:31
  • I couldn't find a reliable documentation, but could you try raw the statement first? ie, `DB::unprepared(DB::raw('... your query...'))` – Lionel Chan Jan 31 '18 at 04:36
  • sorry, it does not work either. – Aung Jan 31 '18 at 04:56

1 Answers1

2

Oh lol it's a typo. You forgot to escape the line:

SET @ret = (SELECT concat(sSeqGroup,'-',lpad(nLast_val,6,0)));

Which it should be:

SET @ret = (SELECT concat(sSeqGroup,\'-\',lpad(nLast_val,6,0)));

Updated:

DB::unprepared('
    CREATE FUNCTION getNextCustomSeq
    (
        sSeqName VARCHAR(50),
        sSeqGroup VARCHAR(10)
    ) RETURNS VARCHAR(20)
    BEGIN
        DECLARE nLast_val INT;

        SET nLast_val =  (SELECT seq_val
                              FROM _sequence
                              WHERE seq_name = sSeqName
                                    AND seq_group = sSeqGroup);
        IF nLast_val IS NULL THEN
            SET nLast_val = 1;
            INSERT INTO _sequence (seq_name,seq_group,seq_val)
            VALUES (sSeqName,sSeqGroup,nLast_Val);
        ELSE
            SET nLast_val = nLast_val + 1;
            UPDATE _sequence SET seq_val = nLast_val
            WHERE seq_name = sSeqName AND seq_group = sSeqGroup;
        END IF;

        SET @ret = (SELECT concat(sSeqGroup,\'-\',lpad(nLast_val,6,0)));
        RETURN @ret;
    END//
');

What happened here is you did not escape the quotes and PHP thinks that you are doing 'long chunk of text' - 'long chunk of text', which of course raise the error A non-numeric value encountered. This warning is telling you that you are trying to do mathematical expression on one or more non numeric string: http://php.net/manual/en/migration71.other-changes.php

Lionel Chan
  • 7,894
  • 5
  • 40
  • 69
  • thanks. I also found out it. have to remove // after END too – Aung Jan 31 '18 at 05:17
  • Pardon me. Can u please kindly check my another question. https://stackoverflow.com/questions/48376840/custom-id-primary-key-cannot-be-retrieved-after-laravel-save-method . thanks – Aung Jan 31 '18 at 06:49