0

I have a dynamic query which gets certain records from another database (database server and database name are variables, hence used dynamic query).

Below is the query

DECLARE @SQLString NVARCHAR(1000)      
set @SQLString='
select distinct(select distinct
(
select * from
(
    ------- Inner query (It is more complex than this)

    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails  
    where lAccountId = 10  
    union  
    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails  
    where lAccountId = 10 
) A
    for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS 
)) as AccXmlValue,
lAccountId as AccountId      
into       
 #tmpAccDetails
from       
 AccountDetails      
where       
 AccountDetails.laccountID in (''10,11'')'     

EXECUTE (@SQLString)    

----- This is the final SQL statement (It is more complex than this)

select * from 
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId 

I want to use the #tmpAccDetails in join with MainAccTable.

  1. How can I achieve this, as the temp table will not be in scope outside the dynamic SQL?
  2. Using Global Temp table solves this, but will it be a good idea to use it in this scenario?

My question is similar to this Question here, except for the fact that I will have to use the #tmpAccDetails table in join, rather than selecting the data from this at one go.

Any help will be appreciated. Thanks.

5 Answers 5

1

I think you'll be OK if you create the temp table first....

Eg....

DECLARE @SQLString NVARCHAR(1000)      

CREATE TABLE #tmpAccDetails
(lAccountId int, 
 sAccountName  NVArchar(100)
);


set @SQLString='
select distinct(select distinct
(
select * from
(
    ------- Inner query (It is more complex than this)

    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails  
    where lAccountId = 10  
    union  
    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails  
    where lAccountId = 10 
) A
    for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS 
)) as AccXmlValue,
lAccountId as AccountId      
into       
 #tmpAccDetails
from       
 AccountDetails      
where       
 AccountDetails.laccountID in (''10,11'')'     

EXECUTE (@SQLString)    

Select * from 
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId 
Sign up to request clarification or add additional context in comments.

6 Comments

No, this does not work. The data does not get inserted into the temp table this way.
It does not give any error, it returns empty result.
Suggesting it is inserting somewhere or the dynamic sql is not returning rows. You have got my curiosity on this one........I'm going to experiment
The temp table goes out of scope after the dynamic query, I guess. Do you know any other way of achieving this functionality?
I have solved the question myself and posted the answer. +1 for your time and effort :)
|
1

Just change the order of the statements, like this:

DECLARE @SQLString NVARCHAR(1000)   

set @SQLString='
select distinct(select distinct
(
select * from
(
    ------- Inner query (It is more complex than this)

    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails  
    where lAccountId = 10  
    union  
    select lAccountId, sAccountName 
    from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails  
    where lAccountId = 10 
) A
    for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS 
)) as AccXmlValue,
lAccountId as AccountId      
into       
 #tmpAccDetails
from       
 AccountDetails      
where       
 AccountDetails.laccountID in (''10,11'')'     


CREATE TABLE #tmpAccDetails
(lAccountId int, 
 sAccountName  NVArchar(100)
);

INSERT INTO #tmpAccDetails
EXEC sp_executesql @sSQL    

Select * from 
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId 

4 Comments

What is @sSQL here?
@ser_nicky It works if I run this as a query, does not work if it is put inside a stored procedure.
@Senjuti Mahapatra - Oh, you didn't mention that this portion of code should be run within the stored procedure. Since you haven't provided SP details, please take a look at: social.msdn.microsoft.com/Forums/sqlserver/en-US/…
@ser_nicky: Your answer was almost correct and helped me to solve my question. I have solved the question myself and posted the answer. +1 for your time and effort.
0
BEGIN TRAN  
    DECLARE @SQLString NVARCHAR(1000)      

      CREATE TABLE #tmpAccDetails
      (lAccountId int, 
       sAccountName  NVArchar(100)
      );


      set @SQLString=' INSERT INTO  #tmpAccDetails

      select distinct(select distinct
      (
      select * from
      (
          ------- Inner query (It is more complex than this)

          select lAccountId, sAccountName 
          from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails  
          where lAccountId = 10  
          union  
          select lAccountId, sAccountName 
          from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails  
          where lAccountId = 10 
      ) A
          for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS 
      )) as AccXmlValue,
      lAccountId as AccountId      
      into       
       #tmpAccDetails
      from       
       AccountDetails      
      where       
       AccountDetails.laccountID in (''10,11'')'     

      EXECUTE (@SQLString)    

      Select * from 
      MainAccTable M
      inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId 


      RollBAck Tran

Comments

0

Have you considered "OPENDATASOURCE" to access data from another database ? https://msdn.microsoft.com/fr-fr/library/ms179856.aspx

You can join tables from different databases with this method

    SELECT *
    FROM MainAccTable M
    INNER JOIN OPENDATASOURCE ('SQLOLEDB', 'Data Source=@myInstance;User ID=@myUserName;Password=@myPassword).XXX.dbo.AccountId AS tmp 
ON tmp.AccountId = M.lAccountId 

Comments

0

I have myself solved the question and posting the answer here, so that others may benefit from the same.

DECLARE @SQLString NVARCHAR(MAX)      
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)

select lAccountId, sAccountName 
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails  
where lAccountId = 10  
union  
select lAccountId, sAccountName 
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails  
where lAccountId = 10 
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS 
)) as AccXmlValue,
lAccountId as AccountId      
from       
 '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccountDetails     
where       
 laccountID in (''10,11'')' 

---- Create temp table here
CREATE TABLE #tmpAccDetails 
(
AccXmlValue NVarchar(max),
AccountId int 
);

---- Insert into temp table here
INSERT INTO #tmpAccDetails EXECUTE (@SQLString)

---- Select from temp table here
Select * from 
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.