Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
Declare @AuditDBName Varchar(50),@sQuery Varchar(Max)
   <big> Select @AuditDBName = ConfigStringValue  FROM [dbo].[Common_SystemConfig]  WHERE ConfigKey= 'CMS_AUDIT_DB'
-- This is my db name



SQL
CASE WHEN EXISTS (SELECT 1 FROM #TempClosedWorkItemReportTagIDForOMMAEvictionWB WHERE CaseNumber = CC.CaseNumber AND ReportTagID IN (160))
                THEN
                    Select @sQuery='SELECT SUBSTRING((SELECT '','' + s.CaseStatus FROM '+@AuditDBName+'.[dbo].[Audit_Common_Case] s where CaseNumber=CC.CaseNumber FOR XML PATH('''')),2,200000)' AS [CaseStatusToolTip]
                END


I want to pass my db name in the below query
Posted
Updated 17-Oct-13 20:36pm
v2
Comments
Khandwawala Hatim 18-Oct-13 2:12am    
what problem you are facing in this
Member 9991522 18-Oct-13 2:17am    
i am not able to pass the @auditDbName in the below qury
Khandwawala Hatim 18-Oct-13 2:27am    
what error is it throwing?

1 solution

You can't do it like that: the dB name is parsed at an earlier stage of SQL command processing, and it won't accept a variable as a dB name.
You can do it, but you need to assemble a string version and then use EXEC instead to execute the string.
 
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