What is .sql file in SQL Server? - sql-server

I have heard about .sql file in SQL Server, But i don't know what it holds also is this file helpful for recovery purpose?

.sql file is the script file of sql server that contain the all command of particular database...
Including
- Create
- Select
- Insert
- Store Processor
- View

SQL, or Structured Query Language files contain SQL code used to modify the contents of a database. They may contain statements for creating or modifying database structures, insertions or updates, deletions.
Your result will be based on what your code in .SQL says.

Related

Does BCP create the destination object?

I apologize if I have a fundamental misunderstanding but basically I wanted to know if there is a way to go directly from ONLY .bcp files to creating a SQL Server database. I have not dealt with .bcp files before and have no format files or know anything about the schema of the database we are trying to re-create. Is there some sort of utility wihtin SQL server management studio that can do what I am asking or do I just not have enough resources to create a database out of this data. Any help is appreciated.
BCP only builk insert the data. So you need to have a blank database first to insert to with the bcp files.
You can use the bcp command line to run with those bcp files. But you need to know the order of the files to run if the db has FKs.

Copy data from one table and save it into another table in different database on different SQL Server

I have two different databases in two different SQL Servers. The databases are identical in schema but contain different data in one of the tables.
I want to copy all the data from one table in one database to the same table in the other database so that I can get rid of the database from which I am copying the data.
The data is too large so I cannot create data scripts and run it onto other database.
How can I achieve this?
There are many ways like ssis transfer,select * into ,but i prefer below way if you are just transferring data
create a linked server on source server for destination server,then you could refer destination server with four part name
Assuming linked server of source is A and destination server is B,data moving is as simple as
insert into B.databasename.Schema.Table
select * from table---this is in source server and db
if data is huge and you may worry about time outs,you can write a simple script which can do in batches like
While (1=1)
begin
insert into B.databasename.Schema.Table
select top 10000* from table---this is in source server and db
if (##rowcount=0)
break
end
Creating linked server ,you can follow this
You have the following options available to you. Not all of these will work, depending on your exact requirements and the networking arrangements between the servers.
SQL Server Management Studio - Import & Export Wizard: this is accessed from the right-click menu for a database > Tasks > Import Data (or Export Data).
SQL query using a Linked Server: a Linked Server configured between the two servers allows you to reference databases on one from the other, in much the same way as if they were on the same server. Any valid SQL query approach for transferring data between two tables within one database will then work, provided you fully-qualify the table names as Server.Database.Schema.Table.
SSIS: create an SSIS package with both servers as connections, and a simple workflow to move the data from one to the other. There is plenty of information available online on how to use SSIS.
Export to flat-file format then import: this could be done using the Import/Export Wizard above or SSIS, but instead of piping the data directly between the two servers, you would output the data from the source table into a suitable flat-file format on the filesystem. CSV is the most commonly used format for this. This file can then be moved to the destination server using any file transfer approach (compressed e.g. to a Zip file if desired), and imported into the destination table.
Database backup and restore: Similar to (4), but instead of using a flat file, you could create a backup of the source database via Tasks > Back Up... You then move that backup as a file (just like the CSV approach), and restore it onto the destination server. Now you have two databases on the destination server, and can move data from one to the other locally.
I hope, this query helps you!!!
INSERT INTO [dbo].[tablename] (Column1, Column2,Column3)
(select Column1, Column2,Column3, from [Database1].[dbo].[tablename]
Thanks!!!

Import database (SQL file) in SQL Server Management Studio

I've created the structure of my database first in PhpMyAdmin and exported it to a .sql file.
Now I'm looking everywhere in SQL Server Management Studio where I can import/add the data in a new database.
Does anybody where to look or what to click?
I'm using the 2014 version (CTP2)
If you have a .sql file which contains SQL statements, you can just copy and paste the contents (or open the file in a query window) and run it. This assumes it has all of the create table etc. statements to create the schema/structure and not just insert statements for the data.
Check the top of the file to make sure that it is first selecting the correct database, if not add a USE statement to select the correct database.
You didn't say how big the file was, but if it is quite large and has the insert statements (data as well as schema), then you'll probably want to run by CLI using sqlcmd command. Much faster and SSMS won't freak out.
Another alternative option to running the .sql file/code is to set up a data source for mysql and just use odbc to access the database itself.
Bear in mind that there are real and very annoying differences between mysql and t-sql that can make migration a pain. If you're just creating a few tables, it may not be an issue, but if there are a ton of tables with lots of fields of different data types, you may run into issues.
If you are looking to import table structure, you can copy-paste the content and run inside SSMS in a query window. Beware of syntax differences with MySQL and SQL Server. You will most likely get errors. You need to convert your SQL script from MySQL dialect to SQL Server dialect (or just add them manually if they are not too many). If you set the databases to a SQL standard-compatibility mode at the very beginning, you will have much less trouble.
If you are ONLY looking just to import the data into existing tables inside the SQL Server only, you can do the same (i.e. copy-paste and run in query window). You will have less trouble with that.
Open the server, open "Databases" and right click the database, go to "Tasks" and then Import Data...
I have had the most 'trouble free' success importing to SQL via a flat file method (comma delimited .txt file), the only stipulation when creating a flat file (i.e from Access) make sure the text identifier is set to {none} and not "".
To import the file: in the SQL Server Management Studio right click on Databases and create a new database. Then right click on the new database -> Tasks -> Import Data... The import window opens: in the DATA SOURCE option select Flat File Source and select the .txt file...click NEXT. In the DESTINATION field select SQL Server Native Client 11.0 and go through the import process. This worked very well for me.

Which is the simple way to export a database from SQL Server including all tables and data into file.sql file?

I'm using SQL Server[s] 2008 R2 [Express].
I would like to create an installation file.sql file for an example database.
In file.sql I mean a file that will run from Microsoft SQL Server Management Studio as a query.
I would like to have a single file including the database itself and all tables and data.
How do I export an entire database into such a file?
P.S.
It is a very small database.
I do not worry about database name duplicate on the new server.
There are Unicode characters and many special characters in data including {[(<`.,'|"?*&/>)]}
In management studio
Right click on Database name
choose
Tasks > Generate scripts
Next, Choose "Script entire database"
Next, go to Advanced
Types of data to script => choose "Schema and Data" (and check other options for your needs)
Select file to save to
Finish.
EDIT :
If it's used in Management Studio only, you'll be faster using Backup and Recovery !
I would just add the caveat that if you have large amounts of data (> 1000 records) then you need to batch the insert statements into groups of <= 1000 inserts at a time. There is a limitation on batch size.
You may also need to pay attention to the order in which inserts occur to avoid constraint conflicts. A safe approach would be to extract the FK constraints into a separate file which is executed last.

How to copy .mdb file to .mdf file

I want to copy the ms access database(.mdb) to sql server database(.mdf). I did it with sql server Import and export data service. But I want it copy the data regularly or a specific time. Is it possible or not. I have tried to create a batch file
copy /y "E:\Dinesh Work\for-reports.mdb" "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\for-reports.mdf"
but it gives the following error:
The process cannot access the file because it is being used by another process.
I have same tables in my sql server database as msaccess database. Is there any solution with batch file or something else.
Any suggestion will be appreciated.
Thanks in Advance.
This sounds like you want to use the Access database to edit the data in the SQL Server database.
Is that correct?
If yes, do you really need to copy the data?
You could also link from Access to the SQL Server tables.
This way, you have tables in Access that look like normal local Access tables, but they are really just links to SQL Server tables. You can edit data in these tables in Access, but you are actually writing directly into the SQL Server database.
Here are some examples how to set this up:
Link to SQL Server data
Access to SQL Server: Linking Tables
MDB and MDF files are wildly different types. You can't just copy them.
You might try setting up an SSIS task to do a regular data transfer - something like ETL if you're familiar with that term.
EDIT: The reason you're seeing the file locked error is because SQL Server maintains that lock on the MDF file while the database is running. In order to move or copy it you need to take that particular database offline.
As #Yuck said, you can not just copy the file and rename it, you need something like ETL or just a tool to export data.
I did xportdsl to copy from a h2database to a mssql and mssql to oracle
http://code.google.com/p/xportdsl/
I used gorm and a hacky dsl that worked and it is still working
You can script a bat file to execute something like this java -jar xportdsl.jar test001.txt

Resources