SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 change the database name in aquery via paramter?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

akpaga
Constraint Violating Yak Guru

USA
311 Posts

Posted - 08/12/2013 :  11:48:43  Show Profile  Reply with Quote
Hi friends

I have following query populating a report on front end

select * from Customers.customer_name cn
join Houston.Customer_address ca
on cn.id=cd.ID

The front end report now should have a city parameter and whenever the city is chosen

the query should change like this say -the city chosen is Dallas
select * from Customers.customer_name cn
join Dallas.Customer_address ca
on cn.id=cd.ID

i.e the database now has been changed to Dallas.Customer_Address..

Can this be done using case statements? Please explain..Thank You


James K
Flowing Fount of Yak Knowledge

3719 Posts

Posted - 08/12/2013 :  11:59:48  Show Profile  Reply with Quote
It probably is possible, but not easy, clean, or recommended. The reason for that is that the database design is not sound. Naming tables based on data can lead to all kinds of problems.

Ideally you should create a single table for customers with multiple columns, one for a customer_id, others for firstname, lastname, address, city, zip code etc. You can further normalize it if you wish by having a Cities table and referring to that table in the city column of the customers table.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

Canada
594 Posts

Posted - 08/12/2013 :  12:11:56  Show Profile  Reply with Quote
Your design does look worrysome.

But to answer your question, you could acheive this with Dynamic SQL.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000