---
name: bq_create_routine
kind: function
lang: py
domain: infra
version: "1.0.0"
purity: impure
signature: "def bq_create_routine(client: BQClient, dataset_id: str, routine_id: str, body: str, routine_type: str = 'SCALAR_FUNCTION', language: str = 'SQL', arguments: list[dict] | None = None, return_type: str = '', description: str = '') -> dict"
description: "Crea una routine (UDF scalar, tabla o stored procedure) en BigQuery. Soporta SQL, JavaScript y Python."
tags: [bigquery, gcp, routine, udf, create, google-cloud, python]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: [google-cloud-bigquery]
params:
  - name: client
    desc: "instancia autenticada de BQClient"
  - name: dataset_id
    desc: "nombre del dataset donde se crea la routine"
  - name: routine_id
    desc: "nombre/identificador de la routine dentro del dataset"
  - name: body
    desc: "cuerpo de la routine: expresion SQL, bloque JavaScript o codigo Python segun el lenguaje"
  - name: routine_type
    desc: "tipo de routine: SCALAR_FUNCTION para UDFs escalares, TABLE_VALUED_FUNCTION para UDFs de tabla, PROCEDURE para stored procedures"
  - name: language
    desc: "lenguaje de implementacion: SQL, JAVASCRIPT o PYTHON"
  - name: arguments
    desc: "lista de argumentos, cada uno como dict con claves 'name' y 'data_type' (ej: INT64, STRING, FLOAT64)"
  - name: return_type
    desc: "tipo de dato que retorna la funcion (ej: INT64, STRING); ignorado para PROCEDURE"
  - name: description
    desc: "descripcion opcional de la routine"
output: "dict con routine_id, dataset_id, project, routine_type, language, body, description, created y modified (ISO 8601)"
tested: false
tests: []
test_file_path: ""
file_path: "python/functions/bigquery/routines.py"
---

## Ejemplo

```python
from bigquery.client import bq_auth
from bigquery.routines import bq_create_routine

client = bq_auth("my-project")

# UDF escalar SQL
fn = bq_create_routine(
    client,
    dataset_id="analytics",
    routine_id="double_value",
    body="x * 2",
    arguments=[{"name": "x", "data_type": "INT64"}],
    return_type="INT64",
    description="Duplica un entero",
)
print(fn["routine_id"], fn["routine_type"], fn["language"])
# double_value SCALAR_FUNCTION SQL

# Stored procedure SQL
bq_create_routine(
    client,
    dataset_id="analytics",
    routine_id="refresh_summary",
    body="INSERT INTO summary SELECT * FROM raw WHERE date = CURRENT_DATE();",
    routine_type="PROCEDURE",
)
```

## Notas

Lanza `google.api_core.exceptions.Conflict` (409) si la routine ya existe. Para actualizar una routine existente, eliminarla primero con `bq_delete_routine` y recrearla, o usar `client._client.update_routine()` directamente.

Los `data_type` de los argumentos deben ser constantes de `bigquery.StandardSqlTypeNames`: `INT64`, `FLOAT64`, `STRING`, `BOOL`, `BYTES`, `DATE`, `DATETIME`, `TIMESTAMP`, `TIME`, `NUMERIC`, `BIGNUMERIC`, `JSON`, `ARRAY`, `STRUCT`.

Las routines JavaScript permiten librerias externas via `imported_libraries` (no expuesto en este wrapper).
