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 */
Related
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:
SQL within \Shared\CommonVariables:
Within [p_An_Example]:
The error just says "SQL46010: Incorrect Syntax near \."
-
If I wrap the path in quotes that error is replaced by one on 'BEGIN':
"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
Yes, the option is in SQL -> Execution Settings -> SQLCMD Mode:
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.
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:
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?
I have thousands of stored procedures in my data base. And I can't stand scrolling through the entire list to find the SPROC I'm looking for. Is there a command in sql server mgmt studio to open the file in the editor like 'OPEN dbo.SomeStoredProcedureName'
There is no T-SQL command to do this, as SSMS is just a client management tool. Your best bet is to use the Filter tool built in to SSMS:
I don't believe there is such command but if you just want to see and not update you could use
sp_helptext 'dbo.SomeStoredProcedureName'
This messes up the formatting so you shouldn't use it to update (while you can). I also map a key to it Ctrl-F1 that can be used to just write the name and select an d press Ctrl-F1.
You've got lots of options, depending on exactly what you're trying to do:
You can "open file" and graphically browse to the directory you want, then graphically scroll down to and open the file you want.
You can "use" the database you want, and "exec" the stored procedure you want from a command-line query window.
You can "exec sp_helptext XXX" the stored procedure to see the text in a command-line query window.
You can use "filter" in the GUI to eliminate stuff you don't want to see
You can write a VBScript or Powershell script to do the same stuff the SSMS GUI lets you do.
Etc etc
You could export the whole database to a creation script. Then it would be easy to search in your favorite file viewer.
From Management Studio you can right-click on the database, choose Tasks->Generate Scripts, and then select "Stored procedures". If you're just browsing, this is very handy. Also good to store such scripts in your code repository.
SqlSmash lets you navigate easily to any object (including stored procedures) in SSMS.
Source
Disclaimer: I am the developer for the addin.
I have approximately 100 SQL views that are a variation of this:
select * from RTC.dbo.MyTable
...now I find I need to change the name of the RTC table to something else. Rather than edit one view at a time, is there a way to script out all their drop/create statements to a text file so that I can do a global replacement?
In SSMS right click the database, go to Tasks and select there 'Generate Scripts...'. Select 'Views', select the views you want exported, export.
I'd use PowerShell. If you're not using SQL 2008 Client Tools, install them. Then get the PowerShell client, add the registered snapins (plenty of information out there on how to do that), and then use the directory structure to get to the folder representing your Views.
Then script them using something like:
Get-ChildItems | % {$_.Script()}
Use ScriptOptions to tell it to use an Alter script.
And replace "RTC." with the new database name... and run them using sqlcmd.
PowerShell actually becomes a really nice deployment option too.
I am using SQL Server 2005. If I try to generate scripts for the dB using the Generate Scripts wizard in the management studio (Right click dB-> Tasks-> Generate Scripts)
I get no option like IF EXISTS DROP condition in the list. Whereas some of the SQL Servers installed on different machines have this option enabled.
Is the service pack missing or any option needs to be modified to get this feature?
If you right-click there is a page in the wizard where you can set your scripting options. One option is "Include IF NOT EXISTS". Or goto the Tools -> Options -> SQL Server Object Explorer-> Scripting to set this.
When I execute this command, all I get is a CREATE DATABASE statement and then the associated options are set.
You want to generate a DROP DATABASE statement?
It doesn't look like this functionality is present, but you can generate the drop statement and then paste it in front.