How to delete a row from table using SPSS Modeler - jython

I'm using SPSS Modeler 17.0 and I want to delete a row from a table. In export database node there is only the option Delete all existing records. It deletes all the records and does enable to delete just one using a WHERE sql like condition.
Is there any other way to do it? using jython scripts for example ?

You can do this from a database source node, which is a little counter-intuitive.
In the node, just select the SQL Query mode in the Data tab and enter the SQL statement(s) required to delete records that you wish to remove.
You can then either just hit the Preview button to execute the statement(s) or you may attach any executable node to the source node and execute that.

Related

Copy 10k rows from table to table on other server

I cannot use linked server.
Both databases on both servers have the same structure but different data.
I have 10k rows to transfer from the DB on one server to the same DB on the other. I cannot restore the DB on the other server as it will take a huge amount of space that I don't have on the other server.
So, I have 2 options that I don't know how to execute:
Backup and restoring only one table - the table is linked to many other tables but these other tables exist on the other server too. Can I somehow delete or remove the other tables or make a backup only over one table?
I need to transfer 10k rows. How is it possible to create 10k insert queries based on selected 10k rows?
Can I somehow delete or remove the other tables or make a backup only over one table?
No you can not do this, unfortunately
How is it possible to create 10k insert queries based on selected 10k rows?
Right-click on Database -> Tasks -> Generate scripts -> (Introduction) Next
Chose Select specific database objects -> Tables, chose table you need -> Next
Advanced -> Search for Types of data script change from Schema only (by default) to Data only -> OK
Chose where to save -> Next -> Next. Wait the operation to end.
This will generate the file with 10k inserts.
Another way is to use Import/Export wizard (the most simple way for one-time-import/export) if you have link between databases.
There are many ways to choose from, here is one way using BCP. That's a tool that ships with SQL Server to Import and Export Bulk Data.
The outlines of the process:
Export the data from the source server to a file using BCP - BCP OUT for a whole table, or BCP QUERYOUT with a query to select the 10k rows you want exported
Copy the file to the destination server
Import the data using BCP on the destination database - BCP IN.
My suggestion would be to export these rows to excel( you can do this by copy pasting your query output) and transfer this to other server and import it there.
this is the official method :-
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/import-data-from-excel-to-sql
and this is the the unofficial method :
http://therealdanvega.com/blog/2010/08/04/create-sql-insert-statements-from-a-spreadsheet.
Here I have assumed that you only need to transfer the transactional data and your reference data is same on both server. So you will need to execute only one query for exporting your data
I would definietely go down the SSIS route once you use SSIS to do a task like this you will not use anything else very simple to script up. You can use any version and it will be a simple job and very quick.
Open new SSIS project in available visual studio version/s there are many different but even a 2008 version will do this simple task you may have to install integration services or something similar used to be called bids (business information development studio in 2008) (anything up to 2015 although support is nearly there in 2017)
add a data flow task
double click the data flow task
Bottom of screen add two connection managers (1 to source and 1 to destination database)
add oledb source pointing to source database table
add oledb destination pointing to destination database table
drag line between the source and destination (should auto map all columns if the same name)
hit Start and the data will flow very quickly
you have create DbInstaller. using dbInstaller you have share whole database. Dbinstaller work both ado.Net and Entity Frame Work but I have using Entity Frame Work.
you can do it by sql server query
first select first database like
Use database1 --- this will be your first database
after select first database we will put our table row in temp table by this query
select * into #Temp from select * from table1
now we select second database and insert temp table data into second database table by this code
use secondDatabaseName
INSERT INTO tableNameintoinsert (col1, col2, )
SELECT col1, col2 FROM #temp;

Backup a single table with data in SQL Server [duplicate]

I want to get a backup of a single table with its data from a database in SQL Server using a script.
How can I do that?
SELECT * INTO mytable_backup FROM mytable
This makes a copy of table mytable, and every row in it, called mytable_backup. It will not copy any indices, constraints, etc., just the structure and data.
Note that this will not work if you have an existing table named mytable_backup, so if you want to use this code regularly (for example, to backup daily or monthly), you'll need to run drop mytable_backup first.
You can use the "Generate script for database objects" feature on SSMS.
Right click on the target database
Select Tasks > Generate Scripts
Choose desired table or specific object
Hit the Advanced button
Under General, choose value on the Types of data to script. You can select Data only, Schema only, and Schema and data. Schema and data includes both table creation and actual data on the generated script.
Click Next until wizard is done
There are many ways you can take back of table.
BCP (BULK COPY PROGRAM)
Generate Table Script with data
Make a copy of table using SELECT INTO, example here
SAVE Table Data Directly in a Flat file
Export Data using SSIS to any destination
You can create table script along with its data using following steps:
Right click on the database.
Select Tasks > Generate scripts ...
Click next.
Click next.
In Table/View Options, set Script Data to True; then click next.
Select the Tables checkbox and click next.
Select your table name and click next.
Click next until the wizard is done.
For more information, see Eric Johnson's blog.
Put the table in its own filegroup. You can then use regular SQL Server built in backup to backup the filegroup in which in effect backs up the table.
To backup a filegroup see:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server
To create a table on a non-default filegroup (its easy) see:
Create a table on a filegroup other than the default
Another approach you can take if you need to back up a single table out of multiple tables in a database is:
Generate script of specific table(s) from a database (Right-click database, click Task > Generate Scripts...
Run the script in the query editor. You must change/add the first line (USE DatabaseName) in the script to a new database, to avoid getting the "Database already exists" error.
Right-click on the newly created database, and click on Task > Back Up...
The backup will contain the selected table(s) from the original database.
To get a copy in a file on the local file-system, this rickety utility from the Windows start button menu worked:
"C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\DTSWizard.exe"

Newly inserted or updated row count in pentaho data integration

I am new to Pentaho Data Integration; I need to integrate one database to another location as ETL Job. I want to count the number of insert/updat during the ETL job, and insert that count to another table . Can anyone help me on this?
I don't think that there's a built-in functionality for returning the number of affected rows of an Insert/Update step in PDI to date.
Nevertheless, most database vendors are able to provide you with the ability to get the number of affected rows from a given operation.
In PostgreSQL, for instance, it would look like this:
/* Count affected rows from INSERT */
WITH inserted_rows AS (
INSERT INTO ...
VALUES
...
RETURNING 1
)
SELECT count(*) FROM inserted_rows;
/* Count affected rows from UPDATE */
WITH updated_rows AS (
UPDATE ...
SET ...
WHERE ...
RETURNING 1
)
SELECT count(*) FROM updated_rows;
However, you're aiming to do that from within a PDI job, so I suggest that you try to get to a point where you control the SQL script.
Suggestion: Save the source data in a file on the target DB server, then use it, perhaps with a bulk loading functionality, to insert/update, then save the number of affected rows into a PDI variable. Note that you may need to use the SQL script step in the Job's scope.
EDIT: the implementation is a matter of chosen design, so the suggested solution is one of many. On a very high level, you could do something like the following.
Transformation I - extract data from source
Get the data from the source, be it a database or anything else
Prepare it for output in a way that it fits the target DB's structure
Save a CSV file using the text file output step on the file system
Parent Job
If the PDI server is the same as the target DB server:
Use the Execute SQL Script step to:
Read data from the file and perform the INSERT/UPDATE
Write the number of affected rows into a table (ideally, this table could also contain the time-stamp of the operation so you could keep track of things)
If the PDI server is NOT the same as the target DB server:
Upload the source data file to the server, e.g. with the FTP/SFTP file upload steps
Use the Execute SQL Script step to:
Read data from the file and perform the INSERT/UPDATE
Write the number of affected rows into a table
EDIT 2: another suggested solution
As suggested by #user3123116, you can use the Compare Fields step (if not part of your environment, check the marketplace for it).
The only shortcoming I see is that you have to query the target database before inserting/updating, which is, of course, less performant.
Eventually it could look like so (note that this is just the comparison and counting part):
Also note that you can split the input of the source data stream (COPY, not DISTRIBUTE), and do your insert/update, but this stream must wait for the stream of the field comparison to end the query on the target database, otherwise you might end up with the wrong statistics.
The "Compare Fields" step will take 2 streams as input for comparison, and its output is 4 distinct streams for "Identical", Changed", "Added", and "Removed" records. You can count those 4, and then process the "Changed", "Added", and "Removed" records with an Insert/Update.
You can do it from the Logging option inside the Transformation settings. Please follow the below steps :
Click on Edit menu --> Settings
Switch to Logging Tab
Select Step from the left menu
Provide the Log Connection & Log table name(Say StepLog)
Select the required fields for logging(LINES_OUTPUT - for inserted count & LINES_UPDATED - for updated count)
Click on SQL button and create the table by clicking on the Execute button
Now all the steps will be logged into the Log table(StepLog), you can use it for further actions.
Enjoy

Transfer data from one database to another database

How to fetch the data from one database and insert in to another database table? I can't to do this. Please help me in transferring data from one to another.
There are several ways to do this, below are two options:
Option 1
- Right click on the database you want to copy
Choose 'Tasks' > 'Generate scripts'
'Select specific database objects'
Check 'Tables'
Mark 'Save to new query window'
Click 'Advanced'
Set 'Types of data to script' to 'Schema and data'
Next, Next
You can now run the generated query on the new database.
Option 2
Right click on the database you want to copy
'Tasks' > 'Export Data'
Next, Next
Choose the database to copy the tables to
Mark 'Copy data from one or more tables or views'
Choose the tables you want to copy
Finish
Example for insert into values in One database table into another database table running on the same SQL Server
insert into dbo.onedatabase.FolderStatus
(
[FolderStatusId],
[code],
[title],
[last_modified]
)
select [FolderStatusId], [code], [title], [last_modified]
from dbo.Twodatabase.f_file_stat
For those on Azure, follow these modified instructions from Virus:
Open SSMS.
Right-click the Database you wish to copy data from.
Select Generate Scripts >> Select Specific Database Objects >> Choose the tables/object you wish to transfer.
strong text
In the "Save to file" pane, click Advanced
Set "Types of data to script" to Schema and data
Set "Script DROP and CREATE" to Script DROP and CREATE
Under "Table/View Options" set relevant items to TRUE. Though I recommend setting all to TRUE just in case. You can always modify the script after it generates.
Set filepath >> Next >> Next
Open newly created SQL file. Remove "Use" from top of file.
Open new query window on destination database, paste script contents (without using) and execute.
if both databases are on same server and you want to transfer entire table (make copy of it) then use simple select into statement ...
select * into anotherDatabase..copyOfTable from oneDatabase..tableName
You can then write cursor top of sysobjects and copy entire set of tables that way.
If you want more complex data extraction & transformation, then use SSIS and build appropriate ETL in it.
You can use visual studio 2015. Go to Tools => SQL server => New Data comparison
Select source and target Database.
You can backup and restore the database using Management Studio.
Again from Management Studio you can use "copy database".
you can even do it manually if there is a reason to do so. I mean manually create the target db and manually copying data by sql statements...
can you clarify why you ask this? Is it that you dont have expierience in doing it or something else?
There are multiple options and depend on your needs.
See the following links:
Copying Data Between Servers
Copy tables from one database to another in SQL Server.
These solutions are working in case when target database is blank.
In case when both databases already have some data you need something more complicated http://byalexblog.net/merge-sql-databases
This can be achieved by a T-SQL statement, if you are aware that FROM clause can specify database for table name.
insert into database1..MyTable
select from database2..MyTable
Here is how Microsoft explains the syntax:
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-ver15
If the table or view exists in another database on the same instance of SQL Server, use a fully qualified name in the form database.schema.object_name.
schema_name can be omitted, like above, which means the default schema of the current user. By default, it's dbo.
Add any filtering to columns/rows if you want to. Be sure to create any new table before moving data.
Doing this programmatically between two different databases could involve a scheduled job using a linked server. But linked servers require DBA-level knowledge to set up. If you can't use a linked server, just write a program that 1. Reads a row from the source table and 2. Inserts it into the target table. The programmer just needs to use a connection string that has INSERT privileges into the target database table. I have solved this problems using both approaches.

Deleting or not deleting rows in ssis package destination table

i used the import/export wizard in sql server 2005 management studio to export rows from an excel sheet to an sql table and checked the Do not delete rows in destination table option. I saved the export operation as a ssis package, and yes new rows are being appended to existing ones, but now i have a requirement to delete all rows in the destination sql table.
When i go into BIDS to edit my package, i cant find the option to change this behavior any where. does any one know how to change this setting the ssis package designer??
When you enable the delete destination rows option, a "Execute SQL Task" is added to the package's Control Flow. This task is used to execute a TRUNCATE TABLE statement on the destination. In your package, the control flow probably contains a single Data Flow Task. Just add an Execute SQL Task and connect it to the data flow task. Assign the destination connection manager to the Execute SQL Task and set the SQL statement to "TRUNCATE TABLE [yourdestinationtable]"