I want to get some data from another database. The name of the other database can change depending on the context, so I figured I'd build the SQL dynamically and execute it. However, it's not doing what I expected. My code is this:
The Select statement shows me that the @SQL variable says this:Code:Select @SQL = ' Select @PolicyName = PolicyName From ' + @DatabaseName + '..uvwSchemeAdministratorDetails Where PolicyNumber = ''' + @PolicyNumber + '''' Select @SQL; Execute @SQL;
However, the Execute bit gives me this error:Code:Select @PolicyName = PolicyName From OtherDatabase..uvwSchemeAdministratorDetails Where PolicyNumber = 'MyPolicy'
Now, I was expecting to have issues populating my variable using this, but I did think it would at least connect to the database correctly. I'm sure I've made a massively obvious mistake in my syntax - any ideas what it might be?Code:Msg 911, Level 16, State 4, Line 55 Database ' Select @PolicyName = PolicyName From OtherDatabase' does not exist. Make sure that the name is entered correctly.
Thanks...




Reply With Quote