Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need data from multipe database to compaine into single table for analysis purpose.

My code

SQL
Declare @dbname varchar(50),@sqlCommand varchar(1000)
Set @dbname ='dbname'
set @sqlCommand = '
	SELECT  RegDate,  SID, Name, CAddress, TelEM1, TelEM2, Relation, Disablity,  Loan, TRStatus 
        FROM' + @dbname +  'dbo.MemberRecord '

EXEC (@sqlCommand)

Error is
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '.'.

if i using the below code, working fine

SELECT  RegDate,  SID, Name, CAddress, TelEM1, TelEM2, Relation, Disablity,  Loan, TRStatus 
	FROM  [dbname].dbo.[MemberRecord]  ORDER BY SID 


Pls advice me
Thank you

Maideen

What I have tried:

SQL
Declare @dbname varchar(50),@sqlCommand varchar(1000)
Set @dbname ='dbname'
set @sqlCommand = '
	SELECT  RegDate,  SID, Name, CAddress, TelEM1, TelEM2, Relation, Disablity,  Loan, TRStatus 
        FROM' + @dbname +  'dbo.MemberRecord '

EXEC (@sqlCommand)
Posted
Updated 29-Jul-18 13:11pm

Try this. I've pasted your code below exactly as it appears in a copy of cp page content to my clipboard:
Declare @dbname varchar(50),@sqlCommand varchar(1000)
Set @dbname ='dbname'
set @sqlCommand = '
	SELECT  RegDate,  SID, Name, CAddress, TelEM1, TelEM2, Relation, Disablity,  Loan, TRStatus 
        FROM' + @dbname +  'dbo.MemberRecord '

EXEC (@sqlCommand)
Ok. So insert this statement now, (right after the SET and before the EXEC):
PRINT @sqlCommand
And, comment out this statement:
EXEC(@sqlCommand)
Run the whole statement altered as above ...
SELECT  RegDate,  SID, Name, CAddress, TelEM1, TelEM2, Relation, Disablity,  Loan, TRStatus
    FROMdbnamedbo.MemberRecord
See what happened? There's no space between the word FROM and the @dbname variable's assigned value of 'dbname' nor is there a "dot" (.) designating the division between the schema name and the database. Usg the PRINT statement interspersed in your code is one method of debugging which is quite useful because it can be commented out easily to do successive tests.

That "." you're missing has to become part of your concatenation:
FROM ' + @dbname + '.' + 'dbo.MemberRecord'
 
Share this answer
 
Thank you

now Solved

Maideen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900