How to deal with user added functions in master in VS2022? - sql-server

I am trying to recreate a SQL Server Database project from a live production database in VS2022. I extracted all the different databases from the server as *.dacpac and imported them into VS2022, established the references between them and so on.
Now I've got the problem, that in the life database there are additional Stored procedures and functions in the master, that were added by the developers and are not part of the default masters database that comes with the SQL Server installation.
Thus the references from the databases to these procedures and functions are unresolved, when I reference the System master database. So I created a .dacpac from the live master database and provided it as master reference instead of the system master database. But if I do that, I get other errors:
I have to manually prefix every use of functions or procedures, that are in the master database as master.dbo.some_function instead of simply some_function.
I get errors and warnings for unresolved references to tables like sysobjects and some standard functions and procedures like SP_EXECUTESQL, that are in the master, even if I prefix them:
SQL71502: Procedure: [dbo].[someProcedure] has an unresolved reference to object master.[dbo].[SP_EXECUTESQL]
Number one is annoying but a problem that I can handle, but number two gives me headaches. How do provide references to a master database, that includes both, the user added functions and procedures and the systems default methods and tables? IS there a way to create a .dacpac including the System databases?

Related

SQL Server : one stored procedure used in many databases

I have one main database and I need to replicate it for each client (each client must have one separated database with its own information based on main database).
What is your suggestion to avoid the replication of stored procedures, triggers, functions etc and to have only one code in the main database? But using those procedures, triggers, functions, etc in each database of course.
Thanks and regards.

SSDT SQL Server Data Tools Customer specific requirements

We are using SQL Server Data Tools (SSDT) to manage our customer databases.
In theory all databases are identical, but in practice we have a few stored procedures (and one trigger) that would change from one customer to another.
We created a main SSDT for everything common, and then one SSDT per customer containing only the specific stored procedures (no tables).
In the specific SSDTs we get warnings because SSDT can't find the tables referred in the stored procedures, but we can live with that (obviously SSDT won't be able to validate the table's fields since it can't find the table). For the trigger, we get an error (table can't be found), thus the database project doesn't compile.
How should we manage that? I guess we should not be alone in this situation.
Is there a way for a database project to refer objects (tables) from another database project ?
Thanks,
Yves Forget
Daniel N gave the right direction, I'll just explain. Let's say you have database project named DatabaseA which will contain the only objects that 100% match for every customer. Then you create another database project DatabaseB and include DatabaseA as "the same instance, the same database". In database DatabaseB you can add customer specific objects. Then you can create other database for other customer in a similar way.
IN SSDT you can add another database project or dacpac as a reference.
In the properties for the referenced project you will be able to set where the referenced database resides, same server same database, same server diff database etc
https://msdn.microsoft.com/en-us/library/jj684584%28v=vs.103%29.aspx?f=255&MSPPError=-2147217396

System-Level Custom Views in SQL Server

I would like to do the following in SQL Server (2008 + ):
Define a view, let's call it [MySchema].[MyTableInfo], that queries the SQL Server catalog views, e.g., [sys].[tables], to obtain a customized presentation of the underlying catalog metadata.
Install this view somewhere (in master?) so that it can be called from the context of any database on the server and return the metadata appropriate for that context, just as the catalog views do.
I have seen reference to techniques to do something similar with utility stored procedures, but this is a little different. Is what I'm wanting to do possible? If so, how?
Update:
I found an article that described how to do almost exactly what I want but with stored procedures. The routines are stored in the master database and marked as system objects. When they return metadata from the catalog views/information schema, the do so in the context of the current database.
Using stored procedures to execute these queries would be extremely inconvenient for my use case; is there not a way to mark views and/or table-valued functions as system objects and have them execute in the context of the calling database? I have hacked on this without success...

Mapping to different linked servers per database

I have an MSSQL Server 2008 database "DBLive" that link to three different external servers [ExtServer1, ExtServer2, ExtServer3], all defined in sys.servers. There are several stored procedures that refer to stored procs or tables on the external servers, f.x. like 'SELECT TOP 1 #SomeId = Id FROM [ExtServer1].TheExtDB.dbo.SomeTable WHERE ...'.
Here is the challenge - if I want to put an database on the server - "DBTest" which is an identical copy of "DBLive" - but which should connect to different external servers - how do I make [ExtServer1 .. 3] point to a different external servers for "DBLive" and "DBTest"?
If this cannot be done - what would be the preferred way of linking external databases in such a way that two instances of the same db, can have their own external server references - without having differences in the stored procedures?
You'd probably want to use synonyms here. See the work around suggested in this Microsoft Connect issue (and vote up the issue while you're there).

Creating a New Database from Within a Stored Procedure

Due to an employee quitting, I've been given a project that is outside my area of expertise.
I have a product where each customer will have their own copy of a database. The UI for creating the database (licensing, basic info collection, etc) is being outsourced, so I was hoping to just have a single stored procedure they can call, providing a few parameters, and have the SP create the database. I have a script for creating the database, but I'm not sure the best way to actually execute the script.
From what I've found, this seems to be outside the scope of what a SP easily can do. Is there any sort of "best practice" for handling this sort of program flow?
Generally speaking, SQL scripts - both DML and DDL - are what you use for database creation and population. SQL Server has a command line interface called SQLCMD that these scripts can be run through - here's a link to the MSDN tutorial.
Assuming there's no customization to the tables or columns involved, you could get away with using either attach/reattach or backup/restore. These would require that a baseline database exist - no customer data. Then you use either of the methods mentioned to capture the database as-is. Backup/restore is preferrable because attach/reattach requires the database to be offline. But users need to be sync'd before they can access the database.
If you got the script to create database, it is easy for them to use it within their program. Do you have any specific pre-requisite to create the database & set permissions accordingly, you can wrap up all the scripts within 1 script file to execute.

Resources