How to identify a database when it's being moved to another server/instance - sql

I'm writing a tool which is used to perform several database operations.
But the tool should only be used with one specific database.
Now I'm looking for a way, to securely identify the database, the tool is connected to.
First I thought about just checking a string like SERVERNAME\INSTANCE#Database.
Also I found this question where the solution is to use a GUID, but this GUID changes if the DB is restored on another server.
The DB should be recognized even when it is being moved to another server or instance, or if the database name changes.
Is there a reliable way to achieve this?

You might be able to achieve this with an extended property.
To create:
exec sp_addextendedproperty #name = 'dbUniqueIdentifier' #value = 'ABCD1234'
To confirm:
select value from sys.extended_properties where name = 'dbUniqueIdentifier'
In my organization, we use extended properties to identify which build and changeset the database schema came from. The properties survive backup/restore/migration.

Related

EXASol set a custom session variable

In SQL Server (2016) we have the SESSION_CONTEXT() and sp_set_session_context to retrieve/store custom variables in a key-value store. These values are available only in the session and their lifetime ends when the session is terminated. (Or in earlier versions the good old CONTEXT_INFO to store some data in a varbinary).
I am looking for a similar solution in EXASol (6.0).
An obvious one would be to create a table and store this info there, however this requires scheduled cleanup script and more error prone than a built-in solution. This is the fallback plan, however I'd like to be sure that there is no other options.
Another option could be to create individual users in the database and configure them, but just because of the amount of users to be added, this was ruled out.
The use-case is the following: An application has several users, each user have some values to be used in each queries. The application have access only to some views.
This works wonderfully in SQL Server, but we want to test EXASol as an alternative with the same functionality.
I cannot find anything related in the EXASol Manual but it is possible, that I just missed something.
Here is a simplified sample code in SQL Server 2016
sp_set_session_context #key='filter', #value='asd', #read_only=1;
CREATE VIEW FilteredMyTable AS
SELECT Col1, Col2, Col3 FROM MyTable
WHERE MyFilterCol = CONVERT(VARCHAR(32), SESSION_CONTEXT('filter'))
I've tried an obviously no-go solution, just to test if it works (it does not).
ALTER SESSION SET X_MY_CUSTOM_FILTER = "asd"
You cannot really set a session parameter in EXASOL, the only way to achieve something similar is to store the values that you need in a table with a structure like:
SESSION_ID KEY VALUE READ_ONLY
8347387 filter asd 1
With LUA you could create a script that will make easier for you to manage these "session" variables.
I think you can achieve what you need by using the scripting capabilities within Exasol - see section 3.5 in the user manual..
You could also handle the parameterisation 'externally' via a shell script

Add Use Command when Selecting Top 1000 from Database Object

I've been searching for this answer and haven't had any luck. My problem is that whenever I
Select top 1000 from tblX it defaults the database to Master and I have to manually change to the correct DB. I know on my VDM at work when doing this, it adds a Use command which specifies the correct database and gives me a connection instantly. I've read that you can change the default database, but I will be switching back and forth between many databases. So I want my connection defaulted to which ever database the selected table is from.
You can use [db name].dbo.[table name]
You could always change the default schema for you user. But this is not my preffered approach. You should always use two part name naming or in your case 3 part according to the 70-461 Training kit.

How do I declare a variable for a hard coded database?

I have some hard coded database values in my SQL and I need to convert to variables , I have declared them in places but I need Production2 to be changed to #Source_Database_Name variable below but I dont know how to place it in with the Information Schema just after it without getting a syntax error
IF EXISTS(SELECT * FROM Production2.INFORMATION_SCHEMA.COLUMNS
I guess that the only way you can do this is dynamic sql generation (unfortunately). And there's actually quite a few reasons (from database engine's perspective) for not allowing a user to parametrise queries in a way you want. The one that sits on top of my head is that it will make impossible to validate syntax of your query (no way to know that you're referring to what actually exists).
In case you're talking about "being able to execute the same set of SQL against different database(s)" and you're actually executing this sql from code (.NET / anything), you can achieve the same result by specifying target database in connection string (i.e. by changing the level where you set database -- not in the [sql] script, but rather at some external point).

Is it safe to change name column in msdb sysjobs?

I need to rename a lot of similar named job names so i want to run an update statement to change the name column of msdb..sysjobs.
Editing system tables is something i am pretty aware of, but changing job names doesn't feel that dangerous, because i think the job_id is what counts. Is it safe to do it in this case?
I am using sql server 2008.
When you have the same jobs running in multiple environments, it's a better practice to refer to jobs by name rather than by ID, because a job with the same name will be created with a different ID in every environment.
If you change the name of the jobs, make sure that any scripts you use to manipulate those jobs also use the new name.
I think there is nothing to worry about, you can modify it.
Please find detail about sysjobs here.
It is NOT save to just rename it. Using sql profiler shows, that renaming a package using the object exlorer will start several procedures. It will create a new package using sp_add_dtspackage and drop it later using sp_drop_dtspackage. The package id is although set using a parameter, so setting it random really seems to be a potential risk.

How to identify a database as valid model?

Im having trouble identifying my databases as correct and valid version of my model. I have a GUID-id for my model I could use, but where do I put it? I dont want an entire table with only one row for this GUID. Is there any metadata-repository for databases (SQL Server 2008) or is there any other methods of identifying databases except their names? I could name the database to my model-GUID but it doesnt seem right...
The scenario is like this: I have a Entity Framework-model and I identify it by a GUID. I let the user pick a database-connectionstring and now I want to verify that its a valid database (which has been created from my app). I want to do this every time the app starts just to be sure. What Im doing now is checking that a certain table with a certain name exists but thats not good enough. I want to be 99% sure.
Edit: I would prefer to set this value from the DDL-script which is already setting up the database and I want to be able to get it from a SqlCommand in ado.net.
Use an Extended database property
In script:
USE preet;
GO
EXEC sys.sp_addextendedproperty
#name = N'myVersion',
#value = N'999-abc-123-nop';
GO
and
In the UI