Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use literal string with NOT EXISTS

Status
Not open for further replies.

rajpree

Programmer
May 24, 2005
18
0
0
US
Hi,
I want to use literal string with NOT EXISTS for example:

SET @select = 'SELECT TOP 1 * FROM ' + @table + ' WHERE issue_id =' +convert(varchar(10),@field_id)

In @select I am constructing sql string and I want to use in
NOT EXISTS(exec(@select)) but it is giving problem, any advise?

Thanks,
Raj.
 
First advice is to stop using dynamic sql.It truly isa poor prgramming practice. Read about sql injection attacks.
Not only that it tends to hurt performance. There is no win from a database perspective to using dynamic sql except to save a little development time. But put that against the security risk and the worse performance and that is no good reason at all to use dyunmic sql.

Personally I never use not exists. I prefer to use left joins. But you don't need the exec part since you are already using dynamic sql.

build your whole outer query in dynamic sql as well as the not exists part with this part something like:
Code:
NOT EXISTS(' +@select +'))'


"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top