How can I generate the SQL CREATE TABLE statement for an MDF table? - sql

I have an .mdf file with Visual Studio 2008 with one table in it that has numerous columns of various types.
How can I generate the SQL statement that produces this table so that I can alter it and create another similar table? Is there a way to do this without SQL Server?

And MDF is a whole SQL Server database - not just a single table.
In SQL Server Management Studio, you can open the "Object Explorer" (View -> Object Explorer or press F7) and then select your database, right-click on it, pick "Tasks..." and then "Generate Scripts ....".
This will launch a wizard which allows you to script out all tables, views, stored procs and any object db objects you might have in your database.
If you're interested in just a single table, drill down into your database and then into tables and find your table in the Object Explorer, and then right click on the table. Pick "Script table as...." and then "Create To....." and you can have a CREATE TABLE script for just that single table in a file, a new query window in SSMS, or on the clipboard for your convenience.
As far as I know, you cannot do this inside Visual Studio, however - you will need to use SQL Server Management Studio (or the Express version of it for SQL Server Express databases).
Marc

Related

Copying an SQL table from one Server to another on SQL Server 2000 / 2005

I’m trying to copy a SQL Server table, schema and data, from Server A to Server B. The SQL Server table is just a reference table which hasn't been populated for some reason on Server B. Can anyone advise how the entire table could be copied across please? On SQL Server 2000/2005.
So far we've tried a long-winded approach by copying the .mdf and .ldf files from Server A to Server B with a plan to then copy the table across into the Server B database but we are having some difficulty re-attaching the database to Server B.
Please can anyone help?
Kind Regards
James
Using SQL Server Management Studio (SSMS):
In Object Explorer right click on source database name, Tasks.. -> Generate Scripts.. - opens Generate and Publish Scripts dialog. Click Next to choose objects, choose "Select specific DB objects", expand Tables, choose your table. Next, setup script destination, for example New query window and (important step!!) - click Advanced, and set "Types of data to script"="Schema and data" and "Script USE DATABASE"=False, OK, Next, Next, .. wait .. Finish. Now you have got complete SQL script to reproduce this table with data. Connect to destination DB and run it.
Tested with SSMS 2014, but as I recall this feature should be available starting from SSMS 2005.
you can use the import/export data wizard in management studio, the wizard will create for you a new table in the server B with the same structure of the table in the server A. before using it you need to have at least one database in sever B.
This confirms why this is one of favourite forums.
Both these methods work beautifully :
Generate Scripts (when altering Types of data to script"="Schema and
data")
Export and Import
Interestingly Generate Scripts works with SQL Express perfectly but the Export method does not save unless you have at least SQL Server Standard Edition.
Thanks so much everyone
Cheers
James
Try this:
SELECT * INTO destination FROM source
But, it will not copy the indexes and key information or you can also try import/export data task from SSMS.

In SQL Server 2000 how to convert table to text file

In SQL Server 2000, I need to convert a table data (whole, not partial) to a text file (csv or tab).
How can I do this with a table (tblCustomer) which has name, address, phone number, for example?
I don't use SQL Server Management Studio. I have many groups of stored procedures running on daily basis and just want to write a stored procedure to add in to one of the groups.
Are you using SQL Server Management Studio?
If so, just right click on the database name, and choose tasks->export data from the context menu, then follow the wizard.
You you are using Management studio, you can try this:
In your SQL Server Management Studio, right-click on the database where the table is and select Tasks->Export Data. This will bring up
the SQL Server Import and Export Wizard, which is very much similar to
the DTS Import/Export utility of SQL Server 2000.
Select your table from the "Choose a Data Source" screen then click on Next.
In the "Choose a Destination", select "Flat File Destination" as your Destination and enter the File name.
Just click on Next after that and you should be ok already.
You can also export it to xml using a query and then, on your app, convert it do csv. Check this out: FOR XML
Or, as #Aaron said, use The BCP Utility
EDIT
As you said you want the query way, I would go for the For XML and them parse the result to a CSV format. AFAIK, SQL server does not have an way of doing what you want automatically, so you will have to make it yourself. Also, you can adapt this sample.
Here and here you can find some scripts that may help you.

create sql server database as other sql database structure (design)

I need to create sql database just like other sql database (without the data) via sql script
some one told me that Oracle has this ability by some code like
create database <your new DB name> as <the old DB name>
so is there a similar statement or workaround in SQL
I use MS-SQL Server 2008/2008R2
I don't want the 'generate scrip' solution as this provide a very long script, I just need the Oracle statement (mentioned above) but in SQL
to be more clear I need the tables structure only (no data) and need all other objects such as functions, views etc...
Please advice,
In SQL Server, you can right click on the database then select "Tasks" then "Generate Scripts" and you can build one script for all your object sans data.
If you are asking about SQL Server (at least in 2005 or newer, not sure about previous versions) if you right click on a Database in SQL Server Management Studio Go To Tasks and then Generate Scripts you have the option to Generate a script to create all the object ins the Database w/o Data.
Update:
You can also right click on a User Database and select Script Database as -> Create To just to get the TSQL to create the DB itself without any of the tables, stored procedure etc.

Create SQL script that create database and tables

I have a SQL database and tables that I would like to replicate in another SQL Server. I would like to create a SQL script that creates the database and tables in a single script.
I can create "Create" script using the SQL Management Studio for each case (Database and Tables), but I would like to know if combining the both "Create" scripts into single script would be enough.
Thanks.
Although Clayton's answer will get you there (eventually), in SQL2005/2008/R2/2012 you have a far easier option:
Right-click on the Database, select Tasks and then Generate Scripts, which will launch the Script Wizard. This allows you to generate a single script that can recreate the full database including table/indexes & constraints/stored procedures/functions/users/etc. There are a multitude of options that you can configure to customise the output, but most of it is self explanatory.
If you are happy with the default options, you can do the whole job in a matter of seconds.
If you want to recreate the data in the database (as a series of INSERTS) I'd also recommend SSMS Tools Pack (Free for SQL 2008 version, Paid for SQL 2012 version).
In SQL Server Management Studio you can right click on the database you want to replicate, and select "Script Database as" to have the tool create the appropriate SQL file to replicate that database on another server. You can repeat this process for each table you want to create, and then merge the files into a single SQL file. Don't forget to add a using statement after you create your Database but prior to any table creation.
In more recent versions of SQL Server you can get this in one file in SSMS.
Right click a database.
Tasks
Generate Scripts
This will launch a wizard where you can script the entire database or just portions. There does not appear to be a T-SQL way of doing this.
An excellent explanation can be found here: Generate script in SQL Server Management Studio
Courtesy Ali Issa Here's what you have to do:
Right click the database (not the table) and select tasks --> generate scripts
Next --> select the requested table/tables (from select specific database objects)
Next --> click advanced --> types of data to script = schema and data
If you want to create a script that just generates the tables (no data) you can skip the advanced part of the instructions!
Not sure why SSMS doesn’t take into account execution order but it just doesn’t. This is not an issue for small databases but what if your database has 200 objects? In that case order of execution does matter because it’s not really easy to go through all of these.
For unordered scripts generated by SSMS you can go following
a) Execute script (some objects will be inserted some wont, there will be some errors)
b) Remove all objects from the script that have been added to database
c) Go back to a) until everything is eventually executed
Alternative option is to use third party tool such as ApexSQL Script or any other tools already mentioned in this thread (SSMS toolpack, Red Gate and others).
All of these will take care of the dependencies for you and save you even more time.
Yes, you can add as many SQL statements into a single script as you wish. Just one thing to note: the order matters. You can't INSERT into a table until you CREATE it; you can't set a foreign key until the primary key is inserted.

How do I create tables in VS 2008?

I am running VS 2008 Standard and SQL Server Express. I created the tables in the Database Designer (creating and xsd), and created the database. How do I get the tables from the Database Designer into the database? Also, is there some documentation available?
Thanks!
My original flow would've been to:
use Server Explorer in Visual Studio to create tables, views and stored procs.
Create the .xsd model 'from database' instead of by hand.
The xsd really is the model for how the data is shaped in the application, rather than in storage at the database.
Unsure if there is a way to explicitly export/create SQL scripts from your .xsd designer. If it was enough of a time-saver, you could:
create a new TSQL query, copy paste the XSD into a varchar(max) variable.
use OPENXML to get a set of the names of the tables & their columns.
create SQL statements for CREATE TABLE