Variable Which Replace DB Of Name In SSMS
I have two DB (DB1 & DB2) on the same server (instance) and I know i can write query like this:
select d1.name, d2.email from [db1].[dbo].[person] as d1 join [db2].[dbo].[details_person] as d2 on d1.id = d2.id_p
but my question is: can i create something like variable which allows replace [db2] in my query when my database will change name.
I mean code similar like this:
[$(var_db2)].[dbo].[details_person] but i don't know if i can create variable in SSMS which will be referes to database in the same server and if it is possible at all.
Dynamic Sql to set
DB dynamically. Try something like this
declare @char varchar(10) = 'db_name' exec ( 'use '+Quotename(@char) + ' select d1.name, d2.email from [dbo].[person] as d1 join [dbo].[details_person] as d2 on d1.id = d2.id_p' )
DECLARE @sql NVARCHAR(max) DECLARE @char VARCHAR(10) = 'db_rd' SET @sql = 'select d1.name, d2.email from ' + Quotename(@char) + '.[dbo].[person] as d1 join '+ Quotename(@char) + '.[dbo].[details_person] as d2 on d1.id = d2.id_p' EXEC Sp_executesql @sql
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key