stored proc from excel with parameters - sql

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

Related

How to set shortcut key to open query in ms access. can some body explain clearly

How to set shortcut key to open query in ms access. can some body explain clearly. how to set shortcut key to already designed qureiess
You can attach a command button to open the query, then in the Command button caption add & and the short cut key you want to use to open the query.
e.g if a command button's caption is Run Query , you can change it to
&NRun Query ,tis will ensure that Alt and N keys will open the query.
Check the images i attached.

Ms Access and SQL: how to identify where error occurs

I have a form in MS Access where a lot of queries are running to calculate all kinds of numbers using user input and using other queries ouput. All of a sudden I get the error below:
Error text is:
The record source ' ' specified on this form or report does not exist.
The name of the recordsource may misspelled, the recordsource was deleted or renamed, or the recordsource exists in a different database.
In the Form or Report's design view or layout view, display the property sheet by clicking the properties button, and then set the recordsource property to an existing table or query.
I don't remember exactly what I added/removed to have caused this and I can't find where the error occurs. How can I, I guess, debug my Access Form while it's running queries?
I don't see your image as it is blocked at work.
The only one way to debug the application when errors occurs is to break the execution using the keyboard combination :
CTRL + Pause/Break
Do it when the error message appears
This obviously doesnt work if the application is compiled in a mde/accde
When the CTRL-Break doesnt work, you have to add a break point in your code.
Bring the VBA window using
ALT + F11
Open the code of your form, go in the procedure that triggers the queries, select a line of code at the beginning and add a breakpoint using F9
Then run the form, it will stop the execution on the breakpoint and show you the VBA window, continue to execute the code step-by-step using F8 until the error occurs
When ever you change the name of a table in ms access for that has drop down list, go to the design view --> property Sheet--> Row Source and change the names to the corresponding names for the drop down selection for combo selection

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

Need help to perform an ETL task with SSIS

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.

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?