0

How do I create a table in SQL Server from a .sql? I see the query statements and the data to be inserted into the table but how do I create the actual tables?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Amen Ra
  • 2,843
  • 8
  • 47
  • 85

3 Answers3

1

You can run it from the command prompt using the command below:

sqlcmd -S myServer\instanceName -i C:\myScript.sql

http://msdn.microsoft.com/en-us/library/ms170572.aspx

This assumes that your .sql files contains the logic to create the needed tables.

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
1

You can create tables from a sql script like so.

CREATE TABLE MyTable1 (
      MyString1 NVARCHAR(50) NOT NULL,
      MyInt1 INT NULL NULL
)
GO
CREATE TABLE MyTable2 (
      Id INT IDENTITY(1,1) NOT NULL,
      Name NVARCHAR(50) NOT NULL,
      PRIMARY KEY(Id)
)
GO

See http://msdn.microsoft.com/en-US/library/ms174979%28v=SQL.90%29.aspx for more info

redec
  • 577
  • 2
  • 13
1

If the statements to create the tables aren't in the .sql file, then you will need to know their structure and create them, either by using a handwritten query, another .sql file or SQL Server Management Studio (SSMS).

In SSMS you can expand a database in the Object Explorer and then right click on "Tables" to select "New Table..." then you will get a UI for defining the columns you need.

With the context of your previous question you need to contact whoever supplied the .sql files and ask for a script to create the required tables. Or perhaps they should send you a copy(a backup) of the database.

Community
  • 1
  • 1
Jamie F
  • 23,189
  • 5
  • 61
  • 77