Skip to content

How to run a query with multiple statements? #1090

@curiosport

Description

@curiosport

I would like to execute 2 queries in a single statement that affect two different tables.

This is what I have and it works:

field := "Something"

_, _ = dbpool.Exec(
	context.Background(),
	`DELETE FROM example.table1 WHERE oneColumn = ($1);`,
	field,
)

_, _ = dbpool.Exec(
	context.Background(),
	`INSERT INTO example.table2 (otherColumn) values ($1);`,
	field,
)

This is what I want:

field := "Something"

_, _ = dbpool.Exec(
	context.Background(),
	`DELETE FROM example.table1 WHERE oneColumn = ($1); INSERT INTO example.table2 (otherColumn) values ($1);`,
	field,
)

And this is what I get:
"cannot insert multiple commands into a prepared statement"

It is not in my interest to have them run simultaneously I just want to simplify the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions