batch file - Using one variable in the SET statement of another variable -


i writing batch script uses sqlcmd dump tables database server onto local machine. construct query such have flexibility specify table name. query gets constructed depending on table name, gets used in sqlcmd command. code snippet construct query shown below:

@echo off set tablename = testdb set dumptable="set nocount on; select * %tablename%" echo %dumptable% 

on running script getting following output

"set nocount on; select * " 

the tablename variable not getting substituted in set statement. how should modify script achieve output:

"set nocount on; select * testdb" 

you must not have whitespace around = sign in variable assignments. replace this:

set tablename = testdb 

with this:

set tablename=testdb 

also, shouldn't have double quotes around value in variable assignment, because way double quotes become part of value. it's better practice put double quotes around whole assignment, , quote variable necessary when it's used, e.g.:

set "tablename=testdb" set "dumptable=set nocount on; select * %tablename%" sqlcmd -d database -q "%dumptable%" 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -