How to debug stored procedure in Visual Studio without publishing - sql-server

Previously I developed everything DB related in SSMS but since I didn't have it setup with any source control I decided to move it into Visual Studio (where I develop everything else) as a Database Project.
This works quite well for most things but I would like to test and debug my stored procedures somehow without publishing the project. Is this possible and how is it done? The important thing is that data from the production DB should be accesible for the stored procedures.

From Visual Studio, open SQL Server Object Explorer (View-SQL Server Object Explorer). Find your database, right click and select "New Query".
You can run/test your sp using "exec spProcedureName".
You can use "Ctrl-Shift-E" as a shortcut to execute query.
Also, you can find your procedure under "Programmability-Stored procedures" and right click on it. If procedure has parameters, you'll get a pop-up window in order to enter params.

Related

T-SQL C# CLR Execute permissions being lost on assemblies when are redeployed

I have inherited a VS2015 .NET C# project that deploys C# assemblies as CLR stored procedures to SQL Server 2016. It has worked seamlessly for many years.
However recently I've noticed that when I redeploy (a.k.a "Publish Database"), one of the assemblies seems to lose EXECUTE permissions for one of the users.
Is there a way I can have the project reapply the EXECUTE permissions after it has redeployed? The script would have to know which SQL Server instance and database I'm deploying the updated CLR to. Thanks
User are never given execute permissions to an assembly. I suspect you are meaning execute permissions to one (or more)of the SQLCLR stored procedures that reference code within this assembly. You can fairly easily add a post-release T-SQL script to your Visual Studio project that will get included in the publish scripts (by SSDT via VS). In this T-SQL script you just add the GRANT statement. And, if it only makes sense in one particular database / instance, then you can wrap that GRANT statement in an IF block that checks for the current instance and/or database names.
Steps for adding a post-release script in VS 2015:
Select project in Solution Explorer
Go to "Project" menu
Select "Add New Item..."
Select "SQL Server" -> "User Scripts"
Select "Post-Deployment Script"
Type a name for the script down in the "Name:" field
Click the "Add" button
Edit the new SQL script to add the GRANT statement(s)
The contents of that script will be added to the end of any deployment / publish script generated by VS / SSDT, and hence will execute in every DB in which you execute those scripts.

How to debug stored procedure in VS 2015?

I know this was asked many times, I've read them.
I've checked the [Enable SQL Server debugging] in all my projects in the solution.
My projects:
ASP.NET web app
DAL class library. (The DAL uses a legacy .dbml to generate SP calling wrapper code.) No OR mapper neither direct ADO.NET used in the project.
SQL Server 11.0.3153
I would like to debug my called SPs when debugging the C# code in VS. Ideally it should step in to the SP, but if this is not supported, then break in the SP on a set breakpoint.
Unfortunatelly it does neither. If I set a breakpoint in the SP, it is not a filled red circle, instead an unfilled, which is not a good sign. (I am setting this breakpoint in Server Explorer, by opening a data connection, and opening the SP in the VS editor.
What am I missing?
General instructions, based on my experience and research.
Run Visual Studio (community version, in my case) as Administrator (for me, debugging a stored proc from VS only works when VS is run as an admin)
Go to the Solution Explorer, right click on your project and go to properties.
Click on the Web tab and make sure that SQL Server is checked. Save and close.
Click on the View menu, then on SQL Server Object Explorer.
In the SQL Server Object Explorer, expand SQL Server and if you don't see your SQL Server, right click on SQL Server and add it.
Right click on the SQL Server that you just added and make sure that both Application Debugging and Allow SQL/CLR Debugging are checked.
Expand your SQL Server instance that you added and find the stored procedure of interest.
Right click that SP and click View Code.
Put a break point where you wish.
Run and enjoy.
You may have to do some of these things next time you wish to debug a stored proc from VS after you close VS and open it up later.
I had the same problem...
In the "Solution Configurations" dropdown, the selected configuration was "Debug (Active)". I changed that to "Debug."
After the change was made, the debugger worked normally and the "Debug (Active)" option disappeared from the list.

Unit testing SQL Server stored procedures

I am trying to follow this:
Unit Testing SQL Server sproc
I am using Visual Studio 2010 but cannot find the option to add a unit test to the imported stored procedures - see this step:
Right-click one of the stored procedures, and click Create Unit Tests to display the Create Unit Tests dialog box.
Am I missing something? Thanks.
The link you provide is about SQL Server Data Tools, a separate download. You need to install SSDT and then create a Database Project
After installed, the context menu is only available in Visual Studio's SQL Server Object Explorer (which can be accessed as a window under the View menu). Expand your project's Programmability folder and then finally right click on Stored Procedures.
After struggling with the "Create Unit Tests" being disabled it came down to this important point - so I deleted everything that would not build.
"...the project needs to build without errors or else the option will be grayed out. – Kevin Cunnane Dec 25 '13 at 14:05"

Visual studio 2010 sql project - how to run?

I have a SQL project in my .Net project in Visual Studio 2010. I added it to my solution because I thought it will help me to create all the database objects in more environment without difficulties.
My scope is to create/define the database objects (tables and stored procedures) in more computers, easily.For this purpose I created an Sql project in visual studio and I added for each table and stored procedure the creation script in this project. Now I have 30 scripts and I'd like to run it on a new sql instance.
If I open each sql file I have, in visual studio, an toolbat that allows me to tun the opened file on a sql instance (I have an connect button) and this generates the proper object (table or SP).
The question is: how do I do to run all the files in this project in one click. How to create all the objects in one click? Now I have about 30 scripts to run, and I need a proper way.
The way I tried to do it was to set as default project the sql project and to press run (F5), but then I got some strange compilation errors in sql files, errors witch didn't was there when I run (execute) each script individually.
Here is the right click menu for this project (No Run, Publish or something else!):
I tried to use from that menu the Deploy command. The bad news (for me) is that I got this on that command:
I don't have any error in my sql scripts, each one runs correctly.
Thank you.
Use need to deploy your project onto a database thus use the Deploy menu item.
The way database projects work, is that they compare the schema in the project to the schema at the destination.
What this means is you shouldn't have an use statements, or alter statements, ect...
What does one of those procs look like that is throwing the error?

Can't drag stored procedure onto dbml designer

I'm trying to do a simple call to a database stored procedure from a C# application.
I'm following a guide like, e.g., this one or this one.
Both of these have the same basic steps.
Add a LINQ to SQL .dbml item to my project
Connect to a database in Server Explorer
Drag a stored procedure from the Server Explorer onto my .dbml designer window
But step 3 doesn't work. I have my stored procedure and my designer window, but I can't drag the sproc. I don't get a plus sign, or a "not allowed" sign. It's just a non-dragable object.
I can't find another way to add my sproc to the .dbml file. And I can't find anyone else who has had this problem. Every source simply says to drag it over.
What could I be doing wrong here?
Screenshot:
Turns out the solution was that I had the wrong version of a .dll for Visual Studio.
I noticed I was getting exceptions when performing actions in the Server Explorer. Googling the exceptions led to this question which points to this dll
C:\Program Files (x86)\Common Files\microsoft shared\Visual Database Tools\dsref80.dll
as the issue.
I replaced it with the corresponding copy from one of my teammates and it now works fine.
I had also installed VS 11 Beta and the designer was working fine until I uninstalled it. I had to delete the DLL manually at the above location and repair the installation by finding Visual Studio 2010 in Programs and Features and selecting Uninstall/Change and then Repair. The installer will replace the DLL file that was deleted with the correct version.
For anyone that can drag the sproc, but sees no result (the sproc simply does not show up in the functions pane): make sure that the sproc only returns datatypes that L2S understands. For example you can not return a geography field.
Editing .dbml files with a designer surface requires the LINQ to SQL tools which are not installed by default as part of any of the workloads of Visual Studio 2017. It can be installed by selecting the "LINQ to SQL tools" item under the "Code Tools" category in the "Individual Components" tab of the Visual Studio installer.

Resources