1

What is the correct way to generate a new id as varchar? Here I'm trying to convert from SQL Server to MySQL database.

Help needed.

I tried internet but it did not solve my problem.

--*GENERATES ANY ID WHEN NEED*
DELIMITER //

CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
    DECLARE lastvalue VARCHAR(10);
    DECLARE i INT;
    DECLARE newId VARCHAR(10);

    IF needTable = 'Item'
            SELECT lastvalue = MAX(resourceID) FROM Item;

            SELECT MAX(resourceID) INTO lastvalue FROM Item;

            IF IS NULL(lastvalue)
                SET lastvalue = 'I00000000';
                SET i = RIGHT(lastvalue,9) + 1;
                SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9);

            RETURN newId;
END; //

DELIMITER ;

SELECT getNewID ('Item');

DROP FUNCTION getNewID

The error says:

Error code 1064, SQL state 42000
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT MAX(resourceID) INTO lastvalue FROM Item;

IF IS NULL(lastval' at line 10
Line 3, column 1

Execution finished after 0 s, 1 error(s) occurred.

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
lizardkingLK
  • 25
  • 2
  • 8
  • 1
    `SELECT lastvalue = MAX(resourceID) FROM Item;` not a valid MySQL query, just comment that line, since the next line is what MySQL valid query. And much more regarding IF inside function. – James Jul 21 '19 at 10:37
  • 2
    That error isn't a SQL Server; but I'm not even sure that SQL is valid MySQL. What are you *actually* using here? I've removed both tags, please retag only the RDBMS you are really using. Thanks. – Thom A Jul 21 '19 at 11:28

1 Answers1

1

Try this

DELIMITER //

CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
    DECLARE lastvalue, newId VARCHAR(10);
    DECLARE i INT;

    SELECT MAX(resourceID) INTO lastvalue FROM Item where needTable = 'Item';

            IF(lastvalue IS NULL) THEN
                SET lastvalue = 'I00000000';
                SET i = RIGHT(lastvalue,9) + 1;
                SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9); -- what you are trying to do here?
                END IF;
-- what you need to return whenlastvalue is not null?
            RETURN newId;
END //

DELIMITER ;
James
  • 1,819
  • 2
  • 8
  • 21
  • this function should give I000000001,I000000002 as item IDs. Convert() does that concatanation with two data types into varchar and return that newID – lizardkingLK Jul 21 '19 at 14:45
  • 1
    Just check mysql manual for convert syntax it's little different – James Jul 21 '19 at 14:49
  • when one to more tables present I could return various ids in SQLServer. I mean if I request the need table Item it gave me I000000001 and if I request for Student table it gives me id like S000000001. There are more if clauses I'm gonna check that's the parameter needTable's purpose. – lizardkingLK Jul 21 '19 at 14:51
  • 1
    @ChanukaFernando I know you are converting SQL from sqlserver to MySQL, so just check MySQL docs on related function you need or let us know that the answer satisfied your query? – James Jul 22 '19 at 04:32