Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamically Buld the Statement.

Author  Topic 

faijurrahuman
Starting Member

15 Posts

Posted - 2012-03-01 : 02:37:08
Hi;

I want dynamically search condition.

This is my table(This is generated dynamically ) this is not a physical table.

id Tablename columnname Value
1 Company Company_name Microsoft
2 Address Pcity CA
3 Phone Pnumber 100-4582

I want search the Value in the particular table , In this tables are already in the database(Company,Address,Phone). dynamically pass the tablename and columnname and search the Value.

Ex
Select c.Company_name from Company c
join Address a on a.companyid=c.companyid
join phone p on p.companyid=c.companyid
where 1=1
and c.company_name like '%Microsoft%'
and a.Pcity Like '%CA%'
and p.Pnumber like '%100-4582%'

I want dynamically buld the query and search the condition in the Value column.

How can I do this ..

Thanks.

Share Knowledge team

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-03-01 : 05:01:02
this is not a good idea. IF you think about what you are really asking then you are saying "I want to do what a general purpose SELECT statement does in a sp".

One of the advantages of a sp is that it stores the execution plan for its use. It's tuned for its job by the engine.

There's nothing *stopping* you from writing a dynamic sql solution that would do what you want but its the wrong idea.

Either -- write a sp for each job you want. Or use vanilla SQL for the different calls.

Don't try and invent some sort of meta sp that takes a table name. It just leads to an insecure, inefficient, wrong design

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

faijurrahuman
Starting Member

15 Posts

Posted - 2012-03-01 : 05:12:09
ok.. But here we need that type of solution. ..help to solve the problem....

Share Knowledge team
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-03-01 : 05:27:47
no you don't - you need to rethink your approach. I'm not going to waste my time helping you do something dangerous and stupid.

If you persist in your plan at least please read this and guard against sql injection:
http://www.sommarskog.se/dynamic_sql.html


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -