pass external parameters to sqlcmd via batch

Asked

Viewed 267 times

1

I used in the oracle so, I wonder if you have any similar method for the sqlserver (sqlcmd).

Batch
sqlplus.exe system/123@localhost/xe @..\CRIA_TABELAS.sql "%last_bill%"

file . sql
SELECT * FROM TB_VENDAS_&1

What the oracle interprets
SELECT * FROM TB_VENDAS_20180901

1 answer

1


Rafael, in SQL Server in the FROM clause it is not possible to use variables for the table denomination (or display); only literal. See documentation from from clause.

However, what you request may be possible using script variable, because the -v option of the utility sqlcmd allows the use of something similar to parameter.

Also evaluate the use of -q and -Q options.

-- código #1 - SCRIPT arquivo.sql
SELECT * FROM $(NomeTabela)

and

-- código #2 - BATCH
sqlcmd -v NomeTabela= "TB_VENDAS_20180901" -i arquivo.sql

Code #2 requires adding the authentication part.
I have not tested the above combination. May contain error(s).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.