Thursday, April 25, 2019

Automatically execute a SQL Command from an icon

You'll need SQL Server command line tools to run this.



Assuming:




  • you have a .sql file you want to run, 

  • you have a username/password and all that.



Create a batch file.



  1. In file manager, right-click somewhere in the folder you want to put it in (like Documents\batch) and New->Text file.

  2. Edit the name that comes up and call it [something].bat

  3. Windows may warn you about renaming file extensions.  Click OK.

  4. Right click the new file in file manager and Edit.  It should open in notepad.

  5. Add this text, using your own server, username and password.




sqlcmd -S ".\SQLEXPRESS" -U myUserName -P myPassword -i %1 -d DBName >result.txt
start Notepad.exe result.txt




Save the file.


See that %1 in there?  That means that the filename of  any .sql file you drop onto this bat file's icon will get inserted and the query will run, dropping the results into a text file (>result.txt)  


The bat file then opens notepad and shows the user the results.










...




Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

How to Auto-generate Order Line Item numbers for bulk uploads

 I had a problem where I had 17000 line items to insert into 9000 orders. The system required line item numbers, preferably numbered 1throug...