-2

I need a command in a .sql file for SQL Server 2012 that lets me run strings as commands, like:

set @command= 'create table mytable (...);';
run(@command);

Preferably with some kind of string format for strings and/or numbers. Usually I do this in C# but I was wondering if I can keep it all in a .sql file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rnunes
  • 2,785
  • 7
  • 28
  • 56
  • 1
    A `.sql` file is nothing but a plain text file, so of course you can. Load the text file, read the statements, and execute them as you would any other SQL statement or script. – Ken White Oct 29 '14 at 17:54

1 Answers1

0

Yes you can, save your SQL command in a *.sql file say test.sql and use SQLCMD Utility to run the sql file in a command prompt like below

sqlcmd -S <ComputerName>\<InstanceName> -i test.sql -o result.txt

Not sure but if you mean running the *.sql file in your code behind; then I would suggest, create a stored procedure and pull up your SQL queries in that stored procedure. Then you can simply call that procedure in your application code instead of reading the SQL file and running them one by one.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rahul
  • 76,197
  • 13
  • 71
  • 125