1 Replies - 1015 Views - Last Post: 29 December 2016 - 05:12 PM

#1 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Using sqlcmd to store query columns in script variables

Posted 29 December 2016 - 01:59 PM

I'm trying to create an empty file who's file name is derived from 3 columns of the query.

I can use this to display the results I want, but not stored in variables.
sqlcmd -S MyServer -Q "SET NOCOUNT ON; select fname, lname, mname  
	from MyTable go"


Or I could use this query to store a single column to variable a. I don't think I can extend this command to store the other columns.
for /f %a in ('sqlcmd -S MyServer -Q "SET NOCOUNT ON; select fname, lname, mname from MyTable go"') do set ColumnVar=%a


I also thought maybe I could write a query to combine the 3 columns into 1 so it could work with the previous command but the column wasn't large enough to store the 3 column values.

Any idea of how I can store the query results in a script variable?

Is This A Good Question/Topic? 0
  • +

Replies To: Using sqlcmd to store query columns in script variables

#2 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Re: Using sqlcmd to store query columns in script variables

Posted 29 December 2016 - 05:12 PM

Nevermind I'm going to do this in C#. I don't know why my initial reaction was to do it from command line.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1