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 <> ""
select @sql = @sql + ' Std = "' + @std + '" AND '
if @pcode1 <> ""
select @sql = @sql + ' PostCode1 = "' + @pcode1 + '" AND '
if @town <> ""
select @sql = @sql + ' (Town ="' + @town + '" OR Village ="' + @town + '"
AND '
If @tradeCode <> ""
select @sql = @sql + '(trade1 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade2 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade3 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade4 LIKE "' + @tradeCode + '"
AND '
select @sql = @sql + '(((SubsGrade) <> "FA1"
AND ((SubsGrade) <> "FA1A"
) AND ((MailFlag)<>"NO"
ORDER BY Email Desc;'
print @sql
execute(@sql)
set nocount off
GO
--------------------------------
Any help appreciated
CB
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 <> ""
select @sql = @sql + ' Std = "' + @std + '" AND '
if @pcode1 <> ""
select @sql = @sql + ' PostCode1 = "' + @pcode1 + '" AND '
if @town <> ""
select @sql = @sql + ' (Town ="' + @town + '" OR Village ="' + @town + '"

If @tradeCode <> ""
select @sql = @sql + '(trade1 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade2 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade3 LIKE "' + @tradeCode
select @sql = @sql + '" OR trade4 LIKE "' + @tradeCode + '"

select @sql = @sql + '(((SubsGrade) <> "FA1"



print @sql
execute(@sql)
set nocount off
GO
--------------------------------
Any help appreciated
CB