Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use SQLCMD Commands in Visual Studio Database Project Stored Procedure?

I have a lot of SQL that is the same over multiple stored procedures.

For example most procedures have the same variables declared, and is in the same try catch block to handle errors.

I'd like to use the :r command so that I can write this code in one file and just import it into each sp. I can use that command in the pre and post build scripts but I can't get it working within a normal 'Build' type database object.

Example:

enter image description here

SQL within \Shared\CommonVariables:

enter image description here

Within [p_An_Example]:

enter image description here

The error just says "SQL46010: Incorrect Syntax near \."

-

If I wrap the path in quotes that error is replaced by one on 'BEGIN':

enter image description here

"SQL46010: Incorrect Syntax near BEGIN."

-

Is there anything I can do to get this working? At the moment [p_An_Example] has the Build Build Action property, and \Shared\CommonVariables is set to None.

(I'm using Visual Studio 2017, the database project is pointing to a 2008 database, SQLCMD is installed etc)

Edit: Not a duplication because I do have SQLCMD mode on... but it turns out SQLCMD commands are not available within stored procedure statements

like image 992
Osie J O'Connor Avatar asked Dec 01 '22 10:12

Osie J O'Connor


2 Answers

Yes, the option is in SQL -> Execution Settings -> SQLCMD Mode:

enter image description here

Note, however, that you can't include sqlcmd code within an SP, as the DBMS can't interpret the code, it's purely for using in a coding environment; such as within SSMS/VS.

like image 93
Larnu Avatar answered Dec 28 '22 07:12

Larnu


In Visual Studio 2019, you can do the same by opening the file with the SQLCMD statements, then click the last icon in the new view to make the errors go away:

enter image description here

like image 27
Serj Sagan Avatar answered Dec 28 '22 08:12

Serj Sagan