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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.