Multiple CLR schemas in Visual Studio SSDT project - sql-server

I'm trying to have a single sqlproj project in Visual Studio 2022, representing my SQL Server database.
When creating a CLR stored proc or function in C#, the SqlFunction or SqlProcedure attribute has a Name property, but not a schema. It seems the entire project will use the default schema. I'd like to be able to have more than one schema with CLR items.
I've found a couple different ways of moving procs from one schema to another after deployment, but is there any way to get it right in the first place?
For example, by default, if I create a MyProc procedure, I can reference it as dbo.MyProc from a regular proc. But I want that to live in the other schema. Even if I change schemas as a post-deploy step, then my regular procs with EXEC [other].[MyProc] will not compile properly in Visual Studio since it thinks it's referencing a non-existent procedure.
I don't want multiple projects because I want fully-functional schema compare and publish steps for the whole database.

Related

SQL Server database project - Create change scripts

We started using SQL Server database project in Visual Studio 2012 by connecting an existing database. We are developing a web application that connects with this database.
Now we have around twenty stored procedures updated for the next release. How can I create a script file that contains only the updated stored procedures? We need to send this script to our clients along with the Web application executable files.
Is there any feature that allows us to compare and create only the changed scripts?
Thanks in advance!
Use Schema Compare as follows:
Create a Schema Comparison that compares the database project to one of the production databases.
Run the schema compare.
Exclude everything except for the stored procedures (plus any objects on which they depend).
Click Generate Script.
If you save this Schema Comparison then you can reuse it the next time you need to publish more stored procedure changes.

Visual Studio Database Schema Compare Adds not showing up

I'm setting up a new SQL Server Database Project for the first time using an existing database. I created my new project, then imported the database and it worked just fine.
However when I add a new stored procedures to my new Database Project and then do a Compare Schema to my local database, the Add of the new stored procedure doesn't show up.
To be clear, when running the Compare I'm setting my source to my Database Project and target to my local database.
The Compare Schema works great for modifications or deletions of stored procedure scripts in my Database Project just not when I define a new one. Am I missing something? Why don't new stored procedures define in my project show up in the comparison results?
I'm using VS2013, SQL Server 2008 R2.
It turns out when I created the stored procedure in Visual Studio I chose the Script... option instead of the Stored Procedure... option. The image below shows what I should have done.
Add the file as a Script resulted in the .sql file being added to a different ItemGroup in the project with no build properties. This is what caused the comparison to not pick it up as a stored procedure.
I determined this by looking at the properties of the .sql file (in Visual Studio) that I added and another .sql in the same project where the comparison worked. The file I added as a Script file had different properties than other files that were being treated as real stored procedures.
Moral of the story... make sure you add stored procedures in Visual Studio correctly.

How does one use SSDT to define a stored procedure which has a table-valued parameter?

My project is using VS 2012 and SSDT for SQL Server 2012. We are using SSDT to publish a database straight from VS.
I would like to define a custom table type and a stored procedure which uses that custom table type. However, SSDT will not let me define the table type and the stored procedure in one file.
How does one define a custom table type in SSDT so that it will be deployed ahead of any stored procedures which use that custom type?
SSDT is indeed smart enough to order the type in the publish script before the sproc. Therefore, there was no need to worry about their being in separate files.
Turned out to be pretty neat.

Generating SQL change scripts in SSMS 2008

I have gone through many related SO threads and got some basic info.
Already generated DB diagram.
After that i am unable to find a button/option to generate SQL scripts (create) for all the tables in diagram.
"Generate script" button is disabled, even on clicking the table in diagram.
However i enabled the auto-generate option in tools->designer. But what to do with previous diagrams.
I just want an easy way to auto-generate such scripts (create/alter) and would be gud if i get auto-generated stored procs for insert/selects/update etc.
EDIT: I could do generate scripts for DB objects.
Now:
1. How to import DB diagram from another DB.
2. How to generate (and manage their change integrated with VS source control) routine stored-procs like insert, update and select.
Ok let me ask another way, can experts guide on the usual flow of creating/altering tables (across releases), creating stored-procs (are stored-procs the best way to go ?) and their change-management using SSMS design tools and minimal effort ?
You can go to the Object Explorer in SSMS, and right-click on your own database, and then pick "Tasks" > "Generate Scripts" to generate a whole bunch of scripts for your database.
Mind you - this is just a single set of CREATE statements, basically.
If you're using the visual table designer to modify your tables, you can also have it create a script to handle the changes you've made.
And in Visual Studio 2010 Professional or up, you can also take snapshots of databases, and compare two sets of your database and generate ALTER scripts from those .

SQL Server Stored Procedure Folders/Grouping

We are currently using SQL Server 2000 but will soon be moving to 2008. I am looking for a way to group related stored procedures into folders. SQL Server 2000 does not seem to have this ability and from my searches it looks like 2008 does not either. This seems like a basic feature that most users would want. Wouldn't it make sense to put all generic stored procedures that are shared across multiple projects in one folder and project specific procs in another?
It seems the way most devs do this now is by using some from of ID_SPNAME syntax and sorting them.
Grouping procs and functions by type in the UI would be nice, but Management Studio can't do it. Using SQL Server 2000, I've done what you suggest (prefixing objects with a grouping code and sorting). In 2005 and 2008, consider creating schemas to serve the same purpose, and grouping your objects in those. Note that the object names are prefixed with the schema name in the UI.
CREATE SCHEMA ShoppingCart AUTHORIZATION Joe
CREATE PROCEDURE AddItem ...
... will display in the UI as ShoppingCart.AddItem.
Schemas in Sql Server 2008
The most common way to do this (in SQL 2005/2008) is by using schemas:
HR.spCalculateEmployeeCompensation
HR.spCalculateContractorBonus
Web.spAppendWebLog
Web.spUserEndSession
Reporting.spGetCurrentYearSummary
Reporting.spGetLastMonthDetail
Not only will these visually organize themselves in the SSMS list, but you can apply different permissions to each schema.

Resources