SQL Server - define target database name dynamically for MERGE statement - sql

I have 2 tables on 2 different databases on the same server with SQL Server 2012.
I'm synchronizing those 2 tables using the MERGE statement but for the target table need to specify the database name dynamically in a query i.e [targetdb].[dbo].[tablename]
I have prepared a query and use EXEC to execute the dynamic query, but Is it possible to use variable value for [targetdb] database

Related

Load DMV with nested rowset column type into an SQL Server table

I would like to load information from the DMV below into an SQL Server table:
select *
from $System.MDSCHEMA_MEASUREGROUP_DIMENSIONS
This DMV returns a column named DIMENSION_PATH which type is nested rowset (see https://learn.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssas/fe23619e-3dd8-4599-be14-95ab2b327617).
Is there a way to transform this column to fit in an SQL Server table ?
I am using openqueries to access the system DMVs in SSAS.

Calling Search Parameters in SQL for multiple SQL Scripts.

Is it possible to create a variable like call to be used holding the search parameters for a SQL "Select" call?. I have multiple SELECT SQL queries that use the same parameters for EXCLUDING certain data. That would be in the "NOTLIKE" portion of the code.
Can you have one variable in the SQL code that can be called in multiple SQL scripts is my question.
This will be for MS SQL Server Management Studio v17.8.1 and MS SQL Server 2008.
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
AND DB.ProjectName NOT LIKE '%Repair%'
AND DB.ProjectName NOT LIKE '%Replace%'
AND DB.ProjectName NOT LIKE '%Abatement%'
ORDER BY DB.ReportNumber
TO -->
SELECT distinct DB.[ReportNumber]
,DB.[StatusText]
,DB.[ProjActStageName]
,DB.[ProjectOwnerTypeName]
,DB.[ProjTypeName]
,DB.[ProjWorkTypeName]
FROM [Database]
WHERE DB.ReportNumber = DP.ReportNumber
AND DB.ProjTypeName IN ('Custom House', 'Spec Houses')
(Variable excluding certain data)
ORDER BY DB.ReportNumber"
You might want to read this thread for some options on how to approach your problem: SQL Query with NOT LIKE IN

How can I use a SQL Server table in an openquery to an Oracle database?

I have a database on SQL Server and would like to use a column in one of my tables in a linked server openquery I'm running to an Oracle database in order to match values between the two and insert the result into columns in my table in SQL Server .
Essentially I want it to be like this:
SELECT col1, col2, col3, col4
FROM OPENQUERY(link, 'SELECT * FROM Oracle_Table
WHERE ID = MSSQL.dbo.table.ID`)
So I'd like to be able to use my internal table column values to query an external database. They are related tables but different systems.
Would it be possible to get a big list of the values in the SQL Server table column and use it as a variable in the Oracle query? I've searched extensively online but haven't been able to find this one.
You can't pass parameters like I wanted to, but I ended up creating a bunch of queries in Powershell using a for loop and variables within the string to create my large query, then put a UNION ALL after each SELECT FROM OPENQUERY()

How to update a table taking values to a second sql instance?

I have two SQL Server instances on two virtual machines, Test server and Production server.
I need to do an UPDATE to the production server taking data values from test server.
For example :
UPDATE [server_production].dbname.mytable
SET column1 = [server_test].dbname.mytable.column1
How do I do this ?
On your production server, define a Linked Server that points to your test server.
Then, on your production server you can run a query similar to the following to update your column:
UPDATE p
SET column1 = t.column1
from <dbname>.<schema>.mytable p
join <TestLinkedServerName>.<dbname>.<schema>.mytable t
on p.<id> = t.<id>
In the above query, you'll need to provide values for the placeholders:
<dbname> - the name of the prod/test database
<schema> - the schema in which the table is defined (typically dbo)
<TestLinkedServerName> - the name you've given to the linked server
<id> - your PK column, or a column that uniquely identifies rows and provides a way to join the two tables
Have a look at redgate sql data compare
http://www.red-gate.com/products/sql-development/sql-data-compare/

Inserting the bulk data into different data tables

In SQL Server 2008 i am having two different databases in both the i am having a same table here i want to copy data from one database to another database
how can we this can be implemented in sql server 2008?
if the two database is database1 and database2,the table name is Tablename,you can use:
Insert into dbo.database1.TableName select * from dbo.database2.Tablename
You can use different methods for this.
Use DTS tool of SQL Server 2008.
INSERT INTO DBName.Schema.TableName
SELECT F1,F2 FROM TableName