ansible-mssql-query icon indicating copy to clipboard operation
ansible-mssql-query copied to clipboard

How to execute a complex SQL statement or to compile a procedure or a function.

Open ameyaagashe opened this issue 7 years ago • 4 comments
trafficstars

Hello there,

Can you kindly provide us with an example of how to execute a complex SQL Statement, or to compile a procedure or a function?

Is there any way we can execute a SQL file?

Kindly assist.

Thank you

Ameya Agashe

ameyaagashe avatar Nov 20 '18 12:11 ameyaagashe

This works for me. Pay attention for double quotes: " ' SELECT ... ' "

  • name: MSSQL query mssql_query: login_host: (serverhostname) query: "{{ wholeline }}"

ansible-playbook MSSQL_query.yml -i hosts -e wholeline="'SELECT * FROM dbo.Table1 WHERE Hostname IS NULL'"

zigaaa avatar Dec 06 '18 10:12 zigaaa

@zigaaa Thanks for that but my question is how we go for a SQL procedure or function spreading over multiple lines?

ameyaagashe avatar Dec 10 '18 06:12 ameyaagashe

I think you can use ansible lookup module to load the sql file and pass that as the query argument.

riyasyash avatar Mar 06 '19 06:03 riyasyash

I think you can use ansible lookup module to load the sql file and pass that as the query argument.

Hi,

Trying to use your module like so:

- name: enable backup
  mssql_query:
    login_user: sa
    login_password: "{{ sql_admin_password }}"
    db: msdb
    query: >
          EXEC msdb.managed_backup.sp_backup_config_basic   
          @enable_backup = 1   
          ,@database_name = '{{ item }}'
          ,@container_url = '{{ url }}'
          ,@retention_days = 4
  loop: "{{ db_list }}"

However the query times out. A quick debug shows that the output it not correct.

debug:

ok: [XX.XX.XX.XX] => (item=DBNAME) => {
    "msg": "EXEC msdb.managed_backup.sp_backup_config_basic   \n@enable_backup = 1   \n,@database_name = 'DBNAME'\n,@container_url = 'https://querytestbackupstorage01.blob.core.windows.net/backupcontainer'\n,@retention_days = 4\n"
}

Using loops make is rather difficult to use this module :)

nicolaibaralmueller avatar Mar 20 '20 08:03 nicolaibaralmueller