Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219· AnthropicHugging Face LMArena Human Preference 140K

I have sql query part [code] but get error [code] I see redundant brackets. How to…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-no-thinking’s answer.
  1. User

    I have sql query part tbl_name RECORD; FOR tbl_name IN (SELECT table_name FROM information_schema.tables WHERE table_catalog = db_name and table_schema = current_schema_name) LOOP -- and table_type = 'BASE TABLE' | and table_type = 'VIEW' EXECUTE format('GRANT SELECT ON TABLE %I.%I TO %I;', current_schema_name, tbl_name, role_name); RAISE NOTICE 'Granted SELECT ON TABLE `%.%` to role `%`', current_schema_name, tbl_name, role_name; END LOOP; but get error SQL Error [42P01]: ERROR: relation "access.(pbi_rls)" does not exist Where: SQL statement "GRANT SELECT ON TABLE access."(pbi_rls)" TO role_am_db_reader;" I see redundant brackets. How to remove them?

  2. Claude Assistant

    The error you're encountering is due to the way PostgreSQL is handling the tbl_name variable in your EXECUTE format statement. When you use a record variable directly in the format string, PostgreSQL is treating the entire record as a string, including the parentheses.

    To fix this issue, you need to access the table_name column from the tbl_name record:

    DO $$
    DECLARE
        tbl_name RECORD;
        db_name TEXT := 'your_database_name'; -- Replace with your database name
        current_schema_name TEXT := 'access'; -- Replace with your schema name
        role_name TEXT := 'role_am_db_reader'; -- Replace with your role name
    BEGIN
        FOR tbl_name IN (SELECT table_name FROM information_schema.tables WHERE table_catalog = db_name AND table_schema = current_schema_name) LOOP
            -- and table_type = 'BASE TABLE' | and table_type = 'VIEW'
            EXECUTE format('GRANT SELECT ON TABLE %I.%I TO %I;', current_schema_name, tbl_name.table_name, role_name);
            RAISE NOTICE 'Granted SELECT ON TABLE `%.%` to role `%`', current_schema_name, tbl_name.table_name, role_name;
        END LOOP;
    END $$;
    

    In this corrected version:

    1. The tbl_name variable is declared as a RECORD type.
    2. The FOR loop iterates over each row returned by the SELECT query, assigning each row to tbl_name.
    3. Within the loop, tbl_name.table_name is used to access the table_name column from the current row.
    4. The EXECUTE statement uses the format function to safely construct the GRANT SELECT statement, ensuring proper quoting of identifiers.

    This should resolve the error and correctly grant SELECT privileges on the specified tables to the role_name. Make sure to replace your_database_name, access, and role_am_db_reader with the actual values you intend to use.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.