Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Here i have 2 database one is my FARVISIONDB and another is JKUMARJV.Both have same table.Here use check organisation i.e database from front end.Here i have created stored procedure with datanm as parametere.

What I have tried:

So my query inside stored procedure like 

<pre>DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX),@dbnm varchar(1000),@rslt varchar(max)

set @dbnm='farvisiondb'

set @cols = 'STUFF((SELECT distinct '','' + QUOTENAME(STATENAME) 
            from ((select CODE from '+@dbnm+'..M_STATEWISEGSTINACTAG)  e inner join
                 (select CODE,STATENAME from '+@dbnm+'..M_STATEMASTER)  f on e.code=f.code)  
                FOR XML PATH(''''), TYPE).value(''.'', ''NVARCHAR(MAX)'') ,1,1,'''')'

print @cols

EXEC sp_executesql @cols, N'@rslt varchar output',@rslt OUTPUT




It prints query perfectly i.e when i pass farvisiondb as parameter then it perfect
STUFF((SELECT distinct ',' + QUOTENAME(STATENAME) 
                    from (                   
                      (select CODE from farvisiondb..M_STATEWISEGSTINACTAG)  e inner join
                      (select CODE,STATENAME from farvisiondb..M_STATEMASTER)  f on e.code=f.code
                         )  
                    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')


But it not excecute.
Please help me.Thanks in advance.
Posted
Updated 14-Jul-17 5:04am
Comments
BinnyVishwant 13-Jul-17 4:36am    
Is this working for JKUMARJV db?
SujataJK 13-Jul-17 4:46am    
no.I mean in 1 st query .I have print command so its results my second query which i shown above.That means my query executed up to print statement but when i am going to execute @cols then it shows error like

Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FOR'.
BinnyVishwant 13-Jul-17 5:08am    
can you try to remove onedot(.)i,e. farvisiondb..M_STATEWISEGSTINACTAG >
farvisiondb.M_STATEWISEGSTINACTAG
BinnyVishwant 13-Jul-17 5:09am    
Also try to execute only the select stmt first and check whether you are getting required resuts! by giving your databasename manually
SujataJK 13-Jul-17 5:18am    
yes i had tried .Is working when i provide manual database name

You're missing the initial SELECT. You can also simplify the join to avoid the nested queries.
SQL
set @cols = N'SELECT STUFF((SELECT DISTINCT '','' + QUOTENAME(f.STATENAME) 
            FROM ' + QUOTENAME(@dbnm) + N'.dbo.M_STATEWISEGSTINACTAG As e INNER JOIN
                 ' + QUOTENAME(@dbnm) + N'.dbo.M_STATEMASTER As f ON e.code = f.code
            FOR XML PATH(''''), TYPE).value(''.'', ''NVARCHAR(MAX)''), 1, 1, '''')'
 
Share this answer
 
Comments
SujataJK 17-Jul-17 1:05am    
Yes when i did that
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(STATENAME)
from (
(select CODE from FARVISIONDB..M_STATEWISEGSTINACTAG) e inner join
(select CODE,STATENAME from FARVISIONDB..M_STATEMASTER) f on e.code=f.code
)
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
print @cols;
i am not able to write @dbnm(Variable) instead of FARVISIONDB .
You need to fully qualify the table in the dynamic sql. That needs to be in the format [database].[schema].[table]. You are missing out the schema. E.g.
SQL
'FROM ' + @dbnm + '.dbo.M_STATEWISEGSTINACTAG) as STCD'
 
Share this answer
 
v2
Comments
Richard Deeming 14-Jul-17 11:05am    
At least in MS SQL Server, if you omit the schema, it will use the default schema for the current user.

It's still a good idea to include the schema; but that's not the cause of the OP's error. :)
SujataJK 17-Jul-17 1:00am    
yes Richard u are correct

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