Generate selected table DDL script - sql-server

I have 100 tables in 6 schema. I want to create DDL (create table) script for 25 tables belong to all 6 schema. All are in one database.
I am new to SSMS (SQL Server 2018). I know its easy in Oracle. I am not getting a way how to get exact DDL only for some tables.
Can someone provide the script if handy?

Right click the database name in SSMS. Choose Tasks-> Generate Scripts. Click that you want to select only certain objects. Within the next dialog, there is an advanced button that allows you to script schema only, or schema and data both.

Related

SQL Server tables not showing without schema [Jetbrains Datagrip]

Whenever I create tables they don't show on the side database explorer. I always have to create a schema first under the database and then:
CREATE TABLE schema.TABLENAME(.....)
I find this kind of annoying, tables do exist and you can call them but they just won't show on the side explorer if they are not under some schema. Is this normal on DataGrip?

Copy a number of views from a linked Oracle database to create tables in SQL Server

I have an Oracle linked server in SQL Server and would like to copy the contents of a number of views to a database in SQL Server, these views from Oracle are to become tables in SQL Server. I have done this one at a time but am looking for a solution to be able to refresh these views, 104 of them, overnight every night.
I am fine with setting the job running manually but am looking for a solution that will either drop and recreate the tables from the views or that will just refresh the data in the SQL Server tables that exist.
Hope I have explained this well enough!
Many thanks in advance for any help on this one.
If you don't already have the table structures in the MSSQL database, I'd say go through one time for all 104 views, and say the following:
SELECT *
INTO MSSQLNewTable (this will be the name of your new table)
FROM <However you reference your Oracle view from within MS SQL Server>
After you do that, then create a SQL Script that says:
TRUNCATE TABLE MSSQLTable_Name
INSERT INTO MSSQLTable_Name
SELECT * FROM OracleTable_Name
.....for each table. Create a job in the database instance that runs on a schedule you set.
use the sys. tables to generate the statements so you don't have to type everything 104 times.

How to export a Table's schema to another Database?

I have 10 tables in my database. Out of this I want to generate 5 tables with same structure and dependencies in a different database. I don't need to copy the data inside these tables, just the schema of tables. How do I do this? I don't want to manually copy the code but to generate files to export.
If you are copying from database(Schema) in SQL Server to another, then what you can do is use the Tasks>Generate Scripts
From here you press the Select Specific Database Objects and select the tables in the tables section that you want to carry to your new database.
Then go to the Set Scripting Options Menu, and press the advanced button at the top right of that menu. Another menu will pop up with options. Based on what you said the default (following) options should work.
Then Just Generate it in your desired format and make sure you're pointing them all to the correct database!
Using SQL Server Management Studio you can right-click on the database in the Object Explorer and choose Tasks/Generate Scriptsand follow the instructions in the wizard.

getting SQL from one database to create a similar database using only SQL

I want to dump one SQL Server database - get all SQL code necessary to create a similar database. I have full online rights to DatabaseA, I can feed it with SQL and get the results back in rows in a table.
I do not have the possibility to use Enterprise Manager, any applications, utilities or the like. I can only feed it with pure SQL.
What I am after is SQL code, like CREATE TABLE and so on. So that I just can paste this into a query and voila - tables, procedures, functions are created in DatabaseB.
I will not copy the data.
This partly does what I want, it gives me procedures and functions:
Select object_Name(object_ID),definition from sys.SQL_Modules
But not for tables.
You can use the command line or you can create a stored procedure to create a back up, then use that backup to create a new database. I have used the command line often. Here is a previous Stack question that has a command line example and a link to a stored procedure example.
You can generate scripts in SQL Server Management Studio for an entire database or specific database objects.
To do this, right click the database then select Tasks then select Generate Scripts.
It will then open a wizard which will give you the option to choose to script the full database or just specific database objects.

Copy table to a different database on a different SQL Server

I would like to copy a table from one database to another. I know you can easily do the following if the databases are on the same SQL Server.
SELECT * INTO NewTable FROM existingdb.dbo.existingtable;
Is there any easy way to do this if the databases are on two different SQL Servers, without having to loop through every record in the original table and insert it into the new table?
Also, this needs to be done in code, outside of SQL Server Management Studio.
Yes. add a linked server entry, and use select into using the four part db object naming convention.
Example:
SELECT * INTO targetTable
FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
If it’s only copying tables then linked servers will work fine or creating scripts but if secondary table already contains some data then I’d suggest using some third party comparison tool.
I’m using Apex Diff but there are also a lot of other tools out there such as those from Red Gate or Dev Art...
Third party tools are not necessary of course and you can do everything natively it’s just more convenient. Even if you’re on a tight budget you can use these in trial mode to get things done….
Here is a good thread on similar topic with a lot more examples on how to do this in pure sql.
SQL Server(2012) provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
Using the SQL Server Management Studio, right-click on the source database from the object explorer, then from Tasks choose Generate Scripts.
In the Choose objects window, choose Select Specific Database Objects to specify the tables that you will generate script for, then choose the tables by ticking beside each one of it. Click Next.
In the Set Scripting Options window, specify the path where you will save the generated script file, and click Advanced.
From the appeared Advanced Scripting Options window, specify Schema and Data as Types of Data to Script. You can decide from here if you want to script the indexes and keys in your tables. Click OK.
Getting back to the Advanced Scripting Options window, click Next.
Review the Summary window and click Next.
You can monitor the progress from the Save or Publish Scripts window. If there is no error click Finish and you will find the script file in the specified path.
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
Microsoft SQL Server Database Publishing Wizard will generate all the necessary insert statements, and optionally schema information as well if you need that:
http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A
Generate the scripts?
Generate a script to create the table then generate a script to insert the data.
check-out SP_ Genereate_Inserts for generating the data insert script.
Create the database, with Script Database as... CREATE To
Within SSMS on the source server, use the export wizard with the destination server database as the destination.
Source instance > YourDatabase > Tasks > Export data
Data Soure = SQL Server Native Client
Validate/enter Server & Database
Destination = SQL Server Native Client
Validate/enter Server & Database
Follow through wizard

Resources