What you need to do in this case is create a sql server temporary
table (such as create table #temp (column1 nvarchar(10),column2
nvarchar(10))) and then build your @query string so that it inserts
the results of your dynamic select statement into the temp table. You
can then play the whole "exists" game on the results of a query
against your #temp table.
Make sure that you do not build your temp table dynamically. And
remember, you can't insert results from a dynamically built select
statement into a normal variable, but you can insert them into a temp
table.
Good Luck!
[quoted text, click to view] "John Bell" <jbellnewsposts@hotmail.com> wrote in message news:<bo2oar$89l$1@hercules.btinternet.com>...
> See inline
>
> "Arijit Chatterjee" <arijitchatterjee123@yahoo.co.in> wrote in message
> news:ea01504d.0311012028.19b506fb@posting.google.com...
> > Create proc sp_test
> > as
> > Declare @query varchar{500)
> This will not compile
>
> > Declare @var varchar(10)
> > set @var='Test'
> > Set @query = 'Select * from table' + ' Where ' + 'Colname = ' + @var
> If you were doing this correctly the value in @var would be enquoted
>
> > exec (@query)----> Working fine
> > if exists (exec (@query))----> Sending error
> > print 'Hi'
> > Now tell me how to solve this.
>
> Reading books online would be the first place to look.
>
> Then read
http://www.algonet.se/~sommar/dynamic_sql.html on why you should
> justify the use of dynamic SQL.
>
> You should then be able to work out how to return the count using
> sp_executesql to get what is require.
>
> > Regards
> > Arijit Chatterjee
>