SQL Server stored procedure - designate database to use - sql-server

I am tasked with creating a stored procedure from a script I wrote which will update tables with data.
Since I run this script against a dev database and a live database, we have always manually changed the USE DATABASE in the script before running.
I am looking for a way to use USE DATABASE within a stored procedure.
Is this possible without having to create two stored procedures of the same script for each database (dev versus live)?

Assuming you mean MS SQL Server. You cannot use USE ... within a stored procedure, but you can directly reference a different database with fully qualified object notation.
database.schema.objectname
Example:
dev_mydb.dbo.MyTable
Do note though that if you need that database name to be variable then you will need to use dynamic SQL to set the dbname.

Related

Does a Stored Procedure calling a SQL Agent Job have to reside on the same server?

I would like to create a stored procedure that calls a SQL Agent job, which will in turn call an SSIS package. The job and the SSIS package will reside on the same database server. However, I would prefer that the stored procedure resides on a different database server. The reason behind this is that we have an app that will be calling the sproc. I don't want to give it access to the database where the SQL Agent job and SSIS package reside.
How would I go about doing this?
In a different database in the same instance is very simple. Just use a doted name (db_name.schema_name.object_name).
In a different instance, you must create a linked server and use a doted name like :
instance_name.db_name.schema_name.object_name

USE Statement in SQL Server

I am currently using USE [databaseName] in my stored procedures. However, is there a way that I can point this at the current local database and not give a database name? I know this may sound a bit obscure but its highly likely that I will be using these Stored Procedures in many different databases.
remove the USE [databaseName] from your stored procedure.
By default, the stored procedure will run on the current database (in which SP is created)

Restore production database to multiple test databases SQL server

What is the best way to copy a production database to multiple test databases, while maintaining the integrity of fully qualified names?
Currently to refresh a test environment we restore the test database from the production database. Then, we script all of the stored procedures/views/etc. and do a find/replace on all of the database references to point at the test objects. After we have all of the references correct, we alter them.
For example, after the database is copied from production, we'll have a stored procedure like so:
alter procedure dbo.SomeProcedure
as
select SomeColumn
from DB.dbo.SomeTable
join Validation.dbo.AnotherTable on SomId = AnoId
For the test database, it needs to be:
alter procedure dbo.SomeProcedure
as
select SomeColumn
from DBQA1.dbo.SomeTable
join ValidationQA1.dbo.AnotherTable on SomId = AnoId
Each test database has views/stored procedures/functions can reference up to 30 different other test databases, so the "find/replace" process is very time consuming and is prone to a lot of errors.
What is the best way to restore these test environments?
We are using SQL Server 2008R2.
Assuming that the different environments are on different SQL Servers (or at least on different Instances), then would recommend that you keep the database names on all environments exactly the same. Use permissions (e.g. Integrated security) to ensure that only the correct environment systems and users access the appropriate environment databases.
However, if you do need to keep different database names for different environments (e.g. all environments on the same SQL instance), you could look at using sqlcmd with the -v switch to parameterize the database name.
Your change scripts would then need to be rewritten like so:
alter procedure dbo.SomeProcedure
as
select SomeColumn
from [$(InternetSecurity)].dbo.SomeTable
join [$(Validation)].dbo.AnotherTable on SomId = AnoId
And then you could write batch files to pass the correct parameter values to sqlcmd.
Alternatively, you could use a .dbproj project in Visual Studio to setup multiple configurations to provide different values for each environment, and generate scripts / publish from Visual Studio.
Also, AFAIK SQL Synonyms are't really going to help here. You would need to replace the 3 part table names in all procs and functions with synonyms, which could confuse the issue as it doesn't make it clear whether the table is local or external.
As far as I know,There is no other simpler way than replacing the database names in the script here.

Dynamic access to tables from another database inside an user function

I have an user defined table function in SQL Server that aggregate data from several tables including a couple of tables of another database. That is done hardcoding the name of the database in the queries, but we want to make the database name configurable (because our databases usually share the server with the databases of other applications).
I tried to construct a dynamic query string inside the function using the database name that is stored in a configuration table, but:
When I tried exec(#sqlStatement) SQL Server said that execute string is not allowed inside a function.
Then I tried exec sp_executesql #sqlStatement and the function was created, but when you execute it SQL Server says that inside a function you can only run extended functions and procedures.
So the question is: is possible create a function or stored procedure that access a table in another database without having to recreate the function when the database name is different?
TIA.
I would really recommend that you settle on fixed database names, so you can avoid this entire problem of using dynamic sql. Can't you come up with unique database names like: your_company_name_XYZ and your_company_name_ABC? if that is not an option you will be doing lots of dynamic SQL, you should read The Curse and Blessings of Dynamic SQL by Erland Sommarskog
All of the restrictions you mention in the question are a limit of SQL Server functions. You can generate and execute dynamic SQL within a stored procedure with no problems.

How to use a system stored procedure in LINQ to SQL

I'd like to add the "msdb.dbo.sp_help_job" system stored procedure to a LINQ to SQL object, but I can't figure out how to specify it. If I create a new Data Connection in Server Explorer and specify the "msdb" database of the server I want, and navigate to "Stored Procedures", that procedure is not listed. Am I looking in the wrong place?
I've added regular (user defined) stored procedures in the past with no problem. I know I could get there by executing it via "ExecuteCommand" on the data context, and I could also create a "wrapper" stored procedure that did nothing but call "sp_help_job", but I'd like to know how to hook it up directly to LINQ, or if it's even possible.
The System Stored Procedures are not actually sitting inside your database, but rather the Read-Only Resource database.
http://msdn.microsoft.com/en-us/library/ms190940.aspx
However, here's how you can make it possible to find them:
Accessing System Databases/Tables using LINQ to SQL?

Resources