I want to see the create database sql and I use show create database test command. But the result is
I want to see the whole information about Create Database column, what should I do?
use
show create database test\G;
or
set max_binary_display_width 200;
Related
I imported data from Power BI into SQL-Server. You can see how is look like imported data.
Additionally I created own database with commands below:
CREATE DATABASE MY_DW
GO
USE MY_DW
GO
Now I want to copy all this table into my base named as MY_DW. So can anybody help me how to solve this problem and copy all tables into my base ?
Please check https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/.
This link suggests various methods to copy the data tables from one database to another.
Thanks,
Rajan
Following approach could resolve your issue:
Imported Database
Generate Scripts
Introduction
Next button
Select the database objects (Tables in your case) to script
Next button
Specify how scripts should be saved
Advanced -> Types of data to script -> Schema and data
Next button
Review your selections
Next button
Script generation would take place and saved which you should run under the database,
MY_DW, you created
Another approach:
Assuming that the databases are in the same server.
The below query will create the table into your database(without constraints).
SELECT * INTO MY_DW.Table_Name
FROM ImportedDB.Table_Name
And the below query will insert the data into your database table.
INSERT INTO MY_DW.Table_Name
SELECT * FROM ImportedDB.Table_Name
Final approach:
Assuming that the databases are in the linked server.
Incase of linked server, four part database object naming convention will be applied like below.
The below query will create the table into your database(without constraints).
SELECT * INTO [DestinationServer].[MY_DW].[dbo].[Table_Name]
FROM [SourceServer].[ImportedDB].[dbo].[Table_Name]
And the below query will insert the data into your database table.
INSERT INTO [DestinationServer].[MY_DW].[dbo].[Table_Name]
SELECT * FROM [SourceServer].[ImportedDB].[dbo].[Table_Name]
I have created a Pivot table using columns passed in from a variable. This variable is set based on a flag in another table, so the name and number of columns can change month-to-month. I need to be able to export this pivot to a delimited text file. The pivot table is stored in a temp table ##PivotTable
I have tried to do this via SSMS, but the only way I know how to do this is to define a file connection manager, which needs set columns. I also tried via sqlcmd, but that one exports extra information and spaces.
I am open to pretty much any process that would allow me to run this via a SQL Agent job on a monthly basis.
Any assistance would be much appreciated.
You can use SQL server import and export wizard
Right click on the database-->Tasks-->ExportData..
map your ##PivotTable.
Click next and set your source database (Select OLEDB provider for SQL Server)
Set a file name for your file (e.g. d:\monthlyData\202006\MyFile.csv
Select option copy data from one to more tables or views -- do not use this option due the fact that your columns getting changed.
Select option write a quarry
You can run this immediately or save as SSIS package and reuse it using SQL job
I think you'll have to use dynamic SQL like found here.
I want to write a query to select a DB and then create a table within that DB.
I have the following query to just create a table, Is there a way to highlight the DB within the command itself?
.create table ABC (
A:string ,
B:string
)
Is there a way to highlight the DB within the command itself?
No - as the command runs in the context of a specific database, you must first make sure you're in the context of the database in which you wish to create the table.
In the web interface, that means 'selecting' the database in the connections panel.
In the API, that means explicitly specifying the database name in the connection string builder (or as part of the REST request parameters), or when calling the method to execute the control command.
I am looking for some idea if we can generate a script for just one view and run that on the another database to create that view with its datas intact. Please help, thank you
If your destination server is not linked with the source, getting this data out will take a few more steps. I am assuming that you only want to transport the data from the view, but the steps below could be applied to the source table(s), making this view instantiation part unnecessary.
First, since a view does not store data, it only references data, you will need to instantiate the view into a table.
Select *
INTO tblNewTable --this creates a new table from the data selected from the view
FROM dbTest.dbo.Tester;
Next, open SSMS. Right click the database, select tasks, then generate scripts
Then select the newly created table, and next
You will need to select advanced and change the 'types of data to script' to schema and data. It will be schema only by default. Select Next and Finish.
SSMS will export a file, or load a new query window with the code to create a new table, but will also have the insert statements to load the new table exactly as it was on the source server
Use following as an example
use dbNew;
go
create view dbo.ViewTest as
select * from dbTest.dbo.Tester;
Following code will create table using another table. The new Table will contain all the data of the previous table.
Select * into DBName1.SchemaName.NewTableName from DBName2.SchemaName.PreviousTableName
You can use this query to create new table in any database and schema.
I have one database with an image table that contains just over 37,000 records. Each record contains an image in the form of binary data. I need to get all of those 37,000 records into another database containing the same table and schema that has about 12,500 records. I need to insert these images into the database with an IF NOT EXISTS approach to make sure that there are no duplicates when I am done.
I tried exporting the data into excel and format it into a script. (I have doe this before with other tables.) The thing is, excel does not support binary data.
I also tried the "generate scripts" wizard in SSMS which did not work because the .sql file was well over 18GB and my PC could not handle it.
Is there some other SQL tool to be able to do this? I have Googled for hours but to no avail. Thanks for your help!
I have used SQL Workbench/J for this.
You can either use WbExport and WbImport through text files (the binary data will be written as separate files and the text file contains the filename).
Or you can use WbCopy to copy the data directly without intermediate files.
To achieve your "if not exists" approache you could use the update/insert mode, although that would change existing row.
I don't think there is a "insert only if it does not exist mode", but you should be able to achieve this by defining a unique index and ignore errors (although that wouldn't be really fast, but should be OK for that small number of rows).
If the "exists" check is more complicated, you could copy the data into a staging table in the target database, and then use SQL to merge that into the real table.
Why don't you try the 'Export data' feature? This should work.
Right click on the source database, select 'Tasks' and then 'Export data'. Then follow the instructions. You can also save the settings and execute the task on a regular basis.
Also, the bcp.exe utility could work to read data from one database and insert into another.
However, I would recommend using the first method.
Update: In order to avoid duplicates you have to be able to compare images. Unfortunately, you cannot compare images directly. But you could cast them to varbinary(max) for comparison.
So here's my advice:
1. Copy the table to the new database under the name tmp_images
2. use the merge command to insert new images only.
INSERT INTO DB1.dbo.table_name
SELECT * FROM DB2.dbo.table_name
WHERE column_name NOT IN
(
SELECT column_name FROM DB1.dbo.table_name
)