How do I create tables in VS 2008? - sql

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

Related

Is there any way to create an Excel File using an Stored Procedure?

I have a table with this columns: name, surname, email, location and number with their respective data.
Is possible generate an Excel using an Stored Procedure in Azure SQL Database and store this file in my desktop with the results of my querie? Thanks!
"Is there any way to create an Excel File using an Stored Procedure?": Yes.
An SP can do anything and that you can do with T-SQL. How you create the Excel spreadsheet is up to you; SSIS, SSRS, CLR, xp_cmdshell,etc are just a few methods. You'll just need to put the appropriate query and statements in your SP, ensure you have the correct permissions and ensure the service account(s) have the appropriate permissions.
If you're asking how to create an Excel file using SQL Server, that is a completely different question.
In Azure SQL database, most of the ways to export data to an Excel file with T-SQL, such as xp_cmdshell、OPENROWSET(Managed instance only) are not suppported. Even if there's no error when creating the stored procedure, the error will happen after EXEC it.
You will get the error like:
"XXX is not supported in this version of SQL Server".
For local SQL Server, you can reference this tutorial. This tutorial provides a demo stored procedure code for you.
Azure SQL database also provides many other ways to export the data to excel files.
For example, Export SQL table to Excel using SSMS. Using the sql statements to select the data your want to the excel file.
Or you can connect to the Azrue SQL database from your new excel file, get the data you want with sql statements. Please reference the Azure tutorial: Connect Excel to a single database in Azure SQL database and create a report
Hope this helps.
enter link description here

SQL Server: Create a duplicate of a database without the data

I have a database called AQOA_Core with huge amount of data.
I have a newly created database called AQOA_Core1 which is basically empty. I want to write a query to duplicate AQOA_Core to AQOA_Core1 without the data. I guess to be precise I want to create a skeleton of the primary database into the secondary database.
PS: I use Toad for my database operations.
You can use SQL Server Script Wizard for scripting database objects. You can exclude data, and select the database object types you want to include in your script
Please check the SQL Server guide I referenced above,
I hope it helps 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.

How to do a search and replace of a part of a string in all columns in all tables in an sql database

Is it possible to search and replace all occurrences of a string in all columns in all tables of a database? I use Microsoft SQL Server.
Not easily, though I can thing of two ways to do it:
Write a series of stored procedures that identify all varchar and text columns of all tables, and generate individual update statements for each column of each table of the form "UPDATE foo SET BAR = REPLACE(BAR,'foobar','quux')". This will probably involve a lot of queries against the system tables, with a lot of experimentation -- Microsoft doesn't go out of its way to document this stuff.
Export the entire database to a single text file, do a search/replace on that, and then re-import the entire database. Given that you're using MS SQL Server, this is actually the easier approach. Microsoft created the Microsoft SQL Server Database Publishing Wizard for other reasons, but it makes a fine tool for exporting all of the tables of a SQL Server database as a text file containing pure SQL DDL and DML. Run the tool to export all of the tables for a database, edit the resulting file as you need, and then feed the file back to sqlcmd to recreate the database.
Given a choice, I'd use the second method, as long as the DPW works with your version of SQL Server. The last time I used the tool, it met my needs (MS SQL Server 2000 / 2005) but it had some quirks when working with database Roles.
In MySQL, you can do it very easily like this:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
I have personally tested this successfully on a production server.
Example:
update users set vct_filesneeded = replace(vct_filesneeded,'.avi','.ai');
Ref: http://www.mediacollege.com/computer/database/mysql/find-replace.html
A good starting point for writing such a query is the "Search all columns in all the tables in a database for a specific value" stored procedure. The full code is at the link (not trivial, but copy/paste it and use it, it just works).
From there on it's relatively trivial to amend the code to do a replace of the found values.

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

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