How to use SQL template parameters in Visual Studio (Data Tools)? - sql-server

I opened a SQL script in Visual Studio containing template parameters (as they are used by SSMS usually), but Visual Studio seems to ignore them.
Some background:
In SQL Server Management Studio (SSMS), I can prepare a T-SQL script as follows and then press Ctrl+Shift+M to open a dialog, where I can enter values for the variables (syntax is: <Name, Datatype, default> as described here, and here is a more detailed description how template parameters can be used):
Example:
DECLARE #UserLastName NVARCHAR(max)='<Last name,nvarchar(max),>';
Here, the <...> bracket expressions will be replaced by the values you entered in the dialog. If you type Doe and click OK in the dialog, you will get
DECLARE #UserLastName NVARCHAR(max)='Doe';
inserted into the script afterwards. This is quite useful if you create scripts for repetitive manual tasks, because you can type the values into the dialog and get a running script for that case.
Instead of the keyboard shortcut, you can use the menu option
The issue:
I tried to do the same in Visual Studio 2017, where I have installed Microsoft SQL Server Data Tools. But the keyboard shortcut does not work there, and there seems to be no such menu option.
Do you know how to invoke this dialog in Visual Studio?

Related

Visual Studio schema compare can't find generated script

I am performing SQL Server schema comparison using Visual Studio 2017. I am pressing on "Generate script" button of Schema Compare utility and see the message
"Update scripts generated successfully"
However, I am unable to see the generated script. Any idea where this went or if it failed?
According to this page "The generated script appears in a new Transact-SQL Editor window".
When this didn't work for me, i closed all open windows other than the schema compare window and re-checkmarked all differences in the list (i had un-checked some). After doing this and running "Generate Script" again, Visual Studio did actually open a new window with the script (The filename was "C:\Users\me\AppData\Local\Temp\MyDbName_Update1.publish.sql").
I've noticed that if SSMS is running , SSDT won't generate Schema Compare script. As soon as I close SSMS and try to generate script again SSDT creates one successfully.
Instead of direct compare from connection string, generate schema file and then compare that file
do this steps:
in visual Studio Go To Sql Server Object Explorer
Right Click on database name
Select Extract Data Tier Application
select location for file save
select Schema Only
deselct all checkbox option or check as your compare requirement
do the same for both source database and target database
then
when compare schema select option Data-Tier appication file option

T-SQL IntelliSense does not work on some solutions

I have a solution in Visual Studio 2013, where among the others I have also a Database project. This project contains some .sql written in T-SQL.
IntelliSence and Parse (SQL (on visual studio menu) > Parse) do not work. I have gone through this Troubleshooting guide and many question on SO, but nothing worked.
As I understand is something in project setting that I need to configure or some leftovers I need to clean.
Any suggestions ?
In brief
IntelliSence is enable
SQL CMD is Disabled
In SQL Server Explorer there is an entry (localdb)\ProjectsV12 containing my DB.
Other solutions opened on the same instance of visual studio work fine
ReSharper is installed but is not the issue (I also tried without it)
Note:
The solution (the one that has issues) was created in a previous version of Visual Studio.
Update 15/12/2015
After some digging I found out that the issue may has something to do with the .v12.suo file (which is located alongside with .sln file and is hidden). Copping this file from a similar solution (one that IntelliSence works) eliminates the problem.
Another workaround that it may help, is to go to the "Database Project" properties (right click on the project in Solution Explorer and then select Properties), on the Debug tab and press Restore Default in "Target Connection String". In my case this just changes the Initial Catalog= value to Database and like "magic" IntelliSence works again! (in some cases it may need to restart the Visual Studio)
After this even if I restore (using Edit) the "Target Connection String" to the previous values the IntelliSence continues to work.

How to debug stored procedure in Visual Studio without publishing

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.

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.

Change run action in Visual Studio database project

I have a database project in Visual Studio 2008, and I want the ability to run a preprocessor on my sql code (using it to allow for variable database names without resorting to dynamic sql). So I'd like to change what action occurs when I hit the run button to include running the code through the preprocessor automatically.
Is there a way to do this? I looked through the dbp file, and there don't seem to be any options even close to this.
One option is to use SQLCMD Mode in your database project scripts.
(VS Menu: Data -> TSQL Editor -> SQLCMD Mode).
In your scripts you can define variables that get replaced when you run them. Example:
:SETVAR tablevar Customers
GO
SELECT * FROM $(tablevar) /* translates to SELECT * FROM Customers */

Resources