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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a recordset from a stored procedure 'print'?

Status
Not open for further replies.

cbulgarna

Programmer
Joined
Jul 23, 2001
Messages
1
Location
GB
Hi

I have a dynamic select statement in a stored procedure, but unless I insert 'print @sql' at the end of the procedure, it will not return a recordset (either in the Query Analyser or to ADO). I cannot understand why 'execute(@sql)' will not do the job on its own.

Here's the procedure

---------------------------------

ALTER PROCEDURE sp_buildsql
@theView varchar(12),
@std varchar(10),
@pcode1 varchar(4),
@town varchar(100),
@tradeCode varchar(2)
AS


DECLARE
@sql varchar(1024)

set nocount on

select @sql = 'SELECT * FROM ' + @theView + ' WHERE '
if @std <> &quot;&quot;
select @sql = @sql + ' Std = &quot;' + @std + '&quot; AND '

if @pcode1 <> &quot;&quot;
select @sql = @sql + ' PostCode1 = &quot;' + @pcode1 + '&quot; AND '

if @town <> &quot;&quot;
select @sql = @sql + ' (Town =&quot;' + @town + '&quot; OR Village =&quot;' + @town + '&quot;) AND '

If @tradeCode <> &quot;&quot;
select @sql = @sql + '(trade1 LIKE &quot;' + @tradeCode
select @sql = @sql + '&quot; OR trade2 LIKE &quot;' + @tradeCode
select @sql = @sql + '&quot; OR trade3 LIKE &quot;' + @tradeCode
select @sql = @sql + '&quot; OR trade4 LIKE &quot;' + @tradeCode + '&quot;) AND '


select @sql = @sql + '(((SubsGrade) <> &quot;FA1&quot;) AND ((SubsGrade) <> &quot;FA1A&quot;)) AND ((MailFlag)<>&quot;NO&quot;) ORDER BY Email Desc;'

print @sql
execute(@sql)
set nocount off

GO

--------------------------------

Any help appreciated

CB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top