2

I confirmed that the data of the id column of the data inserted with the above sql was returned to the Oracle console window.

declare
  tmp_id number;
begin
  insert into users(nickname, platform, socialKey) values('test', 'test', 'test')
  returning id into tmp_id;
   DBMS_OUTPUT.PUT_LINE(tmp_id);
end;

enter image description here The id value of the normally inserted data row is returned.

func TestTemp(t *testing.T) {
    envSetting()

    db := database.DatabaseInstance()
    defer db.Close()

    var userCount int
    db.QueryRow(`
    SET SERVEROUTPUT ON
    declare
        tmp_id number;
    begin
        insert into users(nickname, platform, socialKey) values('test', 'test', 'test')
        returning id into tmp_id;
        DBMS_OUTPUT.PUT_LINE(tmp_id);
    end;
    `).Scan(&userCount)

    t.Log(userCount)

}

return data :

Running tool: /opt/homebrew/bin/go test -timeout 30s -run ^TestTemp$ github.com/temp/temp/test/auth

=== RUN   TestTemp
    /Users/temp/project/temp/test/auth/user_test.go:169: 0
--- PASS: TestTemp (0.22s)
PASS
ok      github.com/temp/temp/test/auth  0.366s


> 2023. 1. 16. 오전 12:24:50에 테스트 실행이 종료됨 <

However, when I run the above code in golang, only 0 is returned. Could you please check what part is wrong?

madol
  • 551
  • 2
  • 17
  • Don't ignore the error returned by Scan. – Peter Jan 15 '23 at 16:06
  • 3
    your golang code can't get the value of the inserted ID via DBMS_OUTPUT, you need to have the block of code RETURN or OUT the tmp_id for your golang fnc to work with. DBMS_OUTPUT isn't for passing values between programs, it's more for debugging and instrumentation – thatjeffsmith Jan 15 '23 at 18:27
  • Thank you, I understand what you mean, can you tell me what keywords to search for in order to get the data I want in golang? – madol Jan 16 '23 at 04:04
  • Assuming you are using the godror driver, look at the test example for RETURNING INTO: https://github.com/godror/godror/blob/9f2dfd6cc378bb24ce9be4bde757d6851882f4f1/z_test.go#L2024 – Christopher Jones Jan 16 '23 at 21:24
  • To answer the base question, Oracle Database returns the ROWID of new data automatically to language drivers from INSERT, UPDATE etc statements, but it doesn't know your data model so you need to change the statements to use RETURNING INTO if you have application-specific primary keys. – Christopher Jones Mar 10 '23 at 21:43

0 Answers0