{"id":1109,"date":"2010-06-07T16:04:00","date_gmt":"2010-06-07T08:04:00","guid":{"rendered":"http:\/\/vm-officeblogs.cloudapp.net\/2010\/06\/07\/running-a-sql-stored-procedure-from-excel-no-vba\/"},"modified":"2022-07-26T04:21:44","modified_gmt":"2022-07-26T11:21:44","slug":"running-a-sql-stored-procedure-from-excel-no-vba","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/microsoft-365\/blog\/2010\/06\/07\/running-a-sql-stored-procedure-from-excel-no-vba\/","title":{"rendered":"Running a SQL Stored Procedure from Excel (No VBA)"},"content":{"rendered":"
Today\u2019s author is Mike Alexander, an Excel MVP who shows us how to run a Stored Procedure to get data from a SQL server.\u00a0<\/span><\/p>\n We all know we can use MS Query to get data from a SQL server. Typically though, we pull from a Table or a View. Well in some organizations, the IT department wants all interaction with the server to be done through Stored Procedure. This adds a level of risk management and makes the DBAs feel better.<\/span><\/p>\n So today, I’ll show you how to easily make Excel run a Stored Procedure to get data. <\/span><\/p>\n Step 1:<\/span><\/b> Data tab \u2013 > From Other Sources -> From SQL Server\u00a0<\/span><\/p>\n Step 2:<\/span><\/b> Enter Credentials. Your server name can be an IP address\u00a0<\/span><\/p>\n Step 3:<\/span><\/b> Choose any old table or view. Pick a small one because we’ll discard it later anyway. <\/span><\/p>\n Step 4:<\/span><\/b> Excel will pop up the Import Data dialog box. Click Properties here (NOT THE OK BUTTON). <\/span><\/p>\n Step 5:<\/span><\/b> Click on the Definition tab. There, change Command Type to SQL, and then enter your Stored Procedure name in the Command Text input.\u00a0<\/span><\/p>\n Step 6:<\/span><\/b> Excel complains about something\u2026.blah\u2026blah\u2026blah. Click Yes \u2013 (as in yes I know what I’m doing).\u00a0<\/span><\/p>\n Step 7:<\/span><\/b> Excel will activate the Import Data dialog box again. This time click OK to fire the Stored Procedure and return the results. <\/span><\/p>\n Step 8: <\/span><\/b>Marvel at your results <\/span><\/p>\n Notes:<\/span><\/b><\/p>\n \u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>Excel will fire the Stored Procedure each time you “Refresh” <\/span><\/p>\n \u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>If you have to pass a parameter, you can enter it in the command text like this:<\/span><\/p>\n \u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>If you have to pass dynamic parameters you’ll have to turn to VBA. I’ll do a post on this later this week. <\/span><\/p>\n \u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>I assume you can do this with ORACLE databases too. <\/span><\/p>\n \u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><\/span>I’ve yet to test whether this will fire a Stored Procedure that doesn’t return data. In other words, Stored Procedures that perform Insert, Update or Delete actions. I assume that if you can, there is the possibility of updating SQL from Excel through a simple connection. Pretty cool.\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":" In some organizations, the IT department wants all interaction with the server to be done through Stored Procedure. This adds a level of risk management and makes the DBAs feel better.<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_classifai_error":"","footnotes":""},"content-type":[4035],"product":[4047,4036],"audience":[4081,4101,4102],"tags":[],"coauthors":[4437],"class_list":["post-1109","post","type-post","status-publish","format-standard","hentry","content-type-tips-and-guides","product-excel","product-microsoft-365","audience-enterprise","audience-personal-and-family","audience-small-business"],"yoast_head":"\n