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

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

Related

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

Sql: export database using TSQL

I have database connection to database DB1. The only thing I could do - execute any t-sql statements including using stored procedures. I want to export the specific table (or even the specific rows of specific table) to my local database. As you can read abve, DBs are on diffrent servers meaning no direct connection is possible. Therefore question: Is it possible to write query that returns the other query to execute on local server and get data? Also note, that table contains BLOBs. Thanks.
If you have SQL Server Management Studio, you can use the data import function on your local database to get the data. It works as long as you have Read/Select access on the tables you are trying to copy.
If you have Visual Studio you can use the database tools in there to move data between two servers as long as you can connect to both from your workstation.
Needs Ultimate or Premium though:
http://msdn.microsoft.com/en-us/library/dd193261.aspx
RedGate has some usefull tools too:
http://www.red-gate.com/products/sql-development/sql-compare/features
Maybe you should ask at https://dba.stackexchange.com/ instead.
If you can login to the remote db (where you can only issue t-sql), you may create linked server on your local server to the remote and use it later directly in queries, like:
select * from [LinkedServerName].[DatabaseName].[SchemaName].[TableName]

Microsoft SQL Server: How to export data from a database and import them to another database?

How can I export all of my rows in a table to sql script in Microsoft SQL Server 2005 and then import them to another database?
Thanks in advance
If you moving it to another sql db you can right click the database you want and choose tasks -> generate scripts. That will launch a a wizard - follow along, choose the option to script all tables and data. Then execute that script in the new db(assuming that you've already created one with the same name)
If you can't find a data import/export tool that will work in your particular circumstances, it's possible to write plain SQL SELECT queries that will generate SQL INSERT statements. In this way it's possible to "export" all your data to a script file that can be run against the destination database. It's kind of an ugly hack, but it's simple and it works if you don't have a lot of data to move. See my answer to this question for details: Export SQL Server 2005 query result to SQL INSERT statement?
Note that this method assumes that the destination table already exists. But it's pretty straightforward to generate table creation scripts, as J Cory's answer has already shown.
There's a command line tool available to dump your data from particular tables into a SQL script that be executed against a different database:
http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/
I don't believe SQL Management Studio Express supports data scripting (as your screenshot on J Cory's answer shows), but the full version does support that feature. In either case, the command line tool should accomplish what you need.

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

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.