Need help to perform an ETL task with SSIS - sql

I want to connect to a server using its IP address. Then, I want to execute a .sql file kept on that servers drive. I also want to store the results of this sql in a text file on the server itself. Should i execute it as an sql task or run it as an extraction from database or something else ? I am not sure how to go about it. I need tips on how to proceed.
The database used is SQL server 2008.

On SSIS, you can execute SQL and export it to text file(csv).
Please refer this article.
http://dwbi1.wordpress.com/2011/06/05/ssis-export-query-result-to-a-file/

Creating an SSIS Solution on SQL Server to flat file.
Open Microsoft Visual Studio
Click Create Project
Select Integration Services Project
Name the Project (x2)
Set the location, example C:\SSISPackages\
Click OK
Click the Data Flow tab
Click to create a new Data Flow Task
Click the Toolbox tab (bottom left)
Drag OLE DB Source to Data Flow section
Drag Flat File Destination (for example) to Data Flow section
Drag the green arrow to connect the OLE DB Source to the Flat File Destination
Double-click OLE DB Source
Click New next to OLE DB Connection Manager
Select YOURSERVER\YOURDB and click OK
Change Data Access Mode to SQL Command
Enter procedure and parameter(s) (for example) under SQL Command Text
Click Parse Query to confirm the SQL Command is valid, and click OK
Double-click the Flat File Destination
Click New next to Flat File Connection Manager
Choose Ragged Right (if standard fixed-width with CR/LF at end of each row) and click OK
Click Browse and select the destination and file name for the output and click OK twice
Click on Mappings and confirm the input and destination columns are correct, and click OK
To append the current date to the file name:
Click on Package Explorer tab (top right of center pane)
Click on Variables tab (bottom left)
Add a variable called path, Scope = Package, Data Type = String, Value = path to file (not including YYYYMMDD.txt portion of file name)
In the Properties for Flat File Connection Manager (bottom center, right-click), open Expressions, and set Property to ConnectionString and Expression to:
#[User::path]+
(DT_STR,4,1252) DatePart("yyyy",getdate()) +
Right("0" + (DT_STR,4,1252) DatePart("m",getdate()),2) +
Right("0" + (DT_STR,4,1252) DatePart("d",getdate()),2) + ".txt"
(Or however you need the date and file name formatted)
Click Evaluate Expression to make sure it looks OK.

Related

How can I export tables to SQL Script?

I need to export many tables to Script SQL.
I'm using Microsoft SQL Server 2014.
How can I do this?
1) Right click on the database
2) Go to Tasks -> Generate Scripts
3) A Generate and Publish script popup window will appear. Click Next
4) Select specific database objects. And select Tables
5) Go To Adanced, another popup window will be displayed. Scroll down to Types of data script and select Schema and Data
6) And then Save to file or clipboard and click Next
1) Simple Right Click on Database name in object explorer,
2) menu list will be open then from this list go to Task in Task sub menu Click on Generate Script
3) A New Dailog will be opened click on Next button
4) select the tables and any DDL name which you want then click next
5) Then choose path where you save script click next

Inserting .DBF Files from different folder structure with their respective FilePath using SSIS?

Am working on creating SSIS Package the does the following, so far my SSIS Package works well to insert records from '.DBF' file from one folder location ONLY (Am using Data Flow Task onto the Control Flow and OLE DB Source/Destination to read a .DBF file and get it inserted). However, am not able to figure out how to make it works with 20 sites, for example: I have 20 '.DBF' files stored in the following structure:
1. E:\DBF Files\Site1\Data\records.DBF
2. E:\DBF Files\Site2\Data\records.DBF
3. E:\DBF Files\Site3\Data\records.DBF
4. E:\DBF Files\Site4\Data\records.DBF
and so on till Site20 (here file name remain the same but the content will differ from site to another).
I have table for 'records.dbf' file in my SQL Server in the following structure:
Records Table:
Product_Name Price FilePath FileName
---------------------------------------------------------------
now here, how can process these files from different sites to be stored in my records sql table with it respective FilePath using SSIS??
P.S File Names will always remain the same and the path will change "Like Site 1"
Grab a For Each Loop container for the SSIS Toolbox and drag it on to the Control Flow designer.
Double click the For Each container to bring up the editor, click the Collection tab from the left-hand menu
Leave the Enumerator set at Foreach Item Enumerator and click the Columns button in the lower right.
Click the Add button and just go with the defaults which should be Column 0 and String. Click OK to return to the previous dialog. In the new Column 0 column add your folder paths one per row.
Next click the Variable Mappings tab from the left-hand menu, for the drop-down menu in the Variable column select New Variable. Again go with the defaults except for the name which in my example I have called dbf_path.
Next click OK to complete the For Each. Now the difficult bit for me because I don't what tasks you currently have that are working for one file. Whatever it is you have working, select it with the mouse and then drag and drop it into the For Each container.
For each task or tasks that need to use the file path do the following:
double click it to edit, in the left-hand menu select Expressions, then click Expressions and then click the ellipsis.
From the Property drop-down in the Property Expression Editor you need to find the property that would hold the file path for this task. The names are not always obvious and can be called different things in different tasks. In my example I am using a File System Task which I think is the Source Property.
Click the ellipsis in the Expression column and add an expression that consists of your path variable concatenated a string that contains the name of your file. One thing to note is backslashes have special meanings so if your string contains any they must be escaped with a second backslash.
Now when you run the package the For Each will loop through each path you have defined and store it in your variable which tasks will reference.

how to remove invalid object names on linkserver query

I created a linked server between 2 SQL Servers, and successfully made the linked server and I can query on the other server in linked server
But when I have a query, there appears a red line under the query which says "invalid object name", but I can query on it
Like this : linked server name: REMOTERT
SELECT done_buy, done_sell
FROM [REMOTERT].front.dbo.custbal
--> shows a red line and when I point mouse on it show invalid object name [REMOTERT].front.dbo.custbal. but if I press F5(Execute) can show the result
once you create a new SQL Server object, your newly created object does not get updated in the IntelliSence Local Cache and due to this, it shows red line underneath that object. So you just need to refresh SSMS IntelliSence Local Cache and once you refresh it, IntelliSence will automatically add newly created object in the cache and the red line will disappear. try this
Edit -> IntelliSense -> Refresh Local Cache
or simply do
Ctrl + Shift + R
It is because of your cached memory is not refresh,
simply close sqlserver and open again. It will not come again.

How do I set up a dynamic file path in SSIS for an Excel file?

The name of the file changes based on months. Every month you have a new file.
I:\Test\Data_201303.xlsx
How do I set up a connection manager that will work with variable file paths?
Look for the "expression" property on the connection manager. That's where you set it to USER::VariableName.
More detail: if you "edit" the connection manager it will show you the edit window. That's not the one you want. Look at the "properties" window, which is where you will find the "Expressions" entry, in front of an empty box.
Click on the empty box, and it will show you a button with three dots on it. Click on that button. This pulls up the "Property Expression Editor". There is a dropdown with properties like "ConnectionString" on the left. On the right, there is ANOTHER button with three dots. Click on that button to pull up the "Expression Builder". If you have declared any variables, you will find them listed in the upper left hand corner, e.g., as User::VariableName, and you can then drag them into the Expression box, where they will appear as #[User::VariableName].
Not obvious, but doable.
You need to set the expression for the ServerName or ExcelFilePath property to modify ConnectionString of Excel connection manager dynamically using an SSIS package variable.
Here are some SO answers that deal with looping multiple Excel files :
How to loop through Excel files and load them into a database using SSIS package?
How to import Excel files with different names and same schema into database?

stored proc from excel with parameters

If I create a stored proc in sql that has a parameter, can I run that from Excel?
Without using VBA you can use this method. This assumes your stored procedure returns a result set. It also does not make the parameter pulled from a workbook. I think you'd have to use VBA to do that.
Under the 'Connections' section click 'Connections'
Click Add
Click 'Browse for More...'
Click 'New Source...'
Select 'Microsoft SQL Server'
Click Next
Type in the login credentials and click ok.
Select your database
Uncheck 'Connect to a specific table'
Click Finish.
When asked to select a table just click ok.
This should bring you back to the Workbook Connections screen with your new connection added.
Select it and click 'Properties'
Go to the 'Definition' tab.
Set the Command Type to SQL
set the Command text to the SQL you want to run (i.e. "exec spStoredProc 'value'")
Set a new name for your connection
Click ok.
if told that you are severing a connection just agree that, that is okay.
Click 'Close' on your 'Workbook Connections'
Click 'Existing Connections' under the 'Get External Data' section
Select your connection you just made under the 'Connections in this Workbook' section.
Click Open
Set your Import Data options if you want or just click ok
You should now see the result set from the SQL Query in your excel worksheet.
This data can be refreshed from the data that is in the database by going to the data tab and clicking 'Refresh All'
Yes
A standard ADO call from VBA. Or use the Tools..Data thing.
Sure, you can use VBA.
Or you can do it like this: http://blogs.msdn.com/b/excel/archive/2010/06/08/running-a-sql-stored-procedure-from-excel-no-vba.aspx
Here's another nice article on it.
http://www.itjungle.com/mgo/mgo102203-story01.html
I have been working to find a solution to this for a while now and got the best results from following the steps in this article.
http://codebyjoshua.blogspot.com/2012/01/get-data-from-sql-server-stored.html