SQL Server > SQL Server Forums > SQL Server Disaster Recovery and Availability > How do I run a command line SQL command in Windows?
Ask a questionAsk a question
 

AnswerHow do I run a command line SQL command in Windows?

  • Tuesday, November 24, 2009 10:49 AMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I tried to run an ISQL command but I get task or command "not recognized". I need to stop a restore that failed. I am not that familiar with WINDOWS SQL Server.  I am using SQL Server Management Studio, SQL 2005.

    thanks

Answers

  • Tuesday, November 24, 2009 11:03 AMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Use sqlcmd which is the new utility that lets you execute T-SQL commands from the command line. It replaces isql, osql and similar utilities from SQL Server 2000.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:15 PM
    •  
  • Tuesday, November 24, 2009 1:22 PMRicardo Sampei Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Have you tried to logon to the instance with that account using Management Studio? If you get the same error message then something is wrong with the instance.
    Also, are you trying to connect to a named instance? If yes, then you need to use -S servername\instancename.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:16 PM
    •  
  • Tuesday, November 24, 2009 4:17 PMTome Tanasovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try stopping the SQL service.... rename the mdf and ldf files for this db.... start the sql service.... now delete the database....

    You should then be able to restart your restore.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:14 PM
    •  

All Replies

  • Tuesday, November 24, 2009 11:03 AMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Use sqlcmd which is the new utility that lets you execute T-SQL commands from the command line. It replaces isql, osql and similar utilities from SQL Server 2000.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:15 PM
    •  
  • Tuesday, November 24, 2009 11:24 AMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much.  I will give that a shot.
  • Tuesday, November 24, 2009 12:37 PMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Perhaps I am doing something wrong.  The sqlcmd tells me that the connection failed and it could be because the server does not allow remote connections. I checked the properties and remote connections are allowed. I also checked to make sure I was using the correct account to log in.  Is the correct command:

    >sqlcmd -U username -P password -S servername  ??

    Thanks
  • Tuesday, November 24, 2009 1:22 PMRicardo Sampei Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Have you tried to logon to the instance with that account using Management Studio? If you get the same error message then something is wrong with the instance.
    Also, are you trying to connect to a named instance? If yes, then you need to use -S servername\instancename.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:16 PM
    •  
  • Tuesday, November 24, 2009 2:25 PMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    You may by in Windows authentication mode. Try

    sqlcmd -S servername
    

    or activate SQL Server authentication from Management Studio.

  • Tuesday, November 24, 2009 2:26 PMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I can log into the instance from the management studio.  That is how I have been doing things, but now I have a restore that errored out, but will not die. It has been running for two days now. I even rebooted thinking it would kill it but it did not.  I want to copy the database so I can have one to test my restore on while leaving the original in tact, but I can't even do that while it is in "Restoring" mode. 
  • Tuesday, November 24, 2009 2:38 PMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much to all of  you. I figured out, with your help,  that I wasn't really giving it the servername. I was only using part of it. It likes the hostname\servername.
  • Tuesday, November 24, 2009 2:56 PMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well.  Now that I am in, I cannot kill or reset the restore.  I thought just running the restore command with NoRecovery would work. It ran successfully, but my database is still "restoring". I can't do anything to it, because of this restore state. Does anyone know how to reset it?

    thanks
  • Tuesday, November 24, 2009 3:09 PMRadu Negru Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Execute

    RESTORE <database name> WITH RECOVERY;
    
  • Tuesday, November 24, 2009 3:32 PMAnnettaC Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Different, but not desired, results this time.  The RESTORE with RECOVERY, gives me an error that the database cannot be recovered because the log was not restored. RESTORE terminated abnormally.  HOWEVER, when I look in Management Studio, it is still "restoring".  Since it was an empty database, I never bothered to back it up before attempting my restore.  Now I can't even delete it to create it again.  I really need this database restored from this backup.   I know the database backup is a good one, as I was successful in restoring it to a different server but I need it on this one. 

    Thanks again for all the help..
  • Tuesday, November 24, 2009 4:17 PMTome Tanasovski Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Try stopping the SQL service.... rename the mdf and ldf files for this db.... start the sql service.... now delete the database....

    You should then be able to restart your restore.
    • Marked As Answer byAnnettaC Tuesday, November 24, 2009 5:14 PM
    •  
  • Tuesday, November 24, 2009 4:37 PMRicardo Sampei Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you please close this issue and open a new one, with a different subject?