zoukankan      html  css  js  c++  java
  • SQLCMD

     http://www.tech-recipes.com/rx/36947/sql-server-execute-sql-script-using-sqlcmd-command-line/?utm_source=tuicool

    SQLCMD command line Utility is an alternative way to execute SQL scripts using the command line. This allows us to execute SQL scripts without touching SQL Server Management Studio (SSMS) GUI.

    The command line has multiple advantages in certain circumstances. Frequently, this process will save your time and mouse clicks. Plus, it is frequently used to run the same files on multiple sql servers with ease.

        To follow along with my example, copy the following code and save it as     CreateDBCompany.sql    file under the     C:Sqlscripts    folder.

    USE Master
    GO
    
    IF EXISTS ( SELECT [name] FROM sys.databases WHERE [name] = 'DBCompany' )
    DROP DATABASE DBCompany
    GO
    
    CREATE DATABASE DBCompany
    GO
    
    USE DBCompany
    GO
    
    IF EXISTS ( SELECT [name] FROM sys.tables where [name]='Employee' )
    DROP TABLE Employee
    GO
    
    CREATE TABLE Employee
    (
    EmpId int,
    Fname varchar(40),
    Lname varchar(40),
    Hiredate datetime,
    Salary int
    )
    GO
    
    INSERT INTO EMPLOYEE VALUES (101,'Vishwanath','Dalvi','10/16/2011',23025);

     

     

    Using the sqlcmd command

    Now that our example is set, we will execute the CreateDBCompany.sql script file using SQLCMD command line.

        1. Click     Run    and type the letters     CMD    . Hit     OK    , and a command prompt window will appear.  

        2. Now, locate the directory     C:sqlscripts    using the following commands:  

         • Change the directory to C drive using     cd c:    .  

         • Go to the folder by typing     cd    followed by a space, and then sqlscripts folder name such as     cd sqlscripts    .

         • See the sqlscripts directory contents using     dir    . You can see our CreateDBCompany.sql file.

        3. Here, we are using SQLCMD to connect to your local sql server database. Through this same process you may connect to any remote sql server on your network.

    SQLCMD -S DALVI2SQLSERVER2012 -E -i CreateDBCompany.sql

      The     -S    switch is for the Server name and is the same one we use to connect using SSMS GUI. Connect to server option. In my case, it is     DALVI2SQLSERVER2012    .    
        The     -E    switch is used for trusted connection.    
        The     -i    switch is for .sql script file name.

    You should now be able to go to SSMS and query the DBCompany database. You will see the employee table with the one record that we had previously created

  • 相关阅读:
    FreeCAD二次开发-makeChamfer创建倒角
    FreeCAD二次开发-makeFillet创建倒圆
    FreeCAD二次开发-Gui.Selection.getSelectionEx() 遍历选中的对象
    FreeCAD二次开发-Part.Ellipse创建椭圆
    FreeCAD二次开发-Part.makePolygon创建多边形
    FreeCAD二次开发-face.extrude创建拉伸
    FreeCAD二次开发-Part.Face创建面
    FreeCAD二次开发-Part.Shape合并几何元素,生成拓扑形状
    FreeCAD二次开发-Part.LineSegment创建直线
    FreeCAD二次开发-Part.Arc创建圆弧
  • 原文地址:https://www.cnblogs.com/hhudata/p/SQLCMD.html
Copyright © 2011-2022 走看看