I have a table category, which exists in both test and prod db. Whenever I have to get data from MySQL I have to write test or prod in db like below, as openquery doesn't support dynamic query.
SELECT *
INTO #Category
FROM
OPENQUERY([MYSQLLINKEDSERVER], 'SELECT * from test.category') category
I have tried it with execute command, as it worked with dynamic query.
declare @storeId as integer = 1
declare @query nvarchar(max) ='SELECT * from test.category where id = ?'
exec(@query, @storeId) at [MYSQLLINKEDSERVER]
but the issue is, I have to create temporary table every time whenever I have to get data, I have multiple db and multiple tables, I can't create temp table every time.
My problem is, I need a way in which I passed a variable like declare @t nvarchar(250) = 'test', and passed a query select * from category and it fetch data from MySQL test db and stored it in temp table as given below
select * into #category
openqueryOrSOmelogic('select * from category','test')
I don't want to provide schema for temp table and provide db name from variable.
Is there any way to achieve it?
SELECT * from MYSQLLINKEDSERVER.test..category where id = 4or whichever syntax mysql supports, i don't remember if schema goes first or last